可以通过发送WM_SYSKEYDOWN或者WM_SYSKEYUP来实现 The WM_SYSKEYDOWN message is posted to the window with the keyboard focus when the user presses the F10 key (which activates the menu bar) or holds down the alt key and then presses another key. It also occurs when no window currently has the keyboard focus; in this case, the WM_SYSKEYDOWN message is sent to the active window. The window that receives the message can distinguish between these two contexts by checking the context code in the lKeyData parameter. WM_SYSKEYDOWN nVirtKey = (int) wParam; // virtual-key code lKeyData = lParam; // key data
为什么非要发送ALT+F4呢
The WM_SYSKEYDOWN message is posted to the window with the keyboard focus when the user presses the F10 key (which activates the menu bar) or holds down the alt key and then presses another key. It also occurs when no window currently has the keyboard focus; in this case, the WM_SYSKEYDOWN message is sent to the active window. The window that receives the message can distinguish between these two contexts by checking the context code in the lKeyData parameter. WM_SYSKEYDOWN
nVirtKey = (int) wParam; // virtual-key code
lKeyData = lParam; // key data
BOOL CTestDlgDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_F4)
{
if(GetKeyState(VK_ALT) < 0)
AfxMessageBox("ALT+F4");
}
return CDialog::PreTranslateMessage(pMsg);
}
RegisterHotKey(m_hWnd,0Xa002,MOD_ALT,VK_F4);
::SetFocus(m_hWnd);***********************************************************
BOOL CMySysHotKeyDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if (pMsg->message==WM_HOTKEY && pMsg->wParam==0Xa002)
{
AfxMessageBox("My MessageBox!");
}
return CDialog::PreTranslateMessage(pMsg);
}******************************************************************LRESULT CMySysHotKeyDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
if(message == WM_CLOSE)
{
::UnregisterHotKey(m_hWnd,0Xa002);
}
return CDialog::WindowProc(message, wParam, lParam);
}