ResiterHotKey的用法和消息处理的用法
这个程序有一个窗体Form1,Form1上注册了一个热键Ctrl+F11,以后只要这个程序在运行,不管在什么,你一按Ctrl+F11, 就会弹出一个消息框。
//Unit.H
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
void __fastcall FormCreate(TObject *Sender);
void __fastcall FormDestroy(TObject *Sender);
private: // User declarations
//这个函数就是消息处理函数,通过VCL_MESSAGE_HANDLER宏和指定消息相连
void __fastcall WMHotKey(TMessage &Msg);
public: // User declarations
__fastcall TForm1(TComponent* Owner);
//以下三句就是消息处理,可以在BEGIN和END之间加入多个HANDLER
BEGIN_MESSAGE_MAP
//处理WM_HOTKEY消息,注册热键之后,按下热键就会向指定窗口发出该消息
VCL_MESSAGE_HANDLER(WM_HOTKEY, TMessage, WMHotKey);
END_MESSAGE_MAP(TForm);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//Unit1.CPP
//---------------------------------------------------------------------------
#define HOTKEY_SMS 0x1000
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
//这就是消息处理函数的实现
void __fastcall TForm1::WMHotKey(TMessage &Msg)
{
SetForegroundWindow(Handle);
ShowMessage("Hot Key Registered!");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
RegisterHotKey(Handle, HOTKEY_SMS, MOD_CONTROL, VK_F11);
//以上那句注册热键,Handle是窗口句柄,0x1000是一个ID号,设置该热键在线程中
//的唯一值,第三个参数表示要按下那些系统键,最后一个是按键的虚拟键码
//具体可参见MSDN帮助
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
UnregisterHotKey(Handle,HOTKEY_SMS);
//注销热键,0x1000和上面对应,指明注销哪个热键
}
//---------------------------------------------------------------------------
转载http://bbs.csdn.net/topics/131060