HOWTO: Manage Per-Window Accelerator TablesID: Q82171
|
In the Windows environment, an application can have several windows, each with its own accelerator table. This article describes a simple technique requiring very little code that an application can use to translate and dispatch accelerator key strokes to several windows. The technique employs two global variables, ghActiveWindow and ghActiveAccelTable, to track the currently active window and its accelerator table, respectively. These two variables, which are used in the TranslateAccelerator function in the application's main message loop, achieve the desired result.
The key to implementing this technique is to know which window is
currently active and which accelerator table, if any, is associated
with the active window. To track this information, process the
WM_ACTIVATE message that Windows sends each time an application gains
or loses activation. When a window loses activation, set the two
global variables to NULL to indicate that the window and its
accelerator table are no longer active. When a window that has an
accelerator table gains activation, set the global variables
appropriately to indicate that the accelerator table is active. The
following code illustrates how to process the WM_ACTIVATE message:
case WM_ACTIVATE:
if (wParam == 0) // Indicates loss of activation.
{
ghActiveWindow = ghActiveAccelTable = NULL;
}
else // Indicates gain of activation.
{
ghActiveWindow = <this window>;
ghActiveAccelTable = <this window's accelerator table>;
}
break;
while (GetMessage(&msg, // Message structure.
NULL, // Handle of window receiving the msg.
NULL, // Lowest message to examine.
NULL)) // Highest message to examine.
{
if (!TranslateAccelerator(ghActiveWindow, // Active window.
ghActiveAccelTable, // Active accelerator.
&msg))
{
TranslateMessage(&msg); // Translates virtual key codes.
DispatchMessage(&msg); // Dispatches message to window procedure.
}
}
Keywords : kbKeyAccel kbNTOS kbGrpUser kbWinOS kbWndw kbWndwMsg
Version :
Platform :
Issue type : kbhowto
Last Reviewed: March 7, 1999