如何查看第二个线程显示的对话框的窗口消息?

发布于 2024-07-29 13:43:03 字数 458 浏览 2 评论 0原文

我已经使用它注册了一个消息过滤器

Application.AddMessageFilter(m_messageFilter);

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

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

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

Thread thread = new Thread(Run);

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

我尝试覆盖 MyDialog.PreProcessMessage,但我很困惑,这似乎永远不会被调用。

谢谢。

I've registered a message filter using

Application.AddMessageFilter(m_messageFilter);

using this I can log all of the mouse clicks a user makes in the application's user interface.

However, one dialog is run on a separate thread, with code something like:

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

Thread thread = new Thread(Run);

The message filter I set up doesn't get to see messages that go to this window. How can I get them (ideally without being too intrusive)?

I tried to override MyDialog.PreProcessMessage, but I'm confused that this never seems to get called.

Thanks.

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

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

发布评论

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

评论(3

不乱于心 2024-08-05 13:43:03

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

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

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

Pretty much all Application methods, including AddMessageFilter, are thread specific. So, if you want to filter the messages on the other thread you have to call AddMessageFilter on that thread.

Just change your Run method to:

void Run()
{
    using( MyDialog dialog = new MyDialog() )
    {
        Application.AddMessageFilter(new MyMessageFilter());
        Application.Run(dialog); 
    }
}
白云悠悠 2024-08-05 13:43:03

只是尝试一下,但您也许可以利用 volatile 关键字,下面是 使用的示例一个易失性布尔值,用于检查不同线程上进程之间的状态

您也许可以调整该方法来满足您的需求。 根据第二个对话框 {EDIT} 上是否出现消息来翻转它。

关于 PreProcessMessage,我相信这仅适用于键盘输入,而不适用于鼠标。 您可能必须重写 WndProc 才能获取这些事件,这很可能是重写的 PreProcessMessage 未触发的原因。

祝你好运!

Just a stab, but you may be able to leverage the volatile keyword, here's an example of using a volatile bool to check state between processes on different threads.

You could probably adapt that methodology to meet your needs. Flip it depending on whether or not the msg came across on the second dialog

{EDIT} In relation to the PreProcessMessage, I believe that is only for Keyboard input, not mouse. You'd probably have to override WndProc to get those events, that's most likely why the overridden PreProcessMessage isn't firing.

Good Luck!

晌融 2024-08-05 13:43:03

我想您应该按以下方式重写您的代码

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

或者可能

Application.Run(dialog);

在内部使用 { ... } 也可以工作

Application.Run() 开始在当前线程上运行标准应用程序消息循环。 通常消息循环只在主线程中工作。 如果没有 Application.Run() 标准消息循环将不会在第二个线程中启动。

这是一篇文章在单独的线程上创建启动屏幕表单,其中使用了类似的技术 - 第二种形式(SlpashScreen)在另一个线程中启动,它们调用 Application.Run(splash); 在第二个线程中。

正如人们从本文中可以了解到的那样,在不同线程中运行表单需要开发人员非常小心地考虑如何关闭/处置表单。

I suppose you should rewrite your code in the following way

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

Or may be

Application.Run(dialog);

inside using { ... } will also work

Application.Run() begins running a standard application message loop on the current thread. Usually message loop is working only in the main thread. Without Application.Run() standard message loop will not start in the second thread.

Here is an article Creating a Splash Screen Form on a Separate Thread where similar technique was used - second form (SlpashScreen) is started in another thread and they call Application.Run(splash); in this second thread.

As one can get from this article, running forms in different threads requires from a developer to be very careful regarding how you will close/dispose forms.

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