Microsoft KB Archive/222829

= How to use accelerator keys within a modal dialog box in Visual C++ =

Article ID: 222829

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 4.1 Subscription

 Microsoft Visual C++ 4.2 Enterprise Edition

 Microsoft Visual C++ 5.0 Enterprise Edition

 Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2002 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2003 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q222829

<div class="notice_section">

Note Microsoft Visual C++ .NET (2002) supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code.

<div class="summary_section">

SUMMARY
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.

<div class="moreinformation_section">

MORE INFORMATION
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 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 base class PreTranslateMessage.

For the purposes of this article, we add accelerator key functionality to the AboutBox dialog box of an MFC MDI application: <ol> Create a new MFC MDI application named Test.</li> Add a button, with resource ID "IDC_BUTTON1", to the AboutBox dialog box resource.</li>  Double-click the button to add a handler for this button and insert the following code as an indicator that the accelerator key works: AfxMessageBox("Hello"); </li> Insert a new Accelerator Table to the Resource.</li> <li>Add an Accelerator key to the table by associating the F5 key to the resource ID "IDC_BUTTON1".</li> <li> Add the member variable, m_hAccelTable, to the class CAboutBox: HACCEL m_hAccelTable; </li> <li> Initialize m_hAccelTable in CAboutBox::CAboutBox: m_hAccelTable = LoadAccelerators(AfxGetInstanceHandle, MAKEINTRESOURCE(IDR_ACCELERATOR1)); </li> <li>Use the Code Wizard (the Class Wizard in Visual C++ 6.0 and earlier) to add an override of the PreTranslateMessage for the CAboutDlg.</li> <li> In the CAboutBox::PreTranslateMessage method, add the following lines of code: BOOL CAboutDlg::PreTranslateMessage(MSG* pMsg) { if (m_hAccelTable) { if (::TranslateAccelerator(m_hWnd, m_hAccelTable, pMsg)) { return(TRUE); }  }   return CDialog::PreTranslateMessage(pMsg);

} </li> <li>Compile and run CTestApp.</li></ol>

Results Accelerator key F5 now triggers the button1 handler when the CAboutBox is active.

<div class="references_section">