MessageWindow 類別
提供傳送和接收 Windows 架構訊息的能力。
命名空間: Microsoft.WindowsCE.Forms
組件: Microsoft.WindowsCE.Forms (在 Microsoft.WindowsCE.Forms.dll 中)
語法
'宣告
Public Class MessageWindow _
Implements IDisposable
'用途
Dim instance As MessageWindow
public class MessageWindow : IDisposable
public ref class MessageWindow : IDisposable
public class MessageWindow implements IDisposable
備註
MessageWindow 類別提供傳送和收送 Windows 訊息的能力。它會以機器碼建立視窗控制代碼,並執行必要的平台叫用,呼叫 Windows 原生函式。
若要在程式中使用 MessageWindow,您必須建立從 MessageWindow 衍生而來的類別,並覆寫預設的 WndProc 行為,以監看是否有特定的視窗訊息。您可以利用 Message 類別來產生訊息。您只能收到以 MessageWindow 或原生控制項所產生的 Windows 訊息。
如果要使用這個類別,您必須在 Visual Studio 2005 專案中新增 Microsoft.WindowsCE.Forms 命名空間的參考。
Topic | Location |
---|---|
HOW TO:使用 MessageWindow 類別 | .NET Compact Framework |
HOW TO:使用 MessageWindow 類別 | .NET Compact Framework |
HOW TO:使用 MessageWindow 類別 | .NET Compact Framework |
範例
下面程式碼範例示範 MessageWindow,所透過的方式是讓表單將目前滑鼠 x-y 座標的 Windows 訊息傳送到訊息視窗,此視窗會叫用表單上的回呼方法,以在標題列上顯示座標。
這個表單包含自訂類別 MsgWindow (從 MessageWindow 衍生而來)。MsgWindow 類別會檢查被覆寫的 WndProc 方法中的訊息,以找出有 WM_CUSTOMMSG 識別項的訊息。當此類別尋找這些訊息時,它會叫用表單中所定義的 RespondToMessage 回呼方法。
此表單會建立新的 MsgWindow 執行個體。MsgWindow 建構函式會接受表單,在此範例中即為包含表單。此表單會在覆寫的 OnMouseMove 方法中,產生 Windows 架構訊息。
當此表單執行時,移動滑鼠會產生訊息至訊息視窗。訊息視窗 WndProc 方法會叫用表單上的回呼方法,以對訊息做出回應。
請注意,您必須將對 Microsoft.WindowsCE.Forms 的參考加入至專案。
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);
}
}
}
繼承階層架構
System.Object
Microsoft.WindowsCE.Forms.MessageWindow
執行緒安全
這個型別的任何 Public static (在 Visual Basic 中為 Shared) 成員都具備執行緒安全。並非所有的執行個體成員都是安全執行緒。
平台
Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
.NET Framework 和 .NET Compact Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱 .NET Framework 系統需求。
版本資訊
.NET Compact Framework
支援版本:3.5、2.0、1.0