.NET Compact Framework 提供 MessageWindow 和 Message 类以生成和接收基于 Windows 的消息。MessageWindow 类使用本机代码以及窗体的句柄创建一个窗口,并执行所需的平台调用来调用本机 Windows 函数。MessageWindow 类仅在 .NET Compact Framework 中提供。

请使用消息窗口的窗口句柄 Hwnd 向消息窗口发送 Windows 消息。您可以使用托管代码中的 Create 或使用应用程序中的本机控件生成消息。您只能接收您生成的消息。不能使用 MessageWindow 监视操作系统消息。

当消息窗口检测到特定消息时,它将使用 WndProc 方法通知窗体,这样您就能提供代码来响应基于 Windows 的消息。

此示例演示 MessageWindow 的功能,但是没有使用本机组件进行演示。当鼠标指针处于自定义控件或 Panel 控件中的 Rectangle 内时,它向一个窗体发送包含当前指针的 x 和 y 坐标的 Windows 消息。自定义控件在体上显示为一个框。这两个控件都使用 OnMouseMove 方法发送消息。这些消息包含 x 坐标和 y 坐标。该窗体通过更新标签(使用当前 x 坐标和 y 坐标)和发送消息的控件来响应用户定义的 WM_BOXUPDATEWM_PNLUPDATE 消息。

当鼠标位于框和面板之外时,窗体的 OnMouseMove 方法在窗体的上下文中使用 x 坐标和 y 坐标来更新标签。

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

  • System.Windows.Forms

  • Microsoft.WindowsCE.Forms

posted on 2010-10-19 13:51  TsingCai  阅读(2254)  评论(0编辑  收藏  举报