Design winform built-in dynamic loading tool button and implement hotkey response, winform hotkey

Source: Internet
Author: User

Design winform built-in dynamic loading tool button and implement hotkey response, winform hotkey
1. Original Intention

We mainly want to design a base form with the shortcut keys for adding tool buttons and buttons. In this way, as long as the designed form inherits from this form, it can implement the hotkey response and dynamic addition of tool buttons.

This article is mainly intended for reference in the future, because it is only a demo and will not be forgotten for a long time. At that time, you can read it and learn from it. Next, I will explain in detail how to design this form.

2. Design Steps

I. Create a winform project. By default, the form1 form is provided with a toolstrip tool control.

II. reference the Assembly rabbit. core. dll, which is my encapsulated hotkey response help class.

Click to download

III. the form1.cs file code is as follows: using Rabbit. core. hotKey; using Rabbit. UI; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. threading. tasks; using System. windows. forms; namespace HotKeyBindDemo {public partial class Form1: Form {// store all registered hotkeys Dictionary <int, ToolStripAndEvent> HotKeyDict = new Dictionary <Int, ToolStripAndEvent> (); public Form1 () {InitializeComponent ();} // when the form is activated, register the private void Form1_Activated (object sender, EventArgs e) {} // undo the hot key private void formincludeactivate (object sender, EventArgs e) {keyBindHelper. unregisterHotKey (this, 11); keyBindHelper. unregisterHotKey (this, 12 );} /// <summary> /// Add a button to the toolbar /// </summary> /// <param name = "Caption"> button title </param> // /<param na Me = "image"> pattern </param> /// <param name = "ClickEvent"> event to be pressed </param> /// <param name = "Hotkey"> shortcut key </param> public ToolStripButton AddComandButton (string Caption, image image, EventHandler ClickEvent, int Hotkey) {ToolStripButton AddButton = new ToolStripButton (Caption, image, ClickEvent); try {AddButton. imageTransparentColor = Color. magenta; AddButton. size = new System. drawing. size (56, 47); AddButto N. textImageRelation = TextImageRelation. imageAboveText; ToolStripAndEvent toolStripAndEvent = new ToolStripAndEvent () {BeClickEvent = ClickEvent, TargetButton = AddButton,}; CommandtoolStrip. items. add (AddButton); HotKeyDict. add (Hotkey, toolStripAndEvent);} catch (Exception) {} return AddButton;} // <summary> // a message mechanism of window, which will continuously execute this function, once you press the shortcut key, it will be captured // </summary> // <param name = "m"> </par Am> protected override void WndProc (ref Message m) {base. wndProc (ref m); if (m. msg = keyBindHelper. WM_HOTKEY) // once the shortcut key is pressed, the current message is corresponding to the hotkey, matched, enter if met internal {int id = m. WParam. toInt32 (); keyBindHelper. hotKeyOperation (HotKeyDict, id); // Click Event corresponding to the response id} private void Form1_Load (object sender, EventArgs e) {# region registers the hotkey, which is actually bound to F1 and 11, f2 and 12 bind keyBindHelper. registerHotKey (this, 11, keyBindHelper. keyModifiers. none, Keys. f1); // F1 is bound with 11 keyBindHelper. registerHotKey (this, 12, keyBindHelper. keyModifiers. none, Keys. f2); // F2 is bound to 12 # endregion # region. Click the Save button in the region toolbar to bind the event to 11, and print the AddComandButton ("Save (F1)", Rabbit. core. properties. resources. save, save_click, 11); // 11 is bound with save_click AddComandButton ("Print (F2)", Rabbit. core. properties. resources. print, Print_click, 12); // 12 is bound to Print_click # endregion // conclusion: in this case, when F1 is pressed, F1 will match the number 11, and then 11 will match. The save_click event is returned.} // the SAVE Function private void save_click (object sender, EventArgs e) {MessageBox. Show ("the SAVE Function is responded! ");} // Print function private void Print_click (object sender, EventArgs e) {MessageBox. Show (" the print function is responded! ") ;}// Close the form logout hotkey private void form=formclosed (object sender, FormClosedEventArgs e) {form=deactivate (null, null );}}}View Code

I'm going to take a look at it. I believe you can understand how to use it.


IV. The running effect is as follows:


V. Summary

Simply count the principle. To implement the hotkey response, you must first register the hotkey and then bind the event, that is, the hotkey-"id =" event, so that the message mechanism will capture such a process.


VI. About

You are welcome to leave a message. Myqq: 739462304

Related Article

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: 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.