没有窗口我可以发送/接收窗口消息吗?

时间:2022-04-13 09:50:41

I'm writing a .NET wrapper around an old MFC-based library we have. It's based around a class that sends notifications using window messages; it has a function that lets the user pass in a handle to a window, and that window will receive the messages.

我正在编写一个围绕我们拥有的基于MFC的旧库的.NET包装器。它基于一个使用窗口消息发送通知的类;它有一个函数,允许用户将句柄传递给窗口,该窗口将接收消息。

I could just require the users of my wrapper to subclass Control and pass their control's handle in order to receive messages, but that's horrible. I want my wrapper class to have events which fire whenever the old library sends a message, and then I can do the decoding of the message into something sensible. But, I don't want my wrapper class to have to be a control.

我可以要求我的包装器的用户继承Control并传递他们的控件的句柄以接收消息,但这太可怕了。我希望我的包装器类具有在旧库发送消息时触发的事件,然后我可以将消息解码为合理的事物。但是,我不希望我的包装类必须是一个控件。

Is there a way for me to create a 'dummy' window handle, and receive the messages sent to that handle, without creating a window?

有没有办法让我创建一个“虚拟”窗口句柄,并接收发送到该句柄的消息,而不创建窗口?

3 个解决方案

#1


There is a concept of MessageOnly Windows which can help you. You may create an internal message only window in your wrapper class and pass this handle to the old library.

MessageOnly Windows有一个概念可以帮助您。您可以在包装器类中创建一个仅内部消息窗口,并将此句柄传递给旧库。

#2


You could try creating a thread with a message pump and sending your messages to that. The thread then raises any necessary events that you want to handle in your C# code.

您可以尝试使用消息泵创建一个线程并将消息发送到该线程。然后,该线程会引发您要在C#代码中处理的任何必要事件。

#3


You can't create a window handle without having a window, since the window handle is the window as far Windows is concerned, but you can make a window without the WS_VISIBLE flag set, and use it for message relaying only. I use that technique sometimes to do cross-thread communication in MFC-only applications (don't tell anyone ;) ). You could derive a (c++) class from CWnd, let it process the messages, and call functions or emit signals for every message that is received. I guess that would make it work with your C# code although I don't have experience with that.

您不能在没有窗口的情况下创建窗口句柄,因为窗口句柄是Windows所关注的窗口,但您可以创建一个没有设置WS_VISIBLE标志的窗口,并仅将其用于消息中继。我有时使用该技术在仅MFC应用程序中进行跨线程通信(不要告诉任何人;))。您可以从CWnd派生(c ++)类,让它处理消息,并为每个收到的消息调用函数或发出信号。我想这会使它与你的C#代码一起工作,尽管我没有相关经验。

#1


There is a concept of MessageOnly Windows which can help you. You may create an internal message only window in your wrapper class and pass this handle to the old library.

MessageOnly Windows有一个概念可以帮助您。您可以在包装器类中创建一个仅内部消息窗口,并将此句柄传递给旧库。

#2


You could try creating a thread with a message pump and sending your messages to that. The thread then raises any necessary events that you want to handle in your C# code.

您可以尝试使用消息泵创建一个线程并将消息发送到该线程。然后,该线程会引发您要在C#代码中处理的任何必要事件。

#3


You can't create a window handle without having a window, since the window handle is the window as far Windows is concerned, but you can make a window without the WS_VISIBLE flag set, and use it for message relaying only. I use that technique sometimes to do cross-thread communication in MFC-only applications (don't tell anyone ;) ). You could derive a (c++) class from CWnd, let it process the messages, and call functions or emit signals for every message that is received. I guess that would make it work with your C# code although I don't have experience with that.

您不能在没有窗口的情况下创建窗口句柄,因为窗口句柄是Windows所关注的窗口,但您可以创建一个没有设置WS_VISIBLE标志的窗口,并仅将其用于消息中继。我有时使用该技术在仅MFC应用程序中进行跨线程通信(不要告诉任何人;))。您可以从CWnd派生(c ++)类,让它处理消息,并为每个收到的消息调用函数或发出信号。我想这会使它与你的C#代码一起工作,尽管我没有相关经验。