c# - 如何查看用第二个线程显示的对话框的 Windows 消息?

标签 c# winforms multithreading messaging

我已经使用

注册了一个消息过滤器
Application.AddMessageFilter(m_messageFilter);

使用它我可以记录用户在应用程序的用户界面中进行的所有鼠标点击。

但是,一个对话框在单独的线程上运行,代码如下:

void Run()
{
    using( MyDialog dialog = new MyDialog() )
    {
        dialog.ShowDialog();
    }
}

Thread thread = new Thread(Run);

我设置的消息过滤器看不到进入此窗口的消息。我怎样才能得到它们(最好不要太打扰)?

我试图覆盖 MyDialog.PreProcessMessage,但我很困惑,这似乎从未被调用过。

谢谢。

最佳答案

几乎所有应用程序方法(包括 AddMessageFilter)都是特定于线程的。因此,如果您想过滤另一个线程上的消息,您必须在该线程上调用 AddMessageFilter。

只需将您的 Run 方法更改为:

void Run()
{
    using( MyDialog dialog = new MyDialog() )
    {
        Application.AddMessageFilter(new MyMessageFilter());
        Application.Run(dialog); 
    }
}

关于c# - 如何查看用第二个线程显示的对话框的 Windows 消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1194527/

相关文章:

c# - 错误仅在发布后出现 : "Unable to connect to any of the specified MySQL hosts."

c# - C# 中的 Listview 慢刷新

java - java中的类级锁定

java - 在 ScheduledThreadPoolExecutor 中终止之前等待任务完成

c# - 我怎样才能连续 QueueUserWorkItems 而不是一次排队?

c# - 在 C# 中处理竞争条件

c# - 为什么 PrintPreview 与纸质打印不完全相同?

c# - WebView2如何运行Javascript函数?

c++ - 这是解决多个生产者的消费者生产者问题的正确方法吗?

c# - toTitleCase 忽略 C# 中的序数