如何:使用 MessageWindow 类
更新:2007 年 11 月
.NET Compact Framework 提供 MessageWindow 和 Message 类以生成和接收基于 Windows 的消息。MessageWindow 类使用本机代码以及窗体的句柄创建一个窗口,并执行所需的平台调用来调用本机 Windows 函数。MessageWindow 类仅在 .NET Compact Framework 中提供。
请使用消息窗口的窗口句柄 Hwnd 向消息窗口发送 Windows 消息。您可以使用托管代码中的 Create 或使用应用程序中的本机控件生成消息。您只能接收您生成的消息。不能使用 MessageWindow 监视操作系统消息。
当消息窗口检测到特定消息时,它将使用 WndProc 方法通知窗体,这样您就能提供代码来响应基于 Windows 的消息。
说明: |
---|
请注意,.NET Compact Framework 提供了将委托封送为函数指针,并从本机代码直接调用托管函数的功能,作为 MessageWindow 的替代方法。有关更多信息,请参见 .NET Compact Framework 中的互操作性。 |
示例
此示例演示 MessageWindow 的功能,但是没有使用本机组件进行演示。当鼠标指针处于自定义控件或 Panel 控件中的 Rectangle 内时,它向一个窗体发送包含当前指针的 x 和 y 坐标的 Windows 消息。自定义控件在体上显示为一个框。这两个控件都使用 OnMouseMove 方法发送消息。这些消息包含 x 坐标和 y 坐标。该窗体通过更新标签(使用当前 x 坐标和 y 坐标)和发送消息的控件来响应用户定义的 WM_BOXUPDATE 和 WM_PNLUPDATE 消息。
当鼠标位于框和面板之外时,窗体的 OnMouseMove 方法在窗体的上下文中使用 x 坐标和 y 坐标来更新标签。
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);
}
}
}
编译代码
此示例需要引用下面的命名空间: