winform注册全局热键ALT+R快捷键呼出,不过在win10下貌似没生效,很奇怪。
using System; using System.Runtime.InteropServices; using System.Windows.Forms; public partial class Form1 : Form { // 导入 user32.dll 库中的 RegisterHotKey 和 UnregisterHotKey 函数 [DllImport("user32.dll")] private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk); [DllImport("user32.dll")] private static extern bool UnregisterHotKey(IntPtr hWnd, int id); // 定义热键 ID private const int HOTKEY_ID = 1; public Form1() { InitializeComponent(); // 注册全局热键 RegisterHotKey(this.Handle, HOTKEY_ID, (int)Modifiers.Alt, Keys.R.GetHashCode()); } protected override void WndProc(ref Message m) { // 捕获 WM_HOTKEY 消息 if (m.Msg == 0x0312 && m.WParam.ToInt32() == HOTKEY_ID) { // 处理热键事件 if (this.Visible) { this.Hide(); } else { this.Show(); } } base.WndProc(ref m); } protected override void Dispose(bool disposing) { // 注销全局热键 UnregisterHotKey(this.Handle, HOTKEY_ID); base.Dispose(disposing); } }