WndProc:表单最小化时如何获取窗口消息
为了与某个服务通信,我必须重写 WindProc
。并接收窗口消息。
但是,当表单最小化时,我不再收到任何消息。我知道它必须是这样的,但是有解决方法吗?我不想有一个始终保持打开状态的隐藏表单......
To communicate with a certain service, I have to override the WindProc
. and receive window messages.
However, when the form is minimized, I get no longer any message. I know that it has to be like that, but is there a workaround for this? I don't want to have a hidden form which stays always open...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最近也需要解决类似的问题。亚伯的回答让我找到了正确的方向。下面是我如何做到这一点的完整示例,将普通窗口更改为仅消息窗口:
注意构造函数:我发现我需要访问 Handle 属性,否则 OnHandleCreated 方法将无法获取叫。不确定原因,也许有人可以解释原因。
我相信我的示例代码也会回答相关问题: 如何从 Windows 窗体创建仅显示消息的窗口?
I've also needed to solve a similar problem recently. Abel's answer set me on the right direction. Here is a complete example of how I did it, by changing a normal window into a message-only window:
Pay attention to the constructor: I've found that I need to access the Handle property or otherwise the OnHandleCreated method won't get called. Not sure of the reason, perhaps someone can explain why.
I believe my sample code also would answer a related question: How do I create a message-only window from windows forms?
如果您想要接收窗口消息,但不想显示用于接收消息的表单,则可以使用仅消息窗口,该窗口从不显示。如果您使用它,则不再需要用于与用户交互的实际 C# 表单来接收来自窗口服务的消息。
这里有关于 MSDN 主题的更多信息 。但需要注意的是,它需要大量使用 Window API,因为 .NET 不直接支持仅消息窗口。
If you want to receive window messages, but don't want to show a form for receiving them, you can use a message-only window, which is never displayed. If you use that, the actual C# Form you use for interacting with the user is no longer needed to also receive the messages from your window service.
Here's more on the subject as MSDN. A warning though, it requires quite a bit of playing around with the Window API, because a message-only window is not directly supported by .NET.
您可以尝试
NativeWindow
来接收消息(VB代码,抱歉):You can try
NativeWindow
to receive messages (VB code, sorry):