HOW TO:使用 MessageWindow 類別

更新:2007 年 11 月

.NET Compact Framework 提供了 MessageWindowMessage 類別,以產生和接收 Windows 架構的訊息。MessageWindow 類別會以機器碼建立帶有表單控制代碼的視窗,並對原生 Windows 函式執行必要的平台叫用 (Invoke) 呼叫。MessageWindow 類別只能在 .NET Compact Framework 中使用。

使用訊息視窗的視窗控制代碼 (Window Handle) Hwnd,將 Windows 訊息傳送至訊息視窗。可以透過在 Managed 程式碼中使用 Create,或在應用程式中使用原生控制項,來產生訊息。只可以接收您所產生的訊息。但不可以使用 MessageWindow 來監視作業系統訊息。

當訊息視窗偵測到特定訊息時,會使用 WndProc 方法來告知表單,讓您能夠提供程式碼以回應 Windows 架構的訊息。

注意事項:

請注意,.NET Compact Framework 版提供了將委派封送處理 (Marshal) 為函式指標,以及從機器碼直接呼叫 Managed 函式的能力,以做為 MessageWindow 的替代方法。如需詳細資訊,請參閱 .NET Compact Framework 中的互通性

範例

這個範例會示範 MessageWindow 的功能,但不使用原生元件。當滑鼠指標位於自訂控制項中的 RectanglePanel 控制項中時,它會將 Windows 訊息 (包含目前指標的 x 座標和 y 座標) 傳送至表單。自訂控制項會顯示為表單上的方塊。這兩個控制項都會使用 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);
  }
 }
}

編譯程式碼

這個範例需要下列命名空間的參考:

請參閱

參考

MessageWindow

Message

其他資源

.NET Compact Framework 中的 Windows Form 控制項