C#設置快捷鍵
要設置快捷鍵必須使用user32.dll下面的兩個方法。
BOOL RegisterHotKey( //註冊系統熱鍵的API函數
HWND hWnd,
int id,
UINT fsModifiers,
UINT vk
);
BOOL UnregisterHotKey( //刪除系統熱鍵的API函數
HWND hWnd,
int id
);
在C#中引用命名空間System.Runtime.InteropServices;來加載非託管類user32.dll
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace HotKey
{
public enum KeyModifiers //組合鍵枚舉
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
public partial class Form1 : Form
{
/*
* RegisterHotKey函數原型及說明:
* BOOL RegisterHotKey(
* HWND hWnd, // window to receive hot-key notification
* int id, // identifier of hot key
* UINT fsModifiers, // key-modifier flags
* UINT vk // virtual-key code);
* 參數 id爲你自己定義的一個ID值
* 對一個線程來講其值必需在0x0000 - 0xBFFF範圍之內,十進制爲0~49151
* 對DLL來講其值必需在0xC000 - 0xFFFF 範圍之內,十進制爲49152~65535
* 在同一進程內該值必須唯一參數 fsModifiers指明與熱鍵聯合使用按鍵
* 可取值爲:MOD_ALT MOD_CONTROL MOD_WIN MOD_SHIFT參數,或數字0爲無,1爲Alt,2爲Control,4爲Shift,8爲Windows
* vk指明熱鍵的虛擬鍵碼
*/
[System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函數
public static extern bool RegisterHotKey(
IntPtr hWnd, // handle to window
int id, // hot key identifier
uint fsModifiers, // key-modifier options
Keys vk // virtual-key code
);
[System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函數
public static extern bool UnregisterHotKey(
IntPtr hWnd, // handle to window
int id // hot key identifier
);
public Form1()
{
InitializeComponent();
}
private void ProcessHotkey(Message m) //按下設定的鍵時調用該函數
{
IntPtr id = m.WParam; //IntPtr用於表示指針或句柄的平臺特定類型
//MessageBox.Show(id.ToString());
string sid = id.ToString();
switch (sid)
{
case "100":
MessageBox.Show("調用A函數");
break;
case "200":
MessageBox.Show("調用B函數");
break;
}
}
private void Form1_Load(object sender, EventArgs e)
{
//Handle爲當前窗口的句柄,繼續自Control.Handle,Control爲定義控件的基類
//RegisterHotKey(Handle, 100, 0, Keys.A); //註冊快捷鍵,熱鍵爲A
//RegisterHotKey(Handle, 100, KeyModifiers.Alt | KeyModifiers.Control, Keys.B);//這時熱鍵爲Alt+CTRL+B
//RegisterHotKey(Handle, 100, 1, Keys.B); //1爲Alt鍵,熱鍵爲Alt+B
RegisterHotKey(Handle, 100, 2,Keys.A); //定義熱鍵爲Alt+Tab,這裏實現了屏幕系統Alt+Tab鍵
RegisterHotKey(Handle, 200, 2, Keys.B); //註冊2個熱鍵,根據id值100,200來判斷需要執行哪個函數
}
private void button1_Click(object sender, EventArgs e) //重新設置熱鍵
{
UnregisterHotKey(Handle, 100);//卸載快捷鍵
RegisterHotKey(Handle, 100, 2, Keys.C); //註冊新的快捷鍵,參數0表示無組合鍵
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e) //退出程序時缷載熱鍵
{
UnregisterHotKey(Handle, 100);//卸載第1個快捷鍵
UnregisterHotKey(Handle, 200); //缷載第2個快捷鍵
}
//重寫WndProc()方法,通過監視系統消息,來調用過程
protected override void WndProc(ref Message m)//監視Windows消息
{
const int WM_HOTKEY = 0x0312;//如果m.Msg的值爲0x0312那麼表示用戶按下了熱鍵
switch (m.Msg)
{
case WM_HOTKEY:
ProcessHotkey(m);//按下熱鍵時調用ProcessHotkey()函數
break;
}
base.WndProc(ref m); //將系統消息傳遞自父類的WndProc
}
}
BOOL RegisterHotKey( //註冊系統熱鍵的API函數
HWND hWnd,
int id,
UINT fsModifiers,
UINT vk
);
BOOL UnregisterHotKey( //刪除系統熱鍵的API函數
HWND hWnd,
int id
);
在C#中引用命名空間System.Runtime.InteropServices;來加載非託管類user32.dll
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace HotKey
{
public enum KeyModifiers //組合鍵枚舉
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
public partial class Form1 : Form
{
/*
* RegisterHotKey函數原型及說明:
* BOOL RegisterHotKey(
* HWND hWnd, // window to receive hot-key notification
* int id, // identifier of hot key
* UINT fsModifiers, // key-modifier flags
* UINT vk // virtual-key code);
* 參數 id爲你自己定義的一個ID值
* 對一個線程來講其值必需在0x0000 - 0xBFFF範圍之內,十進制爲0~49151
* 對DLL來講其值必需在0xC000 - 0xFFFF 範圍之內,十進制爲49152~65535
* 在同一進程內該值必須唯一參數 fsModifiers指明與熱鍵聯合使用按鍵
* 可取值爲:MOD_ALT MOD_CONTROL MOD_WIN MOD_SHIFT參數,或數字0爲無,1爲Alt,2爲Control,4爲Shift,8爲Windows
* vk指明熱鍵的虛擬鍵碼
*/
[System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函數
public static extern bool RegisterHotKey(
IntPtr hWnd, // handle to window
int id, // hot key identifier
uint fsModifiers, // key-modifier options
Keys vk // virtual-key code
);
[System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函數
public static extern bool UnregisterHotKey(
IntPtr hWnd, // handle to window
int id // hot key identifier
);
public Form1()
{
InitializeComponent();
}
private void ProcessHotkey(Message m) //按下設定的鍵時調用該函數
{
IntPtr id = m.WParam; //IntPtr用於表示指針或句柄的平臺特定類型
//MessageBox.Show(id.ToString());
string sid = id.ToString();
switch (sid)
{
case "100":
MessageBox.Show("調用A函數");
break;
case "200":
MessageBox.Show("調用B函數");
break;
}
}
private void Form1_Load(object sender, EventArgs e)
{
//Handle爲當前窗口的句柄,繼續自Control.Handle,Control爲定義控件的基類
//RegisterHotKey(Handle, 100, 0, Keys.A); //註冊快捷鍵,熱鍵爲A
//RegisterHotKey(Handle, 100, KeyModifiers.Alt | KeyModifiers.Control, Keys.B);//這時熱鍵爲Alt+CTRL+B
//RegisterHotKey(Handle, 100, 1, Keys.B); //1爲Alt鍵,熱鍵爲Alt+B
RegisterHotKey(Handle, 100, 2,Keys.A); //定義熱鍵爲Alt+Tab,這裏實現了屏幕系統Alt+Tab鍵
RegisterHotKey(Handle, 200, 2, Keys.B); //註冊2個熱鍵,根據id值100,200來判斷需要執行哪個函數
}
private void button1_Click(object sender, EventArgs e) //重新設置熱鍵
{
UnregisterHotKey(Handle, 100);//卸載快捷鍵
RegisterHotKey(Handle, 100, 2, Keys.C); //註冊新的快捷鍵,參數0表示無組合鍵
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e) //退出程序時缷載熱鍵
{
UnregisterHotKey(Handle, 100);//卸載第1個快捷鍵
UnregisterHotKey(Handle, 200); //缷載第2個快捷鍵
}
//重寫WndProc()方法,通過監視系統消息,來調用過程
protected override void WndProc(ref Message m)//監視Windows消息
{
const int WM_HOTKEY = 0x0312;//如果m.Msg的值爲0x0312那麼表示用戶按下了熱鍵
switch (m.Msg)
{
case WM_HOTKEY:
ProcessHotkey(m);//按下熱鍵時調用ProcessHotkey()函數
break;
}
base.WndProc(ref m); //將系統消息傳遞自父類的WndProc
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.