[英]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.