C#如何:使用 MessageWindow 类来响应Windows的消息
2010-10-19 13:51
337 查看
.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_BOXUPDATE 和 WM_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);
}
}
}
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
编译代码
此示例需要引用下面的命名空间:
System
System.Windows.Forms
Microsoft.WindowsCE.Forms
请使用消息窗口的窗口句柄 Hwnd 向消息窗口发送 Windows 消息。您可以使用托管代码中的 Create 或使用应用程序中的本机控件生成消息。您只能接收您生成的消息。不能使用 MessageWindow 监视操作系统消息。
当消息窗口检测到特定消息时,它将使用 WndProc 方法通知窗体,这样您就能提供代码来响应基于 Windows 的消息。
此示例演示 MessageWindow 的功能,但是没有使用本机组件进行演示。当鼠标指针处于自定义控件或 Panel 控件中的 Rectangle 内时,它向一个窗体发送包含当前指针的 x 和 y 坐标的 Windows 消息。自定义控件在体上显示为一个框。这两个控件都使用 OnMouseMove 方法发送消息。这些消息包含 x 坐标和 y 坐标。该窗体通过更新标签(使用当前 x 坐标和 y 坐标)和发送消息的控件来响应用户定义的 WM_BOXUPDATE 和 WM_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);
}
}
}
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
编译代码
此示例需要引用下面的命名空间:
System
System.Windows.Forms
Microsoft.WindowsCE.Forms
相关文章推荐
- [Windows Mobile]C#如何:使用 MessageWindow 类来响应Windows的消息 [转]
- 使用 MessageWindow 类来响应Windows的消息
- 在vc中使用MapX时,如何在视图类中响应鼠标消息
- 异常信息:CLR无法从COM 上下文0x645e18 转换为COM上下文0x645f88,这种状态已持续60秒。拥有目标上下文/单元的线程很有可能执行的是非泵式等待或者在不发送 Windows 消息的情况下处理一个运行时间非常长的操作.这种情况通常会影响到性能,甚至可能导致应用程序不响应或者使用的内存随时间不断累积
- 在java程序中如何能获取到另一个程序的windows句柄,又如何使用这个句柄向这个窗口发送消息
- GUI程序中使用windows消息实现并行
- 如何让CloudStack使用KVM创建Windows实例成功识别并挂载数据盘 推荐
- Windows系统基于anocanda安装tensorflow以及如何在notebook,spyder等中使用tensorflow
- VC对话框如何添加WM_ERASEBKGND消息(OnEraseBkgnd函数)及对话框使用位图背景并透明
- qt捕获全局windows消息(使用QAbstractNativeEventFilter,然后注册这个类)
- 如何使用CMD Command 在Windows中查找和删除任何计算机病毒
- Windows下如何使用QT编写dll程序
- ack是什么,如何使用Ack机制,如何关闭Ack机制,基本实现,STORM的消息容错机制,Ack机制
- Windows平台下如何使用Android NDK
- 和Steve, Wade 一起学习如何使用Windows Azure Startup Tasks
- MFC中如何让一个CStatic控件响应消息
- 你是如何在苹果上使用windows系统的
- 如何在Windows上使用GIT下载Android源代码
- 在程序中如何使用自定义消息
- Windows下如何编译使用boost?