VC下简易实现全局热键–无DLL无钩子(Register HotKey)

作者:Wupei  |  发表时间:  |  所属分类:VC

使用RegisterHotKey()函数即可.
MSDN:The RegisterHotKey function defines a system-wide hot key.

//函数原型:
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
);


具体实现:
1.首先加入函数

BOOL CMyDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here

	//注册热键(Ctrl+W,标识9999)
	RegisterHotKey(this->m_hWnd,9999,MOD_CONTROL,'W');

	return TRUE;  // return TRUE  unless you set the focus to a control
}

2.加入相应全局热键函数

//相应WindowProc消息,加入函数
LRESULT CMyCatchScreenDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	switch(message)
	{
		///////////////
		//热键操作
		case WM_HOTKEY: 
			if(wParam==9999)   
			{   
				if(!IsWindowVisible())
				{
					//ShowMyWindow();	// 实现代码
				}
				else
				{
					//HideMyWindow();	//实现代码
				}
			}
		break;
	} 
	return CDialog::WindowProc(message, wParam, lParam);
}

Trackback from your site.

请在这里留言: