本文實例講述了C#全局熱鍵設置與窗體熱鍵設置,分享給大家供大家參考。具體實現方法如下:
1、窗體熱鍵
首先要設置主窗體KeyPreview為true,可直接在屬性中進行設置,
或者在窗體加載中設置: this.KeyPreview = true;
然后添加窗體KeyDown事件,如下:
2、全局熱鍵設置
定義API函數 》 注冊熱鍵 》 卸載熱鍵
我這里定義了AppHotKey類,全部代碼如下:
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd, //要取消熱鍵的窗口的句柄
int id //要取消熱鍵的ID
);
//定義了輔助鍵的名稱(將數字轉變為字符以便于記憶,也可去除此枚舉而直接使用數值)
[Flags()]
public enum KeyModifiers
{
None = 0,
style="border-left-color: rgb(0, 153, 204); border-left-width: 1px; border-left-style: solid; padding: 0px 3px; margin: 3px auto 0px; width: 640px; background-color: rgb(242, 246, 251); clear: both; border-top-color: rgb(0, 153, 204); border-top-width: 1px; border-top-style: solid; border-right-color: rgb(0, 153, 204); border-right-width: 1px; border-right-style: solid;"> 復制代碼代碼如下:
希望本文所述對大家的C#程序設計有所幫助。
新聞熱點
疑難解答