处理来自 C# 的本机外部进程窗口事件

本文关键字:外部 进程 窗口 事件 本机 处理 | 更新日期: 2023-09-27 18:34:34

我正在从C#应用程序中启动一个进程(即gnuplot.exe(。该过程可以打开一些窗口,对于该过程,我想拦截以下事件:

  • 打开的窗口
  • 关闭的窗口
  • 聚焦窗口

基本思想是处理用户是否关闭某些窗口或更改活动窗口等,仅指启动的进程。换句话说,我不想处理其他人的焦点更改或关闭的窗口事件,这些事件不是由 gnuplot 窗口抛出的。

你可以帮我吗?是否可以避免轮询?我应该参考哪个 API?您可以粘贴/链接 mwe 或一些示例代码吗?提前谢谢你

更新

正如埃里克·布朗(Eric Brown(所建议的那样,我尝试了这种方法,但仍然不起作用。你能帮我发现我哪里错了吗?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System;
    using System.Windows;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Diagnostics;
    using System.Windows.Automation;
    namespace WinApiEvents
    {
        class Program
        {
            public static void Main()
            {
                Process gp = new Process();
                gp.StartInfo.FileName = @"C:'Software'gp463-win32'gnuplot'bin'gnuplot.exe";
                gp.StartInfo.UseShellExecute = false;
                gp.StartInfo.RedirectStandardInput = true;
                gp.StartInfo.CreateNoWindow = true;
                gp.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                gp.Start();
                AutomationElement targetElement = 
                    AutomationElement.FromHandle(gp.Handle);
                StructureChangedEventHandler structChangedHandler =
                    new StructureChangedEventHandler(OnGnuplotWindowStructureChanged);
                Automation.AddStructureChangedEventHandler(
                    targetElement, TreeScope.Element, structChangedHandler);
                AutomationEventHandler focusHandler =
                    new AutomationEventHandler(OnGnuplotWindowFocusGained);
                Automation.AddAutomationEventHandler(
                    AutomationElement.AutomationFocusChangedEvent, targetElement, TreeScope.Element, focusHandler);
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("set term wxt 1 enhanced");
                sb.AppendLine("plot sin(x)");
                gp.StandardInput.WriteLine(sb.ToString());
                gp.StandardInput.Flush();
                                    sb.Clear();

                sb.AppendLine("set term wxt 2 enhanced");
                sb.AppendLine("plot cos(x)");
                gp.StandardInput.WriteLine(sb.ToString());
                gp.StandardInput.Flush();
                                    sb.Clear();

                sb.AppendLine("set term wxt 3 enhanced");
                sb.AppendLine("plot atan(x)");
                gp.StandardInput.WriteLine(sb.ToString());
                gp.StandardInput.Flush();
                                    sb.Clear();
                MessageBox.Show("Click to exit.");
            }
            private static void OnGnuplotWindowStructureChanged(object src, StructureChangedEventArgs e)
            {
                Console.WriteLine("structure changed window, id=" + e.EventId.ProgrammaticName);
            }
            private static void OnGnuplotWindowFocusGained(object src, AutomationEventArgs e)
            {
                Console.WriteLine("focused window, id=" + e.EventId.ProgrammaticName);
            }
        }
    }

提前谢谢你

处理来自 C# 的本机外部进程窗口事件

我会为此使用 UI 自动化框架。

我假设你有(或可以得到(gnuplot 窗口句柄。 一旦有了它,你可以获取该窗口的UI Automation元素,并设置FocusChanged事件处理程序和StructureChanged处理程序来检测子窗口的打开和关闭。 WindowClosed 事件将告诉您 gnuplot 窗口本身是否关闭。 (您也可以在子窗口上使用它,但您仍然需要侦听 StructureChanged 事件以检测子窗口打开事件。