如何从 Windows 窗体创建仅显示消息的窗口?
我正在尝试创建一个仅消息窗口,以在 winforms 应用程序中从 MFC 库类接收窗口消息。
我尝试过对 NativeWindow
进行子类化,并在构造函数中请求如下所示的窗口句柄:
CreateParams cp = new CreateParams();
cp.Parent = (IntPtr)HWND_MESSAGE;
this.CreateHandle(cp);
但我收到了抛出的 Win32Exception 消息,并显示消息“创建窗口句柄时出错”。 如何从 Windows 窗体创建仅显示消息的窗口? 使用 NativeWindow
是正确的方法吗?
I'm trying to create a message-only window to receive window messages from an MFC library class, within a winforms application.
I've tried subclassing NativeWindow
, and in the constructor requesting a window handle like this:
CreateParams cp = new CreateParams();
cp.Parent = (IntPtr)HWND_MESSAGE;
this.CreateHandle(cp);
but I get a Win32Exception thrown with the message "Error creating window handle". How do I create a message-only window from windows forms? Is using NativeWindow
the right approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我知道这已经有 7.5 年历史了,但以防万一有人发现这个,我想我会回应。 我使用了微软的 TimerNativeWindow 代码 并删除了计时器功能。 我最终使用了这种方法:
I know this is 7.5 years old, but just in case anyone finds this, I thought I would respond. I used Microsoft's TimerNativeWindow code and removed the timer functionality. I ended up using this approach:
尝试一下:
Try that :
我担心您必须从
Form
派生,并强制窗口不可见。另一种方法(在类库可修改的情况下)是运行没有窗口的消息泵(请参阅 Application.Run 和 Application.AddMessageFilter,或者如果您更喜欢使用 PeekMessage & Co)。
在这种情况下,您可以使用 PostThreadMessage 发送消息通过拥有运行 Application.Run 的线程 ID,但实际上您无法与应用程序消息泵线程同步,因为它不等待消息确认。
I fear that you must derive from a
Form
, and force the window invisible.Another approach (in the case the class library is modifiable) is to run a message pump without a window (see Application.Run and Application.AddMessageFilter, or if you prefer pinvokes using PeekMessage & Co).
In this case you can send messages using PostThreadMessage by having the thread id which as run Application.Run, but actually you cannot synch with the application message pump thread because it doesn't wait message acknowledge.
我相信您还需要指定一个窗口类。
I believe that you'll need to also specify a window class.