c#设置输入框只能字符

        [DllImport("user32.dll")]
        public static extern bool PostMessage(int hhwnd, uint msg, IntPtr wparam, IntPtr lparam);

        [DllImport("user32.dll")]
        public static extern IntPtr LoadKeyboardLayout(string pwszKLID, uint Flags);

        public static uint WM_INPUTLANGCHANGEREQUEST = 0x0050;
        public static int HWND_BROADCAST = 0xffff;
        public static string en_US = "00000409"; //英文
        public static string cn_ZH = "00000804";
        public static uint KLF_ACTIVATE = 1;

        // 文本框获取焦点事件处理
        private void textBox1_Enter(object sender, EventArgs e)
        {
            PostMessage(HWND_BROADCAST, WM_INPUTLANGCHANGEREQUEST, IntPtr.Zero, LoadKeyboardLayout(en_US, KLF_ACTIVATE));
        }
        public InputLanguageChanger(UITextBox textBox)
        {
            textBox.Enter += textBox1_Enter;
        } 

 

posted @ 2024-06-26 17:27  未风  阅读(11)  评论(0编辑  收藏  举报