HOWTO: Using Accelerator Keys Within a Modal Dialog BoxID: Q222829
|
Accelerator keys are a common User Interface feature of Windows applications; therefore, why limit them to just the application? This article shows how to add accelerator key functionality to any modal dialog box.
Keyboard accelerators are processed by calling the TranslateAccelerator() function in the application's main message loop. However, for a modal dialog box, the dialog box manager message loop (built into Windows) is used to translate and dispatch messages. Of course, because this message loop is not designed to process accelerators, it does not call the TranslateAccelerator() function.
To process accelerator keys in a modal dialog box, you must override the the dialog box's PreTranslateMessage() function and try to process the message as an accelerator by calling ::TranslateAccelerator(). If this method fails, then processing continues by calling the the base class PreTranslateMessage().
For the purposes of this article, we add accelerator key functionality to the AboutBox dialog box of an MFC AppWizard-generated MDI application:
AfxMessageBox("Hello");
HACCEL m_hAccelTable;
m_hAccelTable = LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_ACCELERATOR1));
BOOL CAboutDlg::PreTranslateMessage(MSG* pMsg) {
if (m_hAccelTable) {
if (::TranslateAccelerator(m_hWnd, m_hAccelTable, pMsg)) {
return(TRUE);
}
}
return CDialog::PreTranslateMessage(pMsg);
}
For additional information about accelerator key processing within modal dialog boxes, please see the following article in the Microsoft Knowledge Base:
Q100770 INFO: Using Accelerator Keys with Modal Dialog Box Main Window
Additional query words: accelerator modal dialog
Keywords : kbKeyAccel kbMFC kbVC410 kbVC420 kbVC500 kbVC600 kbVS600
Version : winnt:4.1,4.2,5.0,6.0
Platform : winnt
Issue type : kbhowto
Last Reviewed: April 21, 1999