c# wince 下獲取當前輸入法狀態2

我的博客中有一篇介紹如何獲取當前輸入狀態,這裏主要介紹通過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";

        }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章