HOWTO: Using Accelerator Keys with Modal Dialog Box Main WindowID: Q100770
|
The TRACER sample application, provided with the Microsoft Foundation Class Library, uses a modal dialog box as its main window. Many applications that use this technique also include a main menu on the dialog box (the TRACER sample does not use a menu). Typically, one or more of the menu items has a keyboard accelerator associated with it. The text below details the steps required to add a menu and keyboard accelerators to the TRACER sample.
A typical application, developed for the Windows operating system
using C and the Microsoft Windows Software Development Kit (SDK), that
uses keyboard accelerators, calls the TranslateAccelerator() function
in its main message loop. However, when you use a modal dialog box as
the main window, the application does not have a main message loop;
instead, the application uses the dialog manager message loop (built
into Windows) 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 CWinApp::ProcessMessageFilter() function. The framework calls
ProcessMessageFilter() before it processes a message.
Perform the following steps to modify the TRACER sample to
correctly process accelerator keys:
PostMessage(WM_COMMAND, IDOK, 0L);
This gives the same effect as clicking OK when the user
chooses Exit from the File menu. Clicking OK closes
TRACER.
HWND ghDlg; // Handle to main dialog box
HACCEL ghAccelTable; // Handle to accelerator table
ghDlg = m_hWnd;
In Visual C++ 4.0, be sure to edit the newly added function, not the
existing, Machintosh-specific one:
ghDlg = m_hWnd;
ghAccelTable = LoadAccelerators(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDR_ACCELERATOR1));
virtual BOOL ProcessMessageFilter(int code, LPMSG lpMsg);
BOOL CTracerApp::ProcessMessageFilter(int code, LPMSG lpMsg)
{
if (code < 0)
CWinApp::ProcessMessageFilter(code, lpMsg);
if (ghDlg && ghAccelTable)
{
if (::TranslateAccelerator(ghDlg, ghAccelTable, lpMsg))
return(TRUE);
}
return CWinApp::ProcessMessageFilter(code, lpMsg);
}
Keywords : kbui kbMFC kbVC100 kbVC150 kbVC151 kbVC152 kbVC200 kbVC210 kbVC400 kbVC500 kbVC600
Version : winnt:2.0,2.1,4.0,5.0,6.0
Platform : winnt
Issue type : kbhowto
Last Reviewed: August 8, 1999