RadioButton Dialog
A Win32 dialog example showing how to read the state of radio buttons with IsDlgButtonChecked.
The minimal pattern for radio buttons in a Win32 dialog. The controls are laid out in the resource as two button groups (IDs 1000 and 2000), so Windows handles the mutual exclusion within each group for you; all the code has to do is ask which one is selected when Ok is pressed. IsDlgButtonChecked returns BST_CHECKED (1) for the selected button in a group, BST_UNCHECKED (0) for the rest - no extra bookkeeping needed. A good reference whenever you need to pull option choices out of a dialog.
/////////////////////////////////////////////////////////////////////
BOOL CALLBACK MainDialog(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
static HMENU SystemMenu;
while(1) // Loop Forever
{
if(Msg==WM_INITDIALOG)
{
MainDialog_hWnd=hDlg;
SystemMenu = GetSystemMenu(MainDialog_hWnd,FALSE);
AppendMenu(SystemMenu,MF_SEPARATOR,0,0);
AppendMenu(SystemMenu,MF_STRING,1000,"www.NetWorkDLS.Com");
AppendMenu(SystemMenu,MF_STRING,1001,"Version");
SendMessage(MainDialog_hWnd,(UINT)WM_SETTEXT,(WPARAM)0,(LPARAM)TitleCaption);
SendMessage(MainDialog_hWnd,WM_SETICON,TRUE,(LPARAM)LoadIcon(GetWindowInstance(MainDialog_hWnd),MAKEINTRESOURCE(300)));
CenterWindow(MainDialog_hWnd);
break;
}
if(Msg==WM_COMMAND)
{
if(wParam==100) //- Okay (Dialog Command)
{
if(IsDlgButtonChecked(MainDialog_hWnd,1000))
MessageBox(MainDialog_hWnd, "Radio 1 Checked", TitleCaption, 0 + MB_ICONASTERISK + MB_TASKMODAL);
if(IsDlgButtonChecked(MainDialog_hWnd,2000))
MessageBox(MainDialog_hWnd, "Radio 2 Checked", TitleCaption, 0 + MB_ICONASTERISK + MB_TASKMODAL);
EndDialog(MainDialog_hWnd,0);
}
if(wParam==200) //- About (Dialog Command)
{
ShowWindow(MainDialog_hWnd, SW_HIDE);
DialogBox(MainInstance,MAKEINTRESOURCE(1000),MainDialog_hWnd,AboutDialog);
ShowWindow(MainDialog_hWnd, SW_SHOW);
}
if(wParam==300) //- Exit (Dialog Command)
{
EndDialog(MainDialog_hWnd,0);
}
break;
}
if(Msg==WM_SYSCOMMAND)
{
if(LOWORD(wParam)==1000) //- NetWorkDLS.Com (SysMenu Command)
{
ShellExecute(0,"Open","Http://www.NetWorkDLS.com",NULL,NULL,SW_SHOWNORMAL);
}
if(LOWORD(wParam) == 1001) //- Version (SysMenu Command)
{
MessageBox(MainDialog_hWnd, FileVersion, TitleCaption, 0 + MB_ICONASTERISK + MB_TASKMODAL);
}
}
if(Msg==WM_CLOSE) //Exit (Windows Message)
{
KillTimer(MainDialog_hWnd,0);
EndDialog(MainDialog_hWnd,0);
}
break;
}
return 0;
}
The same IsDlgButtonChecked call works for checkboxes too - it is the universal "what did the user pick" query for dialog button-style controls. To set a radio programmatically use CheckDlgButton(hDlg, id, BST_CHECKED).