When we use the winform controls, you will find these controls (such as Label) does not support Ctrl + c to copy and Ctrl + v keyboard shortcuts to copy and paste function, if we need to implement this function to change how to do it?
1. First, we create a winform project.
We add three controls on the form in the project, two and a label textBox
We are going to achieve the function is to use Ctrl + C content can be displayed on the label Copy me the label "Copy me" copied to the clipboard, when we use Ctrl + V in the textbox controls which can Clipboard copy the string to the textbox control, and then we modify the string in which textBox use Ctrl + C to copy the string, and then select copy me label can use Ctrl + V to copy the value of the textbox to copy me this label inside, but check can not copy this label use Ctrl + C and Ctrl + V do not have any effect.
2. we add a new class HotKeysManager, this class uses DllImport features introduced user32.dll, and two inside the package and method RegisterHotKey UnregisterHotKey, code is as follows:
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace HotKey { public class HotKeysManager { // If the function succeeds, the return value is not zero. // If the function fails, the return value is zero. To get extended error information, call GetLastError. [The DllImport ( " User32.dll " , the SetLastError = to true )] public static extern BOOL RegisterHotKey ( HWnd IntPtr, // handle to define a hotkey window int the above mentioned id, // define a hotkey ID (can not be duplicated with other ID) KeyModifiers fsModifiers, // whether to identify when you press the hot key Alt, Ctrl, Shift, Windows and other key to take effect Keys VK // content-defined hotkey ); [DllImport("user32.dll", SetLastError = true)] public static extern bool UnregisterHotKey( HWnd IntPtr, // you want to cancel the hotkey window handle int the above mentioned id // To cancel a hotkey ID ); // defines the name of the secondary keys (character into a digital memory in order, may also be used directly remove this enumeration value) [the Flags ()] public enum KeyModifiers { None = 0, Alt = 1, Ctrl = 2, Shift = 4, WindowsKey = 8 } } }
3, we come in when the mouse enters Copy me in the Form registered hotkey Ctrl + C and Ctrl + V Copy me when the mouse leaves our cancellation hotkey Ctrl + C and Ctrl + V, and then reload the WndProc method to capture hotkey news If it is the Ctrl + C to copy values to the clipboard copy me inside, if the copy Ctrl + V to put the contents of the clipboard copy me label inside.
Look at the code:
/// Overload FromA in WndProc function /// monitor Windows message /// overloaded WndProc method for implementing a response hotkey protected override void WndProc(ref Message m) { const int WM_HOTKEY = 0x0312 ; // Ankuaichajian Switch (m.Msg) { case WM_HOTKEY: switch (m.WParam.ToInt32()) { case 100: Clipboard.SetText(this.labelTest.Text); break; case 101: this.labelTest.Text = Clipboard.GetText(); break; } break; } base.WndProc(ref m); } private void labelTest_MouseLeave(object sender, EventArgs e) { // cancellation Id No. 100 hotkey set HotKeysManager.UnregisterHotKey (the Handle, 100 ); // cancellation Id Number hotkey set to 101 HotKeysManager.UnregisterHotKey (the Handle, 101 ); } private void labelTest_MouseEnter(object sender, EventArgs e) { // Register hotkey Ctrl + C, Id number 100. . HotKeysManager.RegisterHotKey (the Handle, 100 , HotKeysManager.KeyModifiers.Ctrl, Keys.C); // Register hotkey Ctrl + V, Id No. 101. HotKeysManager.RegisterHotKey (the Handle, 101 , HotKeysManager.KeyModifiers.Ctrl, Keys.V); }
So we realized Ctrl + C and Ctrl + V to copy and paste the contents of the label functions.