Radio Button Control Example
#include <windows.h>
#define ID_BLUE 1
#define ID_YELLOW 2
#define ID_RED 3
COLORREF bk_color = RGB(255, 255, 255);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX wc;
MSG msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = TEXT("MyWindowClass");
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wc);
CreateWindowEx(
WS_EX_CLIENTEDGE,
TEXT("MyWindowClass"),
TEXT("Radio Button Demo"),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
700,
200,
NULL,
NULL,
hInstance,
NULL);
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
HBRUSH hBrush;
RECT rect;
switch (msg)
{
case WM_CREATE:
// Create a group box
CreateWindow(
TEXT("BUTTON"),
TEXT("Choose Colour"),
WS_CHILD | WS_VISIBLE | BS_GROUPBOX,
50, 25, 300, 70,
hwnd,
NULL,
NULL,
NULL);
// Create the radio buttons
CreateWindow(
TEXT("BUTTON"),
TEXT("Blue"),
WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,
70, 50, 75, 30,
hwnd,
(HMENU)ID_BLUE,
NULL,
NULL);
CreateWindow(
TEXT("BUTTON"),
TEXT("Yellow"),
WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,
145, 50, 85, 30,
hwnd,
(HMENU)ID_YELLOW,
NULL,
NULL);
CreateWindow(
TEXT("BUTTON"),
TEXT("Red"),
WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,
230, 50, 75, 30,
hwnd,
(HMENU)ID_RED,
NULL,
NULL);
return 0;
case WM_COMMAND:
if (HIWORD(wParam) == BN_CLICKED)
{
switch (LOWORD(wParam))
{
case ID_BLUE:
bk_color = RGB(0, 76, 255);
break;
case ID_YELLOW:
bk_color = RGB(255, 255, 0);
break;
case ID_RED:
bk_color = RGB(255, 0, 0);
break;
}
// Redraw the window
InvalidateRect(hwnd, NULL, TRUE);
}
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
hBrush = CreateSolidBrush(bk_color);
GetClientRect(hwnd, &rect);
FillRect(hdc, &rect, hBrush);
DeleteObject(hBrush);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}