Moving on to dialog buttons now. We're gonna start with the Check Box control.
Drag a "Check Box" control over from your tool box and place it on your dialog box.
You can see if it is checked by:
IsDlgButtonChecked( hDlg, IDC_MYCHECKBOX );
hDlg is the handle to the dialog that has the checkbox
IDC_MYCHECKBOX is the id of your check box.
This function returns an unsigned int defined as
BST_CHECKED if its checked
BST_INDETERMINATE if it is grayed
BST_UNCHECKED if it is not checked
unsigned int isChecked;
isChecked = IsDlgButtonChecked( hDlg, IDC_MYCHECKBOX );
if ( isChecked == BST_CHECKED )
{
//do something because it's checked
}
else if ( isChecked == BST_UNCHECKED )
{
//it's not checked
}
So what if you want to set it to checked or not? Then you do...
CheckDlgButton( hDlg, IDC_MYCHECKBOX, check_status );
check_status can be one of the following...
BST_CHECKED if it should be checked
BST_INDETERMINATE if it should be grayed
BST_UNCHECKED if it should not be checked
the above are all defines that are included with your windows.h file.
Now the good news, this works for Check Boxes and Radio buttons!
__________________________________________________________
#include <stdio.h>
#include <windows.h>
#include "resource.h"
INT_PTR CALLBACK DialogMessages(HWND hwnd,UINT
uMsg,WPARAM wparam,LPARAM lparam)
{
//when the dialog box is first
//initialized have it send the string " Type in your
//first name" to our Static Text control
if( uMsg == WM_INITDIALOG )
{
char string [260]= "Type in your first name";
SetDlgItemText( hwnd, IDC_TEXT1, string );
}
if ( uMsg == WM_COMMAND )
{
if ( HIWORD(wparam) == BN_CLICKED )
{
if (LOWORD(wparam) == IDOK)
{
unsigned int isChecked;
isChecked = IsDlgButtonChecked( hwnd, IDC_CHECK1 );
//if the box is checked
if ( isChecked == BST_CHECKED )
{
char name [260];
char nameMr [260];
//get the name entered
GetDlgItemText( hwnd, IDC_EDIT1, name, 260 );
//take the name entered
//and add Mr. to it
sprintf(nameMr,"Mr.%s",name);
//place name in
//message box :)
MessageBox( NULL,nameMr, "messagebox",
MB_OK|MB_ICONEXCLAMATION );
EndDialog( hwnd, 0 );
return TRUE;
}
//if the box is not checked
else if ( isChecked == BST_UNCHECKED )
{
char name [260];
GetDlgItemText( hwnd, IDC_EDIT1, name, 260 );
MessageBox( NULL,name, "messagebox",
MB_OK|MB_ICONEXCLAMATION );
EndDialog( hwnd, 0 );
return TRUE;
}
}
else if (LOWORD(wparam) == IDCANCEL)
{
EndDialog( hwnd, 0 );
return TRUE;
}
}
}
return FALSE;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE
hPrevInstance, LPSTR pCmdLine, int iCmdShow)
{
DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_MYDIALOG),
NULL, DialogMessages);
return 0;
}