c# sendmessage 发送键盘按键详解

废话不说,直接说demo

1.先引用

using System.Runtime.InteropServices;

2.导入sendmessage函数入口

 [DllImport("user32.dll", EntryPoint = "SendMessageA")]

        private static extern int SendMessage(

           IntPtr hWnd,   // handle to destination window

           int Msg,    // message

           uint wParam, // first message parameter

           uint lParam // second message parameter

     );

里面的参数类型可以自己改改,以适应c#的类型

3.调用 SendMessage(textBox_BarCode.textBox_edit.Handle, 258, 13, 0);

解释:

参数1:接受句柄

参数2:258=wm_char 具体意思:就是Const WM_CHAR = 0x0102,安下某键,并且是已经发送wm_keydown和wm_keyup消息

参数3:13:表示键的值,回车键就是13,因此这里发送了个回车键

参数4:不解释了,组合键的情况令谈
 

猜你喜欢

转载自blog.csdn.net/zhongheijituan/article/details/39232267