分类: C/C++
2008-08-01 17:11:05
//用PreTranslateMessage的方法,判断Ctrl Q是否按下,按下就执行快捷键对应的操作 if(pMsg->message == WM_KEYDOWN && pMsg->wParam == 'Q' && IsCTRLPressed()) { AfxMessageBox("Ctrl Q 被按下"); return TRUE; }
HACCEL hAcc; hAcc = LoadAccelerators(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDR_ACCELERATOR1));在PreTranslateMessage函数中进行快捷键的翻译,如果是WM_KEYDOWN和WM_SYSKEYDOWN消息就翻译,翻译成功返回TRUE。
BOOL CAccelerator2Dlg::PreTranslateMessage(MSG* pMsg) { // TODO: Add your specialized code here and/or call the base class int iResult; //针对WM_KEYDOWN消息和WM_SYSKEYDOWN消息,翻译快捷键 switch(pMsg->message) { case WM_KEYDOWN: case WM_SYSKEYDOWN: iResult = TranslateAccelerator(m_hWnd,hAcc,pMsg); //翻译快捷键成功,返回TRUE if(iResult) return TRUE; } return CDialog::PreTranslateMessage(pMsg); }下面处理快捷键对应的响应,在该实例中,快捷键表中建立了Ctrl Q,Shift Q,Alt Q,Ctrl Shift Q,Ctrl Alt Q,Shift Alt Q,Ctrl Shift Alt Q六个快捷键分别进行测试。
void CAccelerator2Dlg::OnCtrlQ() { // TODO: Add your control notification handler code here AfxMessageBox("你按下了定义的快捷键"); }