The WPF TextBox Control obtains the hotkey and converts it to win32 Keys and wpftextbox.

Source: Internet
Author: User
Tags modifiers

The WPF TextBox Control obtains the hotkey and converts it to win32 Keys and wpftextbox.

The Key object used in WPF is different from the Keys in WinForm. The Key enumeration objects of the two are different from those of the physical Key, and the type cannot be forcibly converted. When you use the win api to register a hot Key, you need to convert it to a win32 Key value. You can use KeyInterop. VirtualKeyFromKey (). In addition, Keys can save the combined Keys, while Keys are only a single Key. The Keys member has a Modifiers, which can be seen from 0 ~ It is used to store function keys except 15 characters. From the comparison of the two images, You can intuitively find the difference between the two.


Sample Code:

using System.Windows.Input;namespace demo.Controls{    class HotKeyTextBox : BeiLiNu.Ui.Controls.WPF.Controls.XTextBox    {        private System.Windows.Forms.Keys pressedKeys = System.Windows.Forms.Keys.None;        protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)        {            int keyValue = KeyInterop.VirtualKeyFromKey(e.Key);            if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)            {                keyValue += (int)System.Windows.Forms.Keys.Control;            }            if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)            {                keyValue += (int)System.Windows.Forms.Keys.Alt;            }            if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)            {                keyValue += (int)System.Windows.Forms.Keys.Shift;            }                        this.Keys = (System.Windows.Forms.Keys) keyValue;            e.Handled = true;                   }        public System.Windows.Forms.Keys Keys        {            get { return pressedKeys; }            set            {                pressedKeys = value;                setText(value);            }        }        private void setText(System.Windows.Forms.Keys keys)        {            this.Text = keys.ToString();        }    }}



WPF handles textbox style issues, click to get the focus background color change, when you click other places of the form (non-control), textbox automatically

The changes in the background color can be written in the IsFocused event.
After the input is complete, the textBox loses focus. You can receive the press ENTER event and the focus loss event in the textbox, and transfer the focus to other elements.
For example
Grid. IsFocusable = true;
Grid. GetFocus ();
Grid. IsFocusable = false;

Right-aligned Text attributes of TextBox controls in WPF

TextAlignment = "Left"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.