日期:2011-07-08 浏览次数:20568 次
要设置快捷键必须使用user32.dll下面的两个方法。
BOOL RegisterHotKey( //注册系统热键的API函数
HWND hWnd,
int id,
UINT fsModifiers,
UINT vk
);
BOOL UnregisterHotKey( //删除系统热键的API函数
HWND hWnd,
int id
);
在C#中引用命名空间System.Runtime.InteropServices;来加载非托管类user32.dll
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace HotKey
{
public enum KeyModifiers //组合键枚举
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
public partial class Form1 : Form
{
/*
* RegisterHotKey函数原型及说明:
* BOOL RegisterHotKey(
* HWND hWnd, // window to receive hot-key notification
* int id, // identifier of hot key
* UINT fsModifiers, // key-modifier flags
* UINT vk // virtual-key code);
* 参数 id为你自己定义的一个ID值
* 对一个线程来讲其值必需在0x0000 - 0xBFFF范围之内,十进制为0~49151
* 对DLL来讲其值必需在0xC000 - 0xFFFF 范围之内,十进制为49152~65535
* 在同一进程内该值必须唯一参数 fsModifiers指明与热键联合使用按键
* 可取值为:MOD_ALT MOD_CONTROL MOD_WIN MOD_SHIFT参数,或数字0为无,1为Alt,2为Control,4为Shift,8为Windows
* vk指明热键的虚拟键码
*/
[System.Runtime.InteropServices.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
);
[System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函数
public static extern bool UnregisterHotKey(
IntPtr hWnd, // handle to window
int id // hot key identifier
);
public Form1()
{
InitializeComponent();
}
private void ProcessHotkey(Message m) //按下设定的键时调用该函数
{
IntPtr id = m.