【问题标题】:Get focus on third party application and return专注于第三方应用并返回
【发布时间】:2013-01-14 03:06:24
【问题描述】:

我正在用 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);
} 

我在下面有一个用于 TextBox 的按键处理程序。 当按键被按下时,我专注于嵌入在我的程序中的第三方应用程序。

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 中不受干扰地输入。

有可能吗?有没有更好,更简单的解决方案?很乐意听取任何建议。

由于我不能回答自己的问题,所以我会写在这里:

我找到了解决人们问题的解决方案

这是我所做的:

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();

}

感谢大家的帮助

【问题讨论】:

    标签: c# .net focus


    【解决方案1】:

    当您需要它重新集中注意力时:

    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 始终位于您离开它的位置。所以应该没问题...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-19
      • 2017-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多