這篇文章主要給大家介紹C# winform快捷鍵設置技巧,涉及到C winform快捷鍵相關知識,對C winform知識感興趣的朋友可以參考下本篇文章
1、Alt+*(按鈕快捷鍵)
按鈕快捷鍵也為最常用快捷鍵,其設置也故為簡單。在大家給button、label、menuStrip等其他控件的Text屬性指定名稱時,在其后面加上‘&'然后在加上一個指定字母即可。如:確定(&D),(Alt+D)調用。
如指定多個字母,則第一個為快捷鍵。如:確定(&OK),(Alt+O)調用;文件(&Fill),(Alt+F)調用。
2、Ctrl+*及其他組合鍵
把 Form 的 KeyPreview 屬性設為 True
使用Modifiers可設置組合鍵,鍵盤數字區按鍵的Keys枚舉以D打頭,而小鍵盤上的數字以NumPad打頭。按下Ctrl與Shift組合鍵的方法與其類似,將Ctrl和Alt的枚舉轉換為int型相加后與Modifiers對比,這樣即可判斷是否按下了該組合鍵。
- private void frmMain_KeyDown(object sender, KeyEventArgs e)
- {
- //比如你的窗體名是frmMain,確定按鈕btnOK,保存按鈕btnSave
- //單鍵
- switch (e.KeyCode)
- {
- case Keys.F1:
- btnOK_Click(this, EventArgs.Empty);
- break;
- case Keys.F2:
- btnSave_Click(this, EventArgs.Empty);
- break;
- }
- // 組合鍵
- if (e.KeyCode == Keys.F1 && e.Modifiers == Keys.Control) //Ctrl+F1
- {
- btnShouYi_Click(this, EventArgs.Empty);
- }
- if ((int)e.Modifiers == ((int)Keys.Control + (int)Keys.Alt) && e.KeyCode == Keys.D0) //Ctrl + Alt + 數字0
- {
- MessageBox.Show("按下了Control + Alt + 0");
- }
另外的,與窗體的AcceptButton屬性相關聯的按鈕,將與鍵盤上的Enter鍵對應;與窗體的CancelButton屬性相關聯的按鈕,將與鍵盤上的Ecs鍵對應。
}
======================================================
鍵 代碼
BACKSPACE {BACKSPACE}、{BS} 或 {BKSP}
BREAK {BREAK}
CAPS LOCK {CAPSLOCK}
DEL 或 DELETE {DELETE} 或 {DEL}
DOWN ARROW(下箭頭鍵) {DOWN}
END {END}
ENTER {ENTER} 或 ~
ESC {ESC}
HELP {HELP}
HOME {HOME}
INS 或 INSERT {INSERT} 或 {INS}
LEFT ARROW(左箭頭鍵) {LEFT}
NUM LOCK {NUMLOCK}
PAGE DOWN {PGDN}
PAGE UP {PGUP}
PRINT SCREEN {PRTSC}(保留供將來使用)
RIGHT ARROW(右箭頭鍵) {RIGHT}
SCROLL LOCK {SCROLLLOCK}
TAB {TAB}
UP ARROW(上箭頭鍵) {UP}
F1 {F1}
F2 {F2}
F3 {F3}
F4 {F4}
F5 {F5}
F6 {F6}
F7 {F7}
F8 {F8}
F9 {F9}
F10 {F10}
F11 {F11}
F12 {F12}
F13 {F13}
F14 {F14}
F15 {F15}
F16 {F16}
數字鍵盤加號 {ADD}
數字鍵盤減號 {SUBTRACT}
數字鍵盤乘號 {MULTIPLY}
數字鍵盤除號 {DIVIDE}
**********************************************************************篇3**************************************************************************************************
#region 快捷鍵相關
///
/// 記錄快捷鍵
///
- private void txtHotKey_KeyDown(object sender, KeyEventArgs e)
- {
- int HotKeyValue = 0;
- string HotKeyString = "";
- e.SuppressKeyPress = false;
- e.Handled = true;
- if (e.Modifiers != Keys.None)
- {
- switch (e.Modifiers)
- {
- case Keys.Control:
- HotKeyString += "Ctrl + ";
- HotKeyValue = (int)e.Modifiers;
- break;
- case Keys.Alt:
- HotKeyString += "Alt + ";
- HotKeyValue = (int)e.Modifiers;
- break;
- case Keys.Shift:
- HotKeyString += "Shift + ";
- HotKeyValue = (int)e.Modifiers;
- break;
- case Keys.Control | Keys.Alt:
- HotKeyString += "Ctrl + Alt + ";
- HotKeyValue = (int)e.Modifiers;
- break;
- case Keys.Control | Keys.Shift:
- HotKeyString += "Ctrl + Shift + ";
- HotKeyValue = (int)e.Modifiers;
- break;
- case Keys.Alt | Keys.Shift:
- HotKeyString += "Alt + Shift + ";
- HotKeyValue = (int)e.Modifiers;
- break;
- case Keys.Control | Keys.Alt | Keys.Shift:
- HotKeyString += "Ctrl + Alt + Shift + ";
- HotKeyValue = (int)e.Modifiers;
- break;
- }
- if (e.KeyCode != Keys.None && e.KeyCode != Keys.ControlKey && e.KeyCode != Keys.Menu && e.KeyCode != Keys.ShiftKey)
- {
- HotKeyString += KeyCodeToString(e.KeyCode);
- HotKeyValue += (int)e.KeyCode;
- }
- }
- else
- {
- if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
- {
- HotKeyString = "無";
- HotKeyValue = -1;
- }
- else if (e.KeyCode != Keys.None)
- {
- HotKeyString = KeyCodeToString(e.KeyCode);
- HotKeyValue = (int)e.KeyCode;
- }
- }
- if (HotKeyValue == 0)
- HotKeyValue = -1;
- TextBox txtHotKey = (TextBox)sender;
- txtHotKey.Text = HotKeyString;
- txtHotKey.Tag = HotKeyValue;
- txtHotKey.SelectionStart = txtHotKey.Text.Length;
- }
- ///
- /// 將按鍵轉換成相應字符
- ///
- /// 按鍵
- /// 字符
- private string KeyCodeToString(Keys KeyCode)
- {
- if (KeyCode >= Keys.D0 && KeyCode <= Keys.D9)
- {
- return KeyCode.ToString().Remove(0, 1);
- }
- else if (KeyCode >= Keys.NumPad0 && KeyCode <= Keys.NumPad9)
- {
- return KeyCode.ToString().Replace("Pad", "");
- }
- else
- {
- return KeyCode.ToString();
- }
- }
- ///
- /// 設置按鍵不響應
- ///
- private void txtHotKey_KeyPress(object sender, KeyPressEventArgs e)
- {
- e.Handled = true;
- }
- ///
- /// 釋放按鍵后,若是無實際功能鍵,則置無
- ///
- private void txtHotKey_KeyUp(object sender, KeyEventArgs e)
- {
- CheckHotkey(sender);
- }
- ///
- /// 失去焦點后,若是無實際功能鍵,則置無
- ///
- private void txtHotKey_LostFocus(object sender, EventArgs e)
- {
- CheckHotkey(sender);
- }
- ///
- /// 檢查是否無實際功能鍵,是則置無
- ///
- private void CheckHotkey(object sender)
- {
- TextBox txtHotKey = (TextBox)sender;
- if (txtHotKey.Text.EndsWith(" + ") || String.IsNullOrEmpty(txtHotKey.Text))
- {
- txtHotKey.Text = "無";
- txtHotKey.Tag = -1;
- txtHotKey.SelectionStart = txtHotKey.Text.Length;
- }
- }
#endregion
#實現快捷鍵(系統熱鍵)響應
在應用中,我們可能會需要實現像Ctrl+C復制、Ctrl+V粘貼這樣的快捷鍵,本文簡單介紹了它的實現,并給出了一個實現類。
(1)建立一個類文件,命名為HotKey.cs,代碼如下:
- using System;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- namespace KoalaStudio.BookshopManager
- {
- class HotKey
- {
- //如果函數執行成功,返回值不為0。
- //如果函數執行失敗,返回值為0。要得到擴展錯誤信息,調用GetLastError。
- [DllImport("user32.dll", SetLastError = true)]
- public static extern bool RegisterHotKey(
- IntPtr hWnd, //要定義熱鍵的窗口的句柄
- int id, //定義熱鍵ID(不能與其它ID重復)
- KeyModifiers fsModifiers, //標識熱鍵是否在按Alt、Ctrl、Shift、Windows等鍵時才會生效
- Keys vk //定義熱鍵的內容
- );
- [DllImport("user32.dll", SetLastError = true)]
- public static extern bool UnregisterHotKey(
- IntPtr hWnd, //要取消熱鍵的窗口的句柄
- int id //要取消熱鍵的ID
- );
- //定義了輔助鍵的名稱(將數字轉變為字符以便于記憶,也可去除此枚舉而直接使用數值)
- [Flags()]
- public enum KeyModifiers
- {
- None = 0,
- Alt = 1,
- Ctrl = 2,
- Shift = 4,
- WindowsKey = 8
- }
- }
- }
簡單說明一下:
“public static extern bool RegisterHotKey()”這個函數用于注冊熱鍵。由于這個函數需要引用user32.dll動態鏈接庫后才能使用,并且
user32.dll是非托管代碼,不能用命名空間的方式直接引用,所以需要用“DllImport”進行引入后才能使用。于是在函數前面需要加上
“[DllImport("user32.dll", SetLastError = true)]”這行語句。
“public static extern bool UnregisterHotKey()”這個函數用于注銷熱鍵,同理也需要用DllImport引用user32.dll后才能使用。
“public enum KeyModifiers{}”定義了一組枚舉,將輔助鍵的數字代碼直接表示為文字,以方便使用。這樣在調用時我們不必記住每一個輔
助鍵的代碼而只需直接選擇其名稱即可。
(2)以窗體FormA為例,介紹HotKey類的使用
在FormA的Activate事件中注冊熱鍵,本例中注冊Shift+S,Ctrl+Z,Alt+D這三個熱鍵。這里的Id號可任意設置,但要保證不被重復。
- private void Form_Activated(object sender, EventArgs e)
- {
- //注冊熱鍵Shift+S,Id號為100。HotKey.KeyModifiers.Shift也可以直接使用數字4來表示。
- HotKey.RegisterHotKey(Handle, 100, HotKey.KeyModifiers.Shift, Keys.S);
- //注冊熱鍵Ctrl+B,Id號為101。HotKey.KeyModifiers.Ctrl也可以直接使用數字2來表示。
- HotKey.RegisterHotKey(Handle, 101, HotKey.KeyModifiers.Ctrl, Keys.B);
- //注冊熱鍵Alt+D,Id號為102。HotKey.KeyModifiers.Alt也可以直接使用數字1來表示。
- HotKey.RegisterHotKey(Handle, 102, HotKey.KeyModifiers.Alt, Keys.D);
- }
在FormA的Leave事件中注銷熱鍵。
- private void FrmSale_Leave(object sender, EventArgs e)
- {
- //注銷Id號為100的熱鍵設定
- HotKey.UnregisterHotKey(Handle, 100);
- //注銷Id號為101的熱鍵設定
- HotKey.UnregisterHotKey(Handle, 101);// http://ike.126.com
- //注銷Id號為102的熱鍵設定
- HotKey.UnregisterHotKey(Handle, 102);
- }
重載FromA中的WndProc函數
- ///
- /// 監視Windows消息
- /// 重載WndProc方法,用于實現熱鍵響應
- ///
- ///
- protected override void WndProc(ref Message m)
- {
- const int WM_HOTKEY = 0x0312;
- //按快捷鍵
- switch (m.Msg)
- {
- case WM_HOTKEY:
- switch (m.WParam.ToInt32())
- {
- case 100: //按下的是Shift+S
- //此處填寫快捷鍵響應代碼
- break;
- case 101: //按下的是Ctrl+B
- //此處填寫快捷鍵響應代碼
- break;
- case 102: //按下的是Alt+D
- //此處填寫快捷鍵響應代碼
- break;
- }
- break;
- }
- base.WndProc(ref m);
- }
完成代碼后,我們在窗體中按下Shift+S、Ctrl+B、Alt+D這三組快捷鍵中的任意一組時,程序都會做出響應的反應。
以上內容是小編給大家介紹的C# WinForm快捷鍵設置技巧,希望大家喜歡。
新聞熱點
疑難解答