0

我正在尝试在控件中实现消息循环,我可以使用它来查看当前进程中的所有消息,查找特定消息,并在检测到特定消息时触发事件。

这是我当前的代码:

    public void MessageHandler()
    {
        MSG msg;

        while (GetMessage(out msg, IntPtr.Zero, 0, 0) > 0)
        {
            if (msg.message == 272)
            {
                // Fire event here
            }
            else
            {
                TranslateMessage(ref msg);
                DispatchMessage(ref msg);
            }
        }
    }

    [DllImport("user32.dll")]
    static extern sbyte GetMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax);
    [DllImport("user32.dll")]
    static extern bool TranslateMessage([In] ref MSG lpMsg);
    [DllImport("user32.dll")]
    static extern IntPtr DispatchMessage([In] ref MSG lpmsg);

此循环当前正在异步运行。我遇到的问题是我似乎没有收到任何消息。循环在 GetMessage() 调用时退出。

如果 GetMessage() <= 0 的返回值,我尝试运行循环 while(true) 并继续,但这似乎产生了相同的结果。

任何投入将不胜感激。

4

0 回答 0