国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

在Winform和WPF中注冊全局快捷鍵實現思路及代碼

2020-01-24 03:35:29
字體:
來源:轉載
供稿:網友

快捷鍵輔助類

復制代碼 代碼如下:

class HotKey
{
/// <summary>
/// 如果函數執行成功,返回值不為0。
/// 如果函數執行失敗,返回值為0。要得到擴展錯誤信息,調用GetLastError。.NET方法:Marshal.GetLastWin32Error()
/// </summary>
/// <param name="hWnd">要定義熱鍵的窗口的句柄</param>
/// <param name="id">定義熱鍵ID(不能與其它ID重復) </param>
/// <param name="fsModifiers">標識熱鍵是否在按Alt、Ctrl、Shift、Windows等鍵時才會生效</param>
/// <param name="vk">定義熱鍵的內容,WinForm中可以使用Keys枚舉轉換,
/// WPF中Key枚舉是不正確的,應該使用System.Windows.Forms.Keys枚舉,或者自定義正確的枚舉或int常量</param>
/// <returns></returns>
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd,
int id,
KeyModifiers fsModifiers,
int vk
);
/// <summary>
/// 取消注冊熱鍵
/// </summary>
/// <param name="hWnd">要取消熱鍵的窗口的句柄</param>
/// <param name="id">要取消熱鍵的ID</param>
/// <returns></returns>
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd,
int id
);
/// <summary>
/// 向全局原子表添加一個字符串,并返回這個字符串的唯一標識符,成功則返回值為新創建的原子ID,失敗返回0
/// </summary>
/// <param name="lpString"></param>
/// <returns></returns>
[DllImport("kernel32", SetLastError = true)]
public static extern short GlobalAddAtom(string lpString);
[DllImport("kernel32", SetLastError = true)]
public static extern short GlobalDeleteAtom(short nAtom);
/// <summary>
/// 定義了輔助鍵的名稱(將數字轉變為字符以便于記憶,也可去除此枚舉而直接使用數值)
/// </summary>
[Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Ctrl = 2,
Shift = 4,
WindowsKey = 8
}
/// <summary>
/// 熱鍵的對應的消息ID
/// </summary>
public const int WM_HOTKEY = 0x312;
}

WinForm方法
在中在onload中注冊事件,然后重載WndProc,對返回的消息進行處理
復制代碼 代碼如下:

int alts,altd;
protected override void OnLoad(EventArgs e)
{
alts= HotKey.GlobalAddAtom("Alt-S");
altd= HotKey.GlobalAddAtom("Alt-D");
HotKey.RegisterHotKey(this.Handle, alts, HotKey.KeyModifiers.Alt, (int)Keys.S);
HotKey.RegisterHotKey(this.Handle, altd, HotKey.KeyModifiers.Alt, (int)Keys.D);
}
protected override void WndProc(ref Message m)// 監視Windows消息
{
switch (m.Msg)
{
case HotKey.WM_HOTKEY:
ProcessHotkey(m);//按下熱鍵時調用ProcessHotkey()函數
break;
}
base.WndProc(ref m); //將系統消息傳遞自父類的WndProc
}
private void ProcessHotkey(Message m) //按下設定的鍵時調用該函數
{
IntPtr id = m.WParam;//IntPtr用于表示指針或句柄的平臺特定類型
int sid=id.ToInt32();
if(sid==alts)
{
MessageBox.Show("按下Alt+S");
}
else if(sid==altd)
{
MessageBox.Show("按下Alt+D");
}
}

也可以在Application使用AddMessageFilter添加處理來代替重載WndProc,可以使form自身實現IMessageFilter接口,
注冊方法
復制代碼 代碼如下:

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var form = new Form1();
Application.AddMessageFilter(form);
Application.Run(form);
}

實現接口
復制代碼 代碼如下:

bool IMessageFilter.PreFilterMessage(ref Message m)
{
const int WM_HOTKEY = 0x0312;//如果m.Msg的值為0x0312那么表示用戶按下了熱鍵
switch (m.Msg)
{
case WM_HOTKEY:
ProcessHotkey(m);//按下熱鍵時調用ProcessHotkey()函數
break;
}
//如果篩選消息并禁止消息被調度,則為 true;如果允許消息繼續到達下一個篩選器或控件,則為 false
return false ;
}

如果同時在AddMessageFilter和WndProc處理消息,順序是先IMessageFilter,然后調用WndProc
WPF方法
WPF中的注冊方法和Winform一樣,只是WPF的Key枚舉的值不能正確監聽,要引用System.Windows.Forms.Keys或者自己定義才可以正確注冊,窗口句柄也需要借助WindowInteropHelper來得到,處理函數的加入也和Winform不同,需要HwndSource來添加處理函數。
注冊及處理方法
復制代碼 代碼如下:

int alts, altd;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
HwndSource hWndSource;
WindowInteropHelper wih = new WindowInteropHelper(this);
hWndSource = HwndSource.FromHwnd(wih.Handle);
//添加處理程序
hWndSource.AddHook(MainWindowProc);
alts = HotKey.GlobalAddAtom("Alt-S");
altd = HotKey.GlobalAddAtom("Alt-D");
HotKey.RegisterHotKey(wih.Handle, alts, HotKey.KeyModifiers.Alt, (int)System.Windows.Forms.Keys.S);
HotKey.RegisterHotKey(wih.Handle, altd, HotKey.KeyModifiers.Alt, (int)System.Windows.Forms.Keys.D);
}
private IntPtr MainWindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
switch (msg)
{
case HotKey.WM_HOTKEY:
{
int sid = wParam.ToInt32();
if (sid == alts)
{
MessageBox.Show("按下Alt+S");
}
else if (sid == altd)
{
MessageBox.Show("按下Alt+D");
}
handled = true;
break;
}
}
return IntPtr.Zero;
}

注意
如果注冊快捷鍵,RegisterHotKey中的fsModifiers參數為0,即None選項,一些安全軟件會警報,可能因為這樣就可以全局監聽鍵盤而造成安全問題
代碼下載

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 杭州市| 新巴尔虎右旗| 田东县| 乌兰浩特市| 分宜县| 汪清县| 科技| 个旧市| 怀安县| 合江县| 乌拉特前旗| 报价| 金湖县| 新龙县| 乐东| 灵寿县| 嫩江县| 左云县| 永善县| 柳江县| 镇远县| 大邑县| 罗田县| 海门市| 宁城县| 大理市| 广灵县| 应城市| 宁陕县| 平阴县| 南充市| 连山| 洪洞县| 万宁市| 海丰县| 福州市| 安丘市| 都安| 清河县| 南阳市| 都匀市|