//checkbox demo
#include <windows.h>
#define ID_BLUE 1
#define ID_GREEN 2
#define ID_RED 3
int red, green,blue;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE hInstance;
COLORREF bk_color;
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PWSTR lpCmdLine, int nCmdShow) {
HWND hwnd;
MSG msg ;
WNDCLASS wc = {0};
wc.lpszClassName = TEXT("myWindowClass");
wc.hInstance = hInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(0, IDC_ARROW);
hInstance = hInstance;
RegisterClass(&wc);
hwnd = CreateWindow(wc.lpszClassName, TEXT("Checkbox Demo"),WS_OVERLAPPEDWINDOW | WS_VISIBLE,100, 100, 600, 200, 0, 0, hInstance, 0);
while( GetMessage(&msg, NULL, 0, 0)) {
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
HBRUSH hBrush;
switch(msg) {
case WM_CREATE:
red=0, green=0,blue=0;
int checked;
CreateWindow(TEXT("button"), TEXT("Blue"),WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,200, 60, 70, 30, hwnd, (HMENU) ID_BLUE , hInstance, NULL);
CreateWindow(TEXT("button"), TEXT("Green"),WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,270, 60, 70, 30, hwnd, (HMENU) ID_GREEN , hInstance, NULL);
CreateWindow(TEXT("button"), TEXT("Red"),WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,340, 60, 70, 30, hwnd, (HMENU) ID_RED , hInstance, NULL);
break;
case WM_COMMAND:
/*responds to button click by setting variable bk_color to value of colour associated with radio button */
if (HIWORD(wParam) == BN_CLICKED) {
switch (LOWORD(wParam)) {
case ID_BLUE:
checked = IsDlgButtonChecked(hwnd,ID_BLUE);
blue=checked*255;
break;
case ID_GREEN:
checked = IsDlgButtonChecked(hwnd,ID_GREEN);
green=checked*255;
break;
case ID_RED:
checked = IsDlgButtonChecked(hwnd,ID_RED);
red=checked*255;
break;
}
InvalidateRect(hwnd, NULL, TRUE);
}
break;
//changes colour of Window background
case WM_PAINT:
RECT theRect;
hdc = BeginPaint(hwnd, &ps);
bk_color=RGB(red,green,blue);
hBrush = CreateSolidBrush(bk_color);
GetClientRect(hwnd, &theRect);
FillRect(hdc, &theRect, hBrush);
EndPaint(hwnd, &ps);
DeleteObject(hBrush);
ReleaseDC(hwnd,hdc);
DeleteDC(hdc);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
Last Updated: 17 September 2022