WndProc:最小化窗体时如何获取窗口消息 [英] WndProc: How to get window messages when form is minimized

查看:163
本文介绍了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...

推荐答案

最近我还需要解决类似的问题.亚伯的回答使我朝着正确的方向前进.这是通过将普通窗口更改为仅消息窗口来完成操作的完整示例:

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:

class MessageWindow : Form {

  [DllImport("user32.dll")]
  static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

  public MessageWindow() {
     var accessHandle = this.Handle;
  }

  protected override void OnHandleCreated(EventArgs e) {
     base.OnHandleCreated(e);
     ChangeToMessageOnlyWindow();         
  }

  private void ChangeToMessageOnlyWindow() {         
     IntPtr HWND_MESSAGE = new IntPtr(-3);
     SetParent(this.Handle, HWND_MESSAGE);         
  }

  protected override void WndProc(ref Message m) {
     // respond to messages here
  } 
}

请注意构造函数:我发现我需要访问Handle属性,否则将不会调用OnHandleCreated方法.不确定原因,也许有人可以解释原因.

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?

这篇关于WndProc:最小化窗体时如何获取窗口消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆