. NET Compact Framework provides MessageWindow and Message classes to generate and receive Windows-based messages. The MessageWindow class uses the local code and the form handle to create a window, and executes the required platform call to call the local Windows function. The MessageWindow class is only provided in. NET Compact Framework.
Use the Windows handle Hwnd in the message window to Send Windows messages to the message window. You can use Create in managed code or use the local control in the application to generate messages. You can only receive messages generated by you. You cannot use MessageWindow to monitor operating system messages.
When the message window detects a specific message, it uses the WndProc method to notify the form, so that you can provide code to respond to Windows-based messages.
This example demonstrates the MessageWindow function, but does not use the local component for demonstration. When the mouse pointer is in the Rectangle of a custom control or Panel control, it sends a Windows message containing the x and y coordinates of the current pointer to a form. The custom control is displayed as a box on the body. Both controls use the OnMouseMove method to send messages. These messages contain the x and y coordinates. This form responds to user-defined WM_BOXUPDATE and WM_PNLUPDATE messages by updating tags (using the current x and y coordinates) and sending message controls.
When the mouse is outside the frame and panel, the OnMouseMove method of the form uses the x and y coordinates in the context of the form to update the label.
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
// Windows messages and to modify y
// Form when they are already ed.
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 supported ed.
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 );
}
}
}
Compile code
The following namespace must be referenced in this example: