Share via


How to: Use the MessageWindow Class 

The .NET Compact Framework provides the MessageWindow and Message classes to generate and receive Windows-based messages. The MessageWindow class creates a window in native code with a handle to your form and performs the required platform invoke calls to native Windows functions. The MessageWindow class is available only in the .NET Compact Framework.

Use the Window handle to the message window, Hwnd, for sending Windows messages to the message window. You can generate messages by using Create in managed code, or by using a native control in your application. You can receive only the messages that you generate. You cannot use MessageWindow to monitor operating system messages.

The message window notifies a form, using the WndProc method, when it detects specific messages enabling you to provide code in response to a Windows-based message.

NoteNote

Note that the .NET Compact Framework version 2.0 provides the ability to marshal delegates as Function Pointers, and directly call managed functions from native code, as an alternative to MessageWindow. See Interoperability in the .NET Compact Framework for more information.

Example

This example demonstrates the functionality of a MessageWindow but without a native component. It sends Windows messages, containing the current pointer x and y coordinates, to a form when the mouse pointer is within a Rectangle in custom control or in a Panel control. The custom control appears as a box on the form. Both controls use the OnMouseMove method for sending messages. These messages contain the x-coordinate and the y-coordinate. The form responds to user defined WM_BOXUPDATE and WM_PNLUPDATE messages by updating a label with the current x-coordinate and y-coordinate and the control that sent the message.

When the mouse is outside the box and panel, the OnMouseMove method of the form updates the label with x-coordinate and y-coordinate in the context of the form.

Imports System
Imports System.Windows.Forms
Imports Microsoft.WindowsCE.Forms

Public Class MessageWindowForm
 Inherits System.Windows.Forms.Form
 Private mainMenu1 As System.Windows.Forms.MainMenu

 ' Create an instance of MsgWindow, a derived MessageWindow class.
 Private MsgWin As MsgWindow

 Public Sub New()

  InitializeComponent()

  ' Create the message window using this form for its constructor.
  Me.MsgWin = New MsgWindow(Me)
 End Sub

 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  MyBase.Dispose(disposing)
 End Sub
#Region "Windows Form Designer generated code"

 Private Sub InitializeComponent()
  Me.mainMenu1 = New System.Windows.Forms.MainMenu
  '
  ' MessageWindowForm
  '
  Me.Menu = Me.mainMenu1
  Me.Text = "Message Window Test"
 End Sub
#End Region


 Shared Sub Main()
   Application.Run(New MessageWindowForm)
 End Sub


 ' Process taps to generate messages
 ' with the WParam and LParam parameters
 ' using the X and Y mouse coordinates.
 Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)
 Dim msg As Microsoft.WindowsCE.Forms.Message = _
  Microsoft.WindowsCE.Forms.Message.Create(MsgWin.Hwnd, _
    MsgWindow.WM_CUSTOMMSG, New IntPtr(e.X), New IntPtr(e.Y))
    MessageWindow.SendMessage(msg)
  MyBase.OnMouseMove(e)
 End Sub

 ' This callback method responds to the Windows-based message.
 Public Sub RespondToMessage(ByVal x As Integer, ByVal y As Integer)
  Me.Text = "X = " + x.ToString() + ", Y= " + y.ToString()
 End Sub
End Class

' Derive MessageWindow to respond to
' Windows messages and to notify the
' form when they are received.

Public Class MsgWindow
 Inherits MessageWindow
 ' Assign integers to messages.
 ' Note that custom Window messages start at WM_USER = 0x400.
 Public Const WM_CUSTOMMSG As Integer = &H400

 ' Create an instance of the form.
 Private msgform As MessageWindowForm

 ' Save a reference to the form so it can
 ' be notified when messages are received.
 Public Sub New(ByVal msgform As MessageWindowForm)
  Me.msgform = msgform
 End Sub

' Override the default WndProc behavior to examine messages.
 Protected Overrides Sub WndProc(ByRef msg As Microsoft.WindowsCE.Forms.Message)
  Select Case msg.Msg
  ' If message is of interest, invoke the method on the form that
  ' functions as a callback to perform actions in response to the message.
  Case WM_CUSTOMMSG
   Me.msgform.RespondToMessage(Fix(msg.WParam.ToInt32), Fix(msg.LParam.ToInt32))
  End Select

 ' Call the base class WndProc method
 ' to process any messages not handled.
 MyBase.WndProc(msg)
 End Sub
End Class
using System;
using System.Windows.Forms;
using Microsoft.WindowsCE.Forms;

namespace MsgWindow
{
public class MessageWindowForm : System.Windows.Forms.Form
{
 private System.Windows.Forms.MainMenu mainMenu1;

 // Create an instance of MsgWindow, a derived MessageWindow class.
 MsgWindow MsgWin;

 public MessageWindowForm()
 {

  InitializeComponent();

  // Create the message window using this form for its constructor.
 this.MsgWin = new MsgWindow(this);

  }
  protected override void Dispose( bool disposing )
  {
   base.Dispose( disposing );
  }
  #region Windows Form Designer generated code
  private void InitializeComponent()
  {
   this.mainMenu1 = new System.Windows.Forms.MainMenu();
   this.Menu = this.mainMenu1;
   this.Text = "Message Window Test";

  }
  #endregion

  static void Main()
  {
   Application.Run(new MessageWindowForm());
  }

  // Process taps to generate messages
  // with the WParam and LParam parameters
  // using the X and Y mouse coordinates.
  protected override void OnMouseMove(MouseEventArgs e)
  {
   Message msg = Message.Create(MsgWin.Hwnd,
    MsgWindow.WM_CUSTOMMSG,
    (IntPtr)e.X,
    (IntPtr)e.Y);
   MessageWindow.SendMessage(ref msg);
   base.OnMouseMove(e);
  }

  // This callback method responds to the Windows-based message.
  public void RespondToMessage(int x, int y)
  {
   this.Text = "X = " + x.ToString() + ", Y= " + y.ToString();
  }
 }

 // Derive MessageWindow to respond to
 // Windows messages and to notify the
 // form when they are received.
 public class MsgWindow : MessageWindow
 {
  // Assign integers to messages.
  // Note that custom Window messages start at WM_USER = 0x400.
  public const int WM_CUSTOMMSG = 0x0400;


  // Create an instance of the form.
  private MessageWindowForm msgform;

  // Save a reference to the form so it can
  // be notified when messages are received.
  public MsgWindow(MessageWindowForm msgform)
  {
   this.msgform = msgform;
  }

  // Override the default WndProc behavior to examine messages.
  protected override void WndProc(ref Message msg)
  {
   switch(msg.Msg)
   {
    // If message is of interest, invoke the method on the form that
    // functions as a callback to perform actions in response to the message.
    case WM_CUSTOMMSG:
     this.msgform.RespondToMessage((int)msg.WParam, (int)msg.LParam);
     break;
   }
   // Call the base WndProc method
   // to process any messages not handled.
   base.WndProc(ref msg);
  }
 }
}

Compiling the Code

This example requires references to the following namespaces:

See Also

Reference

MessageWindow
Message

Other Resources

Windows Forms Controls in the .NET Compact Framework