#include <Windows.h>
typedef WNDPROC WMPROC;
typedef struct{
UINT msg;
WMPROC wmProc;
}MsgWithProc;
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define WINDOW_TITLE L"按钮的创建"
HDC g_hdc = NULL;
HINSTANCE g_hInstance = NULL;
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT __stdcall WMcreate(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT __stdcall WMpaint(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT __stdcall WMcommand(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT __stdcall WMkeydown(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT __stdcall WMdestory(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){
g_hInstance = hInstance;
WNDCLASSEX wndClass = {0};
wndClass.cbSize = sizeof(WNDCLASSEX);
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = NULL;
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = L"Page36";
if(!RegisterClassEx(&wndClass)){
return -1;
}
HWND hwnd = CreateWindowEx(
NULL,
L"Page36",
WINDOW_TITLE,
WS_POPUP | WS_SYSMENU | WS_SIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT,
NULL, NULL, hInstance, NULL
);
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
MSG msg = {0};
while(msg.message != WM_QUIT){
if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
UnregisterClass(L"Page36", hInstance);
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
MsgWithProc msgWithProc[] = {
{WM_CREATE,WMcreate},
//{WM_PAINT,WMpaint},
{WM_COMMAND,WMcommand},
{WM_KEYDOWN,WMkeydown},
{WM_DESTROY,WMdestory}
};
for(MsgWithProc mwp : msgWithProc){
if(mwp.msg == message){
mwp.wmProc(hwnd, message, wParam, lParam);
return 0;
}
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
LRESULT __stdcall WMcreate(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
CreateWindow(
TEXT("button"),
TEXT("Hello"),
WS_CHILD | WS_VISIBLE,
20, 20, 50, 30,
hwnd,
(HMENU)10001,
g_hInstance, NULL
);
return 0;
}
LRESULT __stdcall WMpaint(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
PAINTSTRUCT paintStruct;
g_hdc = BeginPaint(hwnd, &paintStruct);
EndPaint(hwnd, &paintStruct);
ValidateRect(hwnd, NULL);
return 0;
}
LRESULT __stdcall WMcommand(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
WORD wHigh = HIWORD(wParam);
WORD wLow = LOWORD(wParam);
switch(wLow){
case 10001:
MessageBox(0, TEXT("按钮被点击"), 0, 0);
break;
}
return 0;
}
LRESULT __stdcall WMkeydown(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
switch(wParam){
case VK_F11:
{
HWND hDesk;
RECT rc;
hDesk = GetDesktopWindow();
GetWindowRect(hDesk, &rc);
SetWindowLong(hwnd, GWL_STYLE, WS_BORDER);
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, rc.right, rc.bottom, SWP_SHOWWINDOW);
break;
}
case VK_ESCAPE:
PostQuitMessage(0);
break;
}
return 0;
}
LRESULT __stdcall WMdestory(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
PostQuitMessage(0);
return 0;
}