C#如何等待弹出窗口并选择它进行输入

我基本上是用C#编写一个专门的宏播放器/录音机。 我需要做的一件事是等待弹出窗口(类似于另存为…对话框),然后我可以选择继续播放宏输入。 理想情况下,我希望能够轮询打开的窗口并搜索其标题以获得匹配的窗口标题。 显然我不能使用Processes.GetProcesses(),因为对话框很可能不会显示为新进程。

我想在哪里打开窗户及其标题?

如果要轮询所有打开的窗口,可以使用EnumWindows() 。 我没有编译这段代码,但它应该非常接近function。

public class ProcessWindows { List visibleWindows = new List(); List allWindows = new List(); ///  /// Contains information about visible windows. ///  public struct Window { public IntPtr Handle { get; set; } public string Title { get; set; } } [DllImport("user32.dll")] static extern int EnumWindows(EnumWindowsCallback lpEnumFunc, int lParam); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern int GetWindowLong(IntPtr hWnd, int nIndex); [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)] private static extern void GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); delegate bool EnumWindowsCallback(IntPtr hwnd, int lParam); public ProcessWindows() { int returnValue = EnumWindows(Callback, 0); if (returnValue == 0) { throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), "EnumWindows() failed"); } } private bool Callback(IntPtr hwnd, int lParam) { const int WS_BORDER = 0x800000; const int WS_VISIBLE = 0x10000000; const int GWL_STYLE = (-16); // You'll have to figure out which windows you want here... int visibleWindow = WS_BORDER | WS_VISIBLE; if ((GetWindowLong(hwnd, GWL_STYLE) & visibleWindow) == visibleWindow) { StringBuilder sb = new StringBuilder(100); GetWindowText(hwnd, sb, sb.Capacity); this.visibleWindows.Add(new Window() { Handle = hwnd, Title = sb.ToString() }); } return true; //continue enumeration } public ReadOnlyCollection GetVisibleWindows() { return this.visibleWindows.AsReadOnly(); } } } 

我想你想要FindWindow()