首先创建一个消息实例:
Message Msg = Message.Create(this.button1.Handle, 0x0100, (IntPtr) 0x21, (IntPtr)0);
第一个参数为窗口或控件的句柄;
第二个参数为windows消息ID(请查看 本人另一转帖——消息大全 http://blog.csdn.net/gulingeagle/archive/2010/02/04/5288973.aspx),上例中是 WM_KEYDOWN的16进制位值,表示 按下一个键;
第三个参数为消息 WParam 字段,通常是一个与消息有关的常量值,也可以是窗口或控件的句柄, 上例中是 PageUp键虚拟值的16进制位值,表示模拟按了PageUp键;
第四个参数为消息 LParam 字段,通常是一个指向内存中数据的指针, 上例中没有用到这个参数,因此赋零 ;
一个问题是,当确定了消息ID,怎么填写WParam和 LParam参数呢?刚开始我也找了很久,后来找到了一个办法:
在MSDN 官方网站 上 的英文 版 (中文版尚无这部分内容) :
http://msdn.microsoft.com/en-us/library/default.aspx
在搜索处填写消息ID号(如WM_KEYDOWN)进行搜索,之后在结果中找到:
标题为:“WM_KEYDOWN” 或 “WM_KEYDOWN Notification ()”的链接,里面即可找到WParam和 LParam参数值的说明,如 WM_KEYDOWN Notification链接里包含以下文字(不包含黑色文字):
-
wParam
- Specifies the virtual-key code of the nonsystem key.(甚至连 The Virtual-Key 都列在后面 ) lParam
-
Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table.
- Specifies the repeat count for the current message. The value is the number of times the keystroke is autorepeated as a result of the user holding down the key. If the keystroke is held long enough, multiple messages are sent. However, the repeat count is not cumulative.
- Specifies the scan code. The value depends on the OEM.
- Specifies whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0.
- Reserved; do not use.
- Specifies the context code. The value is always 0 for a WM_KEYDOWN message.
- Specifies the previous key state. The value is 1 if the key is down before the message is sent, or it is zero if the key is up.
- Specifies the transition state. The value is always zero for a WM_KEYDOWN message.
0-15
16-23
24
25-28
29
30
31
然后通过MessageWindow发送模拟消息:
MessageWindow.SendMessage(ref Msg);
注意若要使用以上类,必须在项目中添加对 Microsoft.WindowsCE.Forms 命名空间的引用。