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

发布于 2024-11-25 11:15:54 字数 128 浏览 1 评论 0原文

为了与某个服务通信,我必须重写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

零時差 2024-12-02 11:15:54

我最近也需要解决类似的问题。亚伯的回答让我找到了正确的方向。下面是我如何做到这一点的完整示例,将普通窗口更改为仅消息窗口:

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 方法将无法获取叫。不确定原因,也许有人可以解释原因。

我相信我的示例代码也会回答相关问题: 如何从 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:

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. 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?

杀お生予夺 2024-12-02 11:15:54

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

娜些时光,永不杰束 2024-12-02 11:15:54

您可以尝试 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

You can try NativeWindow to receive messages (VB code, sorry):

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文