A subtlety that adds a level of polish to your dialogs is supporting double-clicked radio buttons as an abbreviation for “select + OK”. (Or “select + Next” or “select + Finish” if the page is part of a wizard.)
Consider the following dialog template and associated dialog procedure:
1 DIALOGEX DISCARDABLE 32, 32, 200, 76 STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU CAPTION “Sample” FONT 8, “MS Shell Dlg” BEGIN LTEXT “A mumbler is needed.”,-1,7,8,100,10 AUTORADIOBUTTON “Do not &obtain a mumber now”, 100,17,24,180,10 AUTORADIOBUTTON “Obtain a mumbler auto&matically”, 101,17,34,180,10 AUTORADIOBUTTON “&Enter mumbler manually”, 102,17,44,180,10 DEFPUSHBUTTON “OK”,IDOK,92,58,50,14 PUSHBUTTON “Cancel”,IDCANCEL,146,58,50,14 ENDINT_PTR CALLBACK DlgProc(HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_INITDIALOG: CheckRadioButton(hdlg, 100, 102, 100); return TRUE; case WM_COMMAND: switch (GET_WM_COMMAND_ID(wParam, lParam)) { case IDOK: for (int i = 100; i <= 102; i++) { if (IsDlgButtonChecked(hdlg, i)) EndDialog(hdlg, i); } break; case IDCANCEL: EndDialog(hdlg, -1); break; } } return FALSE; }
This is pretty standard unexciting dialog box that asks the user to select an option from a list. Notice that double-clicking the radio button doesn’t do anything special. We can fix that.
1 DIALOGEX DISCARDABLE 32, 32, 200, 76 STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU CAPTION “Sample” FONT 8, “MS Shell Dlg” BEGIN LTEXT “A mumbler is needed.”,-1,7,8,100,10 AUTORADIOBUTTON “Do not &obtain a mumber now”, 100,17,24,180,10,BS_NOTIFY AUTORADIOBUTTON “Obtain a mumbler auto&matically”, 101,17,34,180,10,BS_NOTIFY AUTORADIOBUTTON “&Enter mumbler manually”, 102,17,44,180,10,BS_NOTIFY DEFPUSHBUTTON “OK”,IDOK,92,58,50,14 PUSHBUTTON “Cancel”,IDCANCEL,146,58,50,14 ENDINT_PTR CALLBACK DlgProc(HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_INITDIALOG: CheckRadioButton(hdlg, 100, 102, 100); return TRUE; case WM_COMMAND: switch (GET_WM_COMMAND_ID(wParam, lParam)) { case IDOK: for (int i = 100; i <= 102; i++) { if (IsDlgButtonChecked(hdlg, i)) EndDialog(hdlg, i); } break; case IDCANCEL: EndDialog(hdlg, -1); break; case 100: case 101: case 102: if (GET_WM_COMMAND_CMD(wParam, lParam) == BN_DBLCLK) { EndDialog(hdlg, GET_WM_COMMAND_ID(wParam, lParam)); } break; } } return FALSE; }
We added the BS_NOTIFY
style to the radio buttons,
which enables the extended notifications (everything other than
BN_CLICKED
). When we receive a WM_COMMAND
message for a radio button specifying that the operation was
BN_DBLCLK
, we automatically click the OK button.
(For a wizard, we would automatically click the Next or
Finish button, as appropriate.)
Note that double-click as a shortcut for select-and-OK should be used only for dialogs or wizard pages where the only relevant controls are radio buttons (and the OK and Cancel buttons). If there are controls on the page other than the radio button, then you have to wait for the OK because the user might want to manipulate those other controls too.
This “double-click means select-and-OK” shortcut can also be used if the only thing on the dialog is a list box or list view from with the user is being asked to select one item. If the user double-clicks an item from the list, treat it as select-and-OK.
The dialog boxes in Explorer are rather inconsistent in their support for double-clicked radio buttons. The Office and Money teams do a much better job.
0 comments