在简单的窗体基础上加了按钮
加了Button的代码
#include <windows.h>
int num = 1;
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//button
switch (message) {
case WM_CREATE: {
CreateWindow(
TEXT("button"), TEXT("按钮"),
WS_CHILD | WS_VISIBLE,
0, 0, 100, 50,
hwnd, (HMENU)1, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
num = 1;
}
case WM_COMMAND: {
if (num == 0)MessageBox(NULL, "点击成功1", "", MB_OK);
num = 0;
break;
}
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
}
BOOL RegisterWindow(HINSTANCE hInstance)
{
WNDCLASSEX wnd;
wnd.hbrBackground = (HBRUSH)COLOR_WINDOW;
wnd.lpfnWndProc = WndProc;
wnd.style = CS_HREDRAW | CS_VREDRAW;
wnd.lpszClassName = TEXT("WindowClass");
wnd.hInstance = hInstance;
wnd.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wnd.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);
wnd.hCursor = LoadCursor(hInstance, IDC_ARROW);
wnd.cbSize = sizeof(WNDCLASSEX);
wnd.lpszMenuName = nullptr;
wnd.cbClsExtra = 0;
wnd.cbWndExtra = 0;
return RegisterClassEx(&wnd);
}
BOOL DisplayWindow(HWND& hwnd, HINSTANCE hInstance, HMENU hMenu, int nCmdShow)
{
hwnd = CreateWindow(TEXT("WindowClass"), TEXT("TweeChaice"), WS_OVERLAPPEDWINDOW, 0, 0, 400, 300, nullptr, nullptr, hInstance, nullptr);
ShowWindow(hwnd, nCmdShow);
return hwnd ? TRUE : FALSE;
}
HWND hwnd;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
if (!RegisterWindow(hInstance))
{
MessageBox(nullptr, TEXT("Error register!"), TEXT("Error"), MB_ICONERROR | MB_OK);
return -1;
}
if (!DisplayWindow(hwnd, hInstance, nullptr, nCmdShow))
{
MessageBox(nullptr, TEXT("Error creating window!"), TEXT("Error"), MB_ICONERROR | MB_OK);
return -1;
}
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)>0){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
还有另一个简化的代码
#include <windows.h>
int num = 1;
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch (Message) {
case WM_CREATE: {
CreateWindow(
TEXT("button"),TEXT("按钮"),
WS_CHILD | WS_VISIBLE,
0, 0,100, 50,
hwnd,(HMENU)1,((LPCREATESTRUCT)lParam)->hInstance,NULL);
num = 1;
}
case WM_COMMAND: {
if (num == 0)MessageBox(NULL, "点击成功1", "", MB_OK);
num = 0;
break;
}
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
memset(&wc, 0, sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, "WindowClass", "Button Style", WS_VISIBLE | WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
640,480,
NULL, NULL, hInstance, NULL);
if (hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
button的相关内容资料