TinyMind
.net.cn
首页
资讯
文章
注册
登录
C# 设置全局热键
几度夕阳红了
关注文章
TinyMind专栏频道
Code
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
System.Runtime.InteropServices;
using
System.Collections;
namespace
HotKey1
{
public
partial
class
Form1:Form
{
public
Form1()
{
InitializeComponent();
}
public
delegate
void
HotkeyEventHandler(
int
HotKeyID);
private
int
Hotkey1;
public
class
Hotkey:System.Windows.Forms.IMessageFilter
{
HashtablekeyIDs
=
new
Hashtable();
IntPtrhWnd;
public
event
HotkeyEventHandlerOnHotkey;
public
enum
KeyFlags
{
MOD_ALT
=
0x1
,
MOD_CONTROL
=
0x2
,
MOD_SHIFT
=
0x4
,
MOD_WIN
=
0x8
}
[DllImport(
"
user32.dll
"
)]
public
static
extern
UInt32RegisterHotKey(IntPtrhWnd,UInt32id,UInt32fsModifiers,UInt32vk);
[DllImport(
"
user32.dll
"
)]
public
static
extern
UInt32UnregisterHotKey(IntPtrhWnd,UInt32id);
[DllImport(
"
kernel32.dll
"
)]
public
static
extern
UInt32GlobalAddAtom(StringlpString);
[DllImport(
"
kernel32.dll
"
)]
public
static
extern
UInt32GlobalDeleteAtom(UInt32nAtom);
public
Hotkey(IntPtrhWnd)
{
this
.hWnd
=
hWnd;
Application.AddMessageFilter(
this
);
}
public
int
RegisterHotkey(KeysKey,KeyFlagskeyflags)
{
UInt32hotkeyid
=
GlobalAddAtom(System.Guid.NewGuid().ToString());
RegisterHotKey((IntPtr)hWnd,hotkeyid,(UInt32)keyflags,(UInt32)Key);
keyIDs.Add(hotkeyid,hotkeyid);
return
(
int
)hotkeyid;
}
public
void
UnregisterHotkeys()
{
Application.RemoveMessageFilter(
this
);
foreach
(UInt32key
in
keyIDs.Values)
{
UnregisterHotKey(hWnd,key);
GlobalDeleteAtom(key);
}
}
public
bool
PreFilterMessage(
ref
System.Windows.Forms.Messagem)
{
if
(m.Msg
==
0x312
)
{
if
(OnHotkey
!=
null
)
{
foreach
(UInt32key
in
keyIDs.Values)
{
if
((UInt32)m.WParam
==
key)
{
OnHotkey((
int
)m.WParam);
return
true
;
}
}
}
}
return
false
;
}
}
public
void
OnHotkey(
int
HotkeyID)
//
Ctrl+F2隐藏窗体,再按显示窗体。
{
if
(HotkeyID
==
Hotkey1)
{
if
(
this
.Visible
==
true
)
this
.Visible
=
false
;
else
this
.Visible
=
true
;
}
else
{
this
.Visible
=
false
;
}
}
private
void
Form1_Load(
object
sender,EventArgse)
{
Hotkeyhotkey;
hotkey
=
new
Hotkey(
this
.Handle);
Hotkey1
=
hotkey.RegisterHotkey(System.Windows.Forms.Keys.F2,Hotkey.KeyFlags.MOD_CONTROL);
//
定义快键(Ctrl+F2)
hotkey.OnHotkey
+=
new
HotkeyEventHandler(OnHotkey);
}
}
}
优质内容筛选与推荐
>>
1、flash 中位图九宫格的用法
2、即时通讯(II)
3、什么是倒排索引?
4、opensuse pptp拨号设置注意事项
5、6月19日 NSFileHandle文件类的常用方法
赞赏
长按二维码向我转账
受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。
阅读
好看
已推荐到看一看
你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
取消
推荐
我知道了
已取消,“好看”想法已同步删除
知道了
已推荐到看一看
和朋友分享想法
最多200字,当前共
字
发送
已发送
朋友将在看一看看到
确定
分享你的想法...
取消
分享想法到看一看
确定
最多200字,当前共
字
发送中
网络异常,请稍后重试
知道了
微信扫一扫
关注该公众号
0
│
收藏
│
举报