简体   繁体   中英

Get focus on third party application and return

I'm writing in C#.

I have a process that lunches a third party application embedded within my program. I also have a RichTextBox in which I write text and then it shown in the embedded application real time. Everything works but I need to move the mouse, for the application will get focus and then refresh and show me the changes.

This is the process:

private void button2_Click(object sender, EventArgs e)
{
    System.IO.File.WriteAllText(@"ForParsing.txt", textBox1.Text);

    pdf.StartInfo.FileName = @"yap.exe";
    pdf.StartInfo.Arguments = "ForParsing.dvi";
    pdf.Start();
    pdf.WaitForInputIdle(-1);
    SetParent(pdf.MainWindowHandle, this.splitContainer2.Panel1.Handle);
    SetWindowPos(pdf.MainWindowHandle, HWND_TOP,
        this.splitContainer2.Panel1.ClientRectangle.Left,
        this.splitContainer2.Panel1.ClientRectangle.Top,
        this.splitContainer2.Panel1.ClientRectangle.Width,
        this.splitContainer2.Panel1.ClientRectangle.Height,
        SWP_NOACTIVATE | SWP_SHOWWINDOW);
} 

I have a key press handler below for the TextBox. When key is pressed, I Focus on a third party application that is embedded in my program.

private void richTextBox1_TextChanged(object sender, EventArgs e)
{
            System.IO.File.WriteAllText(@"ForParsing.txt", textBox1.Text);
            //Focus on third party application
            SetForegroundWindow(pdf.MainWindowHandle);
}

So far so good. Now the problem: I want the focus instantly returned to the same place the courser was in the TextBox. I want to be able to keep on writing in the TextBox like nothing happened except the real time refreshing of the embedded application.

In simple words, I need the third party application to refresh (gain focus) instantly and me be able to type without interference, at the current position I stopped, in the TextBox.

Is it possible to so? Is there a better, more simple solution for this? would happily listen to any advice.

As I'm not allowed to answer my own questions, I will write it here:

I've found a solution tinkering with people's problems

Here is what I've done:

private void richTextBox1_TextChanged(object sender, EventArgs e) { System.IO.File.WriteAllText(@"ForParsing.txt", textBox1.Text);

        //Focus on third party application
        SetForegroundWindow(pdf.MainWindowHandle);

        //Restore focus
        pdf.WaitForInputIdle();
        SetForegroundWindow(this.Handle);
        this.Focus();

}

Thanks for the help everyone

When you need it to focus back up:

if (!handle.Equals(IntPtr.Zero))
{
    if (NativeMethods.IsIconic(WindowHandle))
        NativeMethods.ShowWindow(WindowHandle, 0x9); // Restore

    NativeMethods.SetForegroundWindow(handle);
}

Where:

[DllImport("User32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern Boolean IsIconic([In] IntPtr windowHandle);

[DllImport("User32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern Boolean SetForegroundWindow([In] IntPtr windowHandle);

[DllImport("User32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern Boolean ShowWindow([In] IntPtr windowHandle, [In] Int32 command);

Normally, when you focus back, the tabindex is always on the same position you left it to. So it shoundn't be a problem...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM