简体   繁体   English

WndProc:最小化表单时如何获取 window 消息

[英]WndProc: How to get window messages when form is minimized

To communicate with a certain service, I have to override the WindProc .要与某个服务通信,我必须覆盖WindProc and receive window messages.并接收 window 消息。

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:这是我如何做到的完整示例,通过将普通 window 更改为仅消息 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
  } 
}

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.注意构造函数:我发现我需要访问 Handle 属性,否则 OnHandleCreated 方法将不会被调用。 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?我相信我的示例代码也会回答一个相关问题: 如何从 windows forms 创建仅消息 window?

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.如果您想接收 window 消息,但不想显示接收它们的表单,则可以使用仅消息 window,它从不显示。 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.如果您使用它,则不再需要您用于与用户交互的实际 C# 表单来接收来自您的 window 服务的消息。

Here's more on the subject as MSDN . 这里有更多关于 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.不过要提醒一下,它需要对 Window API 进行大量操作,因为 Z3982DAZ1 不直接支持仅消息 window。

You can try NativeWindow to receive messages (VB code, sorry):你可以试试NativeWindow来接收消息(VB代码,不好意思):

Imports System.Windows.Forms

Public Class MyClass: Inherits NativeWindow

Private piFormHandle As Integer = 0
Sub New()
    Me.CreateHandle(New CreateParams)
    piFormHandle = CInt(Me.Handle)
End Sub

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    Select Case (m.Msg)
        Case MyMessage
    End Select
    MyBase.WndProc(m)
End Sub

End Class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM