[收藏]在C#程序中使用系统热键

1.首先引入System.Runtime.InteropServices
using System.Runtime.InteropServices; 

2.在类内部声明两个API函数,它们的位置和类的成员变量等同.

4.在类的构造函数出注册系统热键
示例,下例注册了四个热键:

Code


5.重写WndProc()方法,通过监视系统消息,来调用过程
示例:

Code


5.不用说,我们接下来需要实现ProcessHotkey函数:

Code


很明显接下来分别实现函数DecreseVolumnb(); 和AddVolumnb(); 即可.

6.最后别忘了在程序退出时取消热键的注册

Code

Code
[DllImport("user32.dll")] //申明API函数
public static extern bool RegisterHotKey( 
IntPtr hWnd, 
// handle to window 
int id, // hot key identifier 
uint fsModifiers, // key-modifier options 
Keys vk // virtual-key code 
); 

[DllImport(
"user32.dll")] //申明API函数 
public static extern bool UnregisterHotKey( 
IntPtr hWnd, 
// handle to window 
int id // hot key identifier 
); 


3.定义一个KeyModifiers的枚举,以便出现组合键

Code
posted @ 2009-01-24 11:11  rangeliu  阅读(938)  评论(0编辑  收藏  举报