6

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

Abel
  • 56,041
  • 24
  • 146
  • 247
lenniep
  • 679
  • 4
  • 11
  • 27
  • Have you been looking at the `NC_*` messages? Just like the hover in the titlebar, this might also be a "non-client area". Also: what do you mean with window messages? Like when a mouse is hovered over the minimized form? – Abel Jul 22 '11 at 07:37

3 Answers3

10

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?

Community
  • 1
  • 1
jrequejo
  • 195
  • 1
  • 6
  • 1
    ATTENTION: You must set ShowInTaskbar = false. ATTENTION: A message-only window is useless if you want to send messages from another process because despite the MSDN says that a message-only window should be found with FindWindowEx, it is not. – Elmue Dec 31 '14 at 18:40
3

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.

Abel
  • 56,041
  • 24
  • 146
  • 247
0

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
Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
DaveA
  • 33
  • 5