WndProc: how to get window messages when form is minimized

To communicate with a specific service, I must redefine WindProc. and receive window messages.

However, when the form is minimized, I no longer receive any message. I know this should be so, but is there a workaround for this? I do not want to have a hidden form that always remains open ...

+5
source share
3 answers

I also needed to solve a similar problem recently. Abel's answer set me in the right direction. Here is a complete example of how I did this by changing the normal window to 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 . , , - , .

, : Windows?

+8

, , , . , #, , .

MSDN. , , API , .NET.

+3

You can try NativeWindowto 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
0
source

All Articles