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);
}
}
}
前もって感謝します