April 7th, 2005

The dialog manager, part 8: Custom navigation in dialog boxes

Some dialog boxes contain custom navigation that goes beyond what the IsDialogMessage function provides. For example, property sheets use Ctrl+Tab and Ctrl+Shift+Tab to change pages within the property sheet. Remember the core of the dialog loop:

while (<dialog still active> &&
       GetMessage(&msg, NULL, 0, 0, 0)) {
 if (!IsDialogMessage(hdlg, &msg)) {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }
}

(Or the modified version we created in part 7.)

To add custom navigation, just stick it in before calling IsDialogMessage.

while (<dialog still active> &&
       GetMessage(&msg, NULL, 0, 0, 0)) {
 if (msg.message == WM_KEYDOWN &&
     msg.wParam == VK_TAB &&
     GetKeyState(VK_CONTROL) < 0) {
  ... do custom navigation ...
 } else if (!IsDialogMessage(hdlg, &msg)) {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }
}

After retrieving a message, we check whether it was Ctrl+Tab before dispatching it or indeed even before letting IsDialogMessage see it. If so, then treat it as a navigation key.

Note that if you intend to have modeless dialogs controlled by this message loop, then your test needs to be a little more focused, because you don’t want to pick off keyboard navigation keys destined for the modeless dialog.

while (<dialog still active> &&
       GetMessage(&msg, NULL, 0, 0, 0)) {
 if ((hdlg == msg.hwnd || IsChild(hdlg, msg.hwnd)) &&
     msg.message == WM_KEYDOWN &&
     msg.wParam == VK_TAB &&
     GetKeyState(VK_CONTROL) < 0) {
  ... do custom navigation ...
 } else if (!IsDialogMessage(hdlg, &msg)) {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }
}

Next time, we’ll see another way of accomplishing this same task.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.

Feedback