我的博客中有一篇介绍如何获取当前输入状态,这里主要介绍通过RegisterWindowMessage("Keyboard_ICO")注册一个消息ID
代码经过测试是OK的
注册类:
class CustomMessageWindow : Microsoft.WindowsCE.Forms.MessageWindow
{
[DllImport("coredll.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern uint RegisterWindowMessage(string lpString);
public delegate void DelTextBoxMsg(string msg);
public event DelTextBoxMsg Msg;
/// <summary>
///
/// </summary>
/// <param name="m"></param>
protected override void WndProc(ref Microsoft.WindowsCE.Forms.Message m)
{
try
{
//MessageBox.Show("消息句柄:" + m.HWnd.ToString() + " id:" + m.Msg + " wparam:" + m.WParam + " LParam:" + m.LParam);
if (m.Msg == RegisterWindowMessage("Keyboard_ICO"))
{
switch (m.LParam.ToInt32())
{
case 40: //大写
Msg("40");
break;
case 41: //小写
Msg("41");
break;
case 42: //数字
Msg("42");
break;
case 43: //字符
Msg("43");
break;
default:
break;
}
}
}
catch
{ }
}
}
监控界面做的事情:
CustomMessageWindow customMessageWindow = new CustomMessageWindow();//实例化
public MainForm()
{
InitializeComponent();
customMessageWindow.Msg += new CustomMessageWindow.DelTextBoxMsg(customMessageWindow_Msg);//注册
}
void customMessageWindow_Msg(string msg)
{
if (msg == "40")
label1.Text = "A";
else if (msg == "41")
label1.Text = "a";
else if (msg == "42")
label1.Text = "9";
else if (msg == "43")
label1.Text = "#";
else
label1.Text = "a";
}