Last active
August 29, 2015 14:00
-
-
Save zerda/11101656 to your computer and use it in GitHub Desktop.
如何使用全局快捷键
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainWindow { | |
private short atom; | |
private HwndSource hwndSource; | |
public MainWindow { | |
this.Loaded += (sender, args) => { | |
// 挂载全局快捷键 | |
var helper = new WindowInteropHelper(this); | |
hwndSource = HwndSource.FromHwnd(helper.Handle); | |
if(hwndSource != null) | |
hwndSource.AddHook(MainWindowProc); | |
atom = NativeMethods.GlobalAddAtom("SybaseExplorer"); | |
if(atom == 0) | |
throw new Win32Exception(Marshal.GetLastWin32Error()); | |
//TODO: 需支持自定义快捷键 | |
if(!NativeMethods.RegisterHotKey(helper.Handle, atom, NativeMethods.MOD_SHIFT | NativeMethods.MOD_CONTROL, NativeMethods.VK_KEY_C)) | |
throw new Win32Exception(Marshal.GetLastWin32Error()); | |
}; | |
// 取消挂载全局快捷键 | |
this.Closed += (sender, args) => { | |
if(atom != 0) | |
NativeMethods.UnregisterHotKey(hwndSource.Handle, atom); | |
}; | |
/// <summary> | |
/// 处理主窗体的消息 | |
/// </summary> | |
/// <param name="hwnd"></param> | |
/// <param name="msg"></param> | |
/// <param name="wParam"></param> | |
/// <param name="lParam"></param> | |
/// <param name="handled"></param> | |
/// <returns></returns> | |
private IntPtr MainWindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { | |
switch(msg) { | |
case NativeMethods.WM_HOTKEY: | |
//TODO: Business Logics | |
handled = true; | |
break; | |
} | |
return IntPtr.Zero; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
internal static class NativeMethods { | |
#region HotKey | |
[DllImport("user32.dll")] | |
[return: MarshalAs(UnmanagedType.Bool)] | |
public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk); | |
[DllImport("user32.dll")] | |
[return: MarshalAs(UnmanagedType.Bool)] | |
public static extern bool UnregisterHotKey(IntPtr hWnd, int id); | |
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)] | |
public static extern short GlobalAddAtom(string lpString); | |
[DllImport("kernel32", SetLastError = true)] | |
public static extern short GlobalDeleteAtom(short nAtom); | |
public const int MOD_ALT = 1; | |
public const int MOD_CONTROL = 2; | |
public const int MOD_SHIFT = 4; | |
public const int MOD_WIN = 8; | |
public const uint VK_KEY_C = 0x43; | |
public const int WM_HOTKEY = 0x312; | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment