[英]Get focus on third party application and return
我正在用C#编写。
我有一个过程可以使程序中嵌入的第三方应用程序吃午餐。 我也有一个RichTextBox,可以在其中编写文本,然后将其实时显示在嵌入式应用程序中。 一切正常,但是我需要移动鼠标,因为应用程序将获得焦点,然后刷新并向我显示更改。
这是过程:
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);
}
到现在为止还挺好。 现在的问题是:我希望焦点立即返回到课程框在TextBox中的相同位置。 我希望能够继续在TextBox中进行书写,除了实时刷新嵌入式应用程序外,什么都没有发生。
简而言之,我需要第三方应用程序立即刷新(获得焦点),并且我能够在不中断的情况下在TextBox的当前位置键入内容。
有可能吗? 是否有更好,更简单的解决方案? 会很乐意听任何建议。
由于我不允许回答自己的问题,因此我将在这里写下:
我找到了解决人们问题的解决方案
这是我所做的:
私有无效richTextBox1_TextChanged(对象发送者,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();
}
谢谢大家的帮助
需要备份时:
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始终位于您留在的位置。 所以这绝对不是问题...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.