本文实例讲述了C#全局热键设置与窗体热键设置,分享给大家供大家参考。具体实现方法如下:
1、窗体热键
首先要设置主窗体KeyPreview为true,可直接在属性中进行设置,
或者在窗体加载中设置: this.KeyPreview = true;
然后添加窗体KeyDown事件,如下:
2、全局热键设置
定义API函数 》 注册热键 》 卸载热键
我这里定义了AppHotKey类,全部代码如下:
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd, //要取消热键的窗口的句柄
int id //要取消热键的ID
);
//定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)
[Flags()]
public enum KeyModifiers
{
None = 0,
style="border-left-color: rgb(0, 153, 204); border-left-width: 1px; border-left-style: solid; padding: 0px 3px; margin: 3px auto 0px; width: 640px; background-color: rgb(242, 246, 251); clear: both; border-top-color: rgb(0, 153, 204); border-top-width: 1px; border-top-style: solid; border-right-color: rgb(0, 153, 204); border-right-width: 1px; border-right-style: solid;"> 复制代码代码如下:
希望本文所述对大家的C#程序设计有所帮助。
新闻热点
疑难解答