C# 关注第三方应用程序并返回
我在用C写 我有一个进程,它将第三方应用程序嵌入到我的程序中。 我还有一个RichTextBox,我在其中编写文本,然后它会实时显示在嵌入式应用程序中。一切正常,但我需要移动鼠标,因为应用程序将获得焦点,然后刷新并显示更改 这是一个过程:C# 关注第三方应用程序并返回,c#,.net,focus,C#,.net,Focus,我在用C写 我有一个进程,它将第三方应用程序嵌入到我的程序中。 我还有一个RichTextBox,我在其中编写文本,然后它会实时显示在嵌入式应用程序中。一切正常,但我需要移动鼠标,因为应用程序将获得焦点,然后刷新并显示更改 这是一个过程: private void button2_Click(object sender, EventArgs e) { System.IO.File.WriteAllText(@"ForParsing.txt", textBox1.Text); p
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);
}
我下面有一个文本框的按键处理程序。
当按下该键时,我关注嵌入在程序中的第三方应用程序
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
System.IO.File.WriteAllText(@"ForParsing.txt", textBox1.Text);
//Focus on third party application
SetForegroundWindow(pdf.MainWindowHandle);
}
到目前为止还不错。
现在的问题是:我希望焦点立即返回到courser在文本框中的相同位置。我希望能够继续在文本框中写作,就像除了实时刷新嵌入式应用程序之外什么都没有发生一样
简单地说,我需要第三方应用程序立即刷新(获得焦点),并且我能够在文本框中的当前停止位置不受干扰地键入内容
有可能吗?有更好、更简单的解决方案吗?我很乐意听取任何建议
由于我不能回答自己的问题,我将在这里写下:
我找到了解决人们问题的办法
以下是我所做的:
私有void richTextBox1_TextChanged(对象发送方,事件参数e)
{
System.IO.File.WriteAllText(@“ForParsing.txt”,textBox1.Text)
}
感谢您在需要时为大家提供的帮助,帮助大家重新聚焦:
if (!handle.Equals(IntPtr.Zero))
{
if (NativeMethods.IsIconic(WindowHandle))
NativeMethods.ShowWindow(WindowHandle, 0x9); // Restore
NativeMethods.SetForegroundWindow(handle);
}
其中:
[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);
通常情况下,当您重新聚焦时,tabindex始终位于您离开它的位置。所以这不应该是个问题
[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);