#include <Windows.h>
#include "resource.h"
#define ID_BUTTON_PUSH1 1
#define ID_BUTTON_PUSH2 11
#define ID_BUTTON_PUSH3 111
#define ID_BUTTON_PUSH4 222
#define ID_BUTTON_PUSH5 333
#define ID_BUTTON_CHECKBOX1 2
#define ID_BUTTON_RADIOBUTTON1 3
#define ID_BUTTON_RADIOBUTTON2 4
HINSTANCE ghInstance = 0;
LRESULT CALLBACK MainWindowProc (HWND, UINT, WPARAM, LPARAM);
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
ghInstance = hInstance;
// 注册窗口
WNDCLASSEX wndclass = { 0 };
wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.style = NULL;
wndclass.lpfnWndProc = MainWindowProc;
wndclass.cbClsExtra = NULL;
wndclass.cbWndExtra = NULL;
wndclass.hInstance = hInstance;
wndclass.hIcon = NULL;
wndclass.hCursor = NULL;
wndclass.hbrBackground = (HBRUSH)COLOR_WINDOW;
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = TEXT("0xCC");
wndclass.hIconSm = NULL;
if (!RegisterClassEx(&wndclass))
{
MessageBox(NULL, TEXT("RegisterClassEx Failed"), TEXT("Error"), MB_OK);
return 1;
}
// 创建窗口
HWND hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
TEXT("0xCC"),
TEXT("CreateWindowEx"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
960,
540,
NULL,
NULL,
hInstance,
NULL
);
if (hwnd == NULL)
{
MessageBox(NULL, TEXT("CreateWindowEx Failed"), TEXT("Error"), MB_OK);
return 1;
}
// 显示窗口
ShowWindow(hwnd, SW_SHOWDEFAULT);
// 更新窗口
UpdateWindow(hwnd);
// 消息处理
MSG msg = { 0 };
while (GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
// 窗口过程
LRESULT CALLBACK MainWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static HWND hButton1, hButton2, hButton3, hButton4, hButton5, hCheck, hRADIOBUTTON1, hRADIOBUTTON2;
switch (uMsg)
{
case WM_CREATE:
{
//创建控件
hButton1 = CreateWindow(
TEXT("BUTTON"),
TEXT("BS_PUSHBUTTON1"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON ,
10, 10,
150, 40,
hwnd,
(HMENU)ID_BUTTON_PUSH1,
ghInstance,
NULL);
hButton2 = CreateWindow(
TEXT("BUTTON"),
TEXT("BS_PUSHBUTTON2"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
170, 10,
150, 40,
hwnd,
(HMENU)ID_BUTTON_PUSH2,
ghInstance,
NULL);
hButton3 = CreateWindow(
TEXT("BUTTON"),
TEXT("BS_PUSHBUTTON3"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_BITMAP,
330, 10,
150, 40,
hwnd,
(HMENU)ID_BUTTON_PUSH3,
ghInstance,
NULL);
hButton4 = CreateWindow(
TEXT("BUTTON"),
TEXT("BS_PUSHBUTTON4"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
170, 60,
150, 40,
hwnd,
(HMENU)ID_BUTTON_PUSH4,
ghInstance,
NULL);
hButton5 = CreateWindow(
TEXT("BUTTON"),
TEXT("BS_PUSHBUTTON5"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
330, 60,
150, 40,
hwnd,
(HMENU)ID_BUTTON_PUSH5,
ghInstance,
NULL);
hCheck = CreateWindow(
TEXT("BUTTON"),
TEXT("CHECKBOX"),
WS_CHILD | WS_VISIBLE | BS_CHECKBOX,
10, 60,
150, 40,
hwnd,
(HMENU)ID_BUTTON_CHECKBOX1,
ghInstance,
NULL);
hRADIOBUTTON1 = CreateWindow(
TEXT("BUTTON"),
TEXT("RADIOBUTTON1"),
WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,
10, 110,
150, 40,
hwnd,
(HMENU)ID_BUTTON_RADIOBUTTON1,
ghInstance,
NULL);
hRADIOBUTTON2 = CreateWindow(
TEXT("BUTTON"),
TEXT("RADIOBUTTON2"),
WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,
10, 160,
150, 40,
hwnd,
(HMENU)ID_BUTTON_RADIOBUTTON2,
ghInstance,
NULL);
//初始控件
SendMessage(hCheck, BM_SETCHECK, BST_CHECKED, 0);
SendMessage(hRADIOBUTTON1, BM_SETCHECK, BST_CHECKED, 0);
HBITMAP hBitmap = LoadBitmap((HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), MAKEINTRESOURCE(IDB_BITMAP1));
if (hBitmap != NULL)
{
SendMessage(hButton3, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBitmap);
}
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case ID_BUTTON_PUSH1:
{
MessageBox(NULL, TEXT("ID_BUTTON_PUSH1"), NULL, NULL);
break;
}
case ID_BUTTON_PUSH2:
{
SendMessage(hButton1, BM_CLICK, 0, 0);
break;
}
case ID_BUTTON_CHECKBOX1:
{
BOOL ret = SendMessage(hCheck, BM_GETCHECK, 0, 0) == BST_CHECKED;
HWND hChe = GetDlgItem(hwnd, ID_BUTTON_CHECKBOX1);
SendMessage(hChe, BM_SETCHECK, ret ? BST_UNCHECKED : BST_CHECKED, 0);
break;
}
case ID_BUTTON_RADIOBUTTON1:
case ID_BUTTON_RADIOBUTTON2:
{
SendMessage(hRADIOBUTTON1, BM_SETCHECK, BST_UNCHECKED, 0);
SendMessage(hRADIOBUTTON2, BM_SETCHECK, BST_UNCHECKED, 0);
SendMessage((HWND)lParam, BM_SETCHECK, BST_CHECKED, 0);
//SendMessage(GetDlgItem(hwnd, LOWORD(wParam)), BM_SETCHECK, BST_CHECKED, 0);
break;
}
case ID_BUTTON_PUSH5:
{
SendMessage(hButton4, BM_SETSTYLE, BS_CHECKBOX, TRUE);
break;
}
}
}
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}