在应用程序中使用热键的一段代码
2008-03-08 12:52:54 来源:WEB开发网核心提示:在.h中:public: // User declarationsBEGIN_MESSAGE_MAP MESSAGE_HANDLER(WM_HOTKEY,TMessage,OnHotKey)END_MESSAGE_MAP(TForm)void __fastcall TMainForm::OnHotKey(TMessag
在.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);//退出程序时注销热键
}
更多精彩
赞助商链接