📄 class1.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.WindowsCE.Forms;
namespace TestForm
{
public class KeyModifers
{
public const uint MOD_ALT = 0x1;
public const uint MOD_CONTROL = 0x2;
public const uint MOD_SHIFT = 0x4;
public const uint MOD_WIN = 0x8;
}
//声明硬件按键常量 只能是这几个值
public class KeysHardware
{
public const uint Hardware1 = 193;
public const uint Hardware2 = 194;
public const uint Hardware3 = 195;
public const uint Hardware4 = 196;
public const uint Hardware5 = 197;
}
//第二步,封装两个api函数,调用非托管dll函数类
public class RegisterHotKeys
{
//声明api
[DllImport("coredll.dll")]
private static extern bool UnregisterFunc1(
uint fsModifiers, //组合键的键值
uint vk //热键键值
);
[DllImport("coredll.dll")]
private static extern bool RegisterHotKey(
IntPtr hWnd, //要注册的窗体的句柄
int id, // 热键的键值
uint fsModifiers, //组合键的键值
uint vk // virtual-key code (虚拟键的编码,这里和第二个参数一样)
);
[DllImport("coredll.dll")]
private static extern bool UnregisterHotKey(
IntPtr hWnd, //要注册的窗体的句柄
int id //热键的键值
);
//end
//注册热键,封装了UnregisterFunc1和RegisterHotKey
private static bool _mRegisterHotKey(IntPtr hwnd, int id, uint vk)
{
// bool re = UnregisterFunc1(KeyModifers.MOD_WIN, vk);
bool re = UnregisterFunc1(KeyModifers.MOD_ALT, vk);
bool re1 = RegisterHotKey(hwnd, id, KeyModifers.MOD_WIN, vk);
return re && re1;
}
public static bool RegisterHotKey(IntPtr hwnd)
{
bool re = true;
for (int i = (int)KeysHardware.Hardware1; i <= (int)KeysHardware.Hardware5; i++)
{
re = _mRegisterHotKey(hwnd, i, (uint)i);
if (!re) break;
}
return re;
}
public static bool UnRegisterHotKey(IntPtr hwnd)
{
bool re = true;
for (int i = (int)KeysHardware.Hardware1; i <= (int)KeysHardware.Hardware5; i++)
{
UnregisterHotKey(hwnd, i);
if (!re) break;
}
return re;
}
}//大家可以看到这个类只有两个public函数,其他的函数对外不提供,为内部服务
//第三步重写消息循环,定义消息窗体,并重写wndproc方法
public class MyMsgWindow : MessageWindow
{
public const int WM_HOTKEY = 0x0312;
private TestForm form = null; //TestForm为你的热键要注册的应用主窗体
public MyMsgWindow(TestForm form)
{
this.form = form;
}
protected override void WndProc(ref Message msg)
{
switch (msg.Msg)
{
case WM_HOTKEY:
form.clickHardWareButton(msg.WParam.ToInt32());//clickHardWareButton是////你在主窗体中定义的按钮事件函数,在这里被调用
return;
}
base.WndProc(ref msg);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -