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

发布于 2024-10-03 09:43:48 字数 192 浏览 5 评论 0原文

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

我在哪里可以找到打开的窗口及其标题?

I am basically writing a specialized macro player/recorder in C#. One thing I need to be able to do is wait for a pop up window (something like a Save As... dialog box) that I can then select to continue playing macro input into. Ideally, I would like to be able to poll for open windows and search through their titles for a matching window title.
Obviously I can't use Processes.GetProcesses() because a dialog most likely will not show up as a new process.

Where do I look to get open windows and their titles?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬シ愛 2024-10-10 09:43:48

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

public class ProcessWindows
{
    List<Window> visibleWindows = new List<Window>();
    List<IntPtr> allWindows = new List<IntPtr>();

    /// <summary>
    /// Contains information about visible windows.
    /// </summary>
    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<Window> GetVisibleWindows()
    {
        return this.visibleWindows.AsReadOnly();
    }
}
}

If you want to poll all open windows, you might use EnumWindows(). I didn't compile this code, but it should be pretty close to functional.

public class ProcessWindows
{
    List<Window> visibleWindows = new List<Window>();
    List<IntPtr> allWindows = new List<IntPtr>();

    /// <summary>
    /// Contains information about visible windows.
    /// </summary>
    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<Window> GetVisibleWindows()
    {
        return this.visibleWindows.AsReadOnly();
    }
}
}
秋风の叶未落 2024-10-10 09:43:48

我想你想要 FindWindow()

I think you want FindWindow().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文