在.h中:
public: // User declarations
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_HOTKEY,TMessage,OnHotKey)
END_MESSAGE_MAP(TForm)
void __fastcall TMainForm::OnHotKey(TMessage &Message); //響應熱鍵消息
在.cpp中:
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
//注冊系統熱鍵
if(!RegisterHotKey(Handle,2000,MOD_CONTROL,VK_F12))
//這裡注冊的熱鍵是Ctrl+F12
ShowMessage("熱鍵注冊失敗");
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::OnHotKey(TMessage &Message)
{
//添加你的代碼
TForm::Dispatch(&Message);
if (!MainForm->Visible)
{
MainForm->Show();
MainForm->BringToFront();
}
else
MainForm->Hide();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action)
{
UnregisterHotKey(Handle, 2000);//退出程序時注銷熱鍵
}