#include <WINDOWS.H>
LRESULT CALLBACK HelloWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rt;
switch(uMsg)
{
case WM_PAINT:
{
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rt);
DrawText(hdc, "Hello Windows!", -1, &rt, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
EndPaint(hwnd,&ps);
}
break;
case WM_CLOSE:
{
if(IDOK == MessageBox(hwnd, "是否关闭窗口", "提示", MB_OK | MB_OKCANCEL))
{
DestroyWindow(hwnd); // 销毁窗口
}
}
break;
case WM_DESTROY:
{
PostQuitMessage(0); // 发送 WM_CLOSE, 形参 0 将会传递给 WPARAM
}
break;;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// 设计一个窗口类
WNDCLASS wndclass;
wndclass.style = CS_VREDRAW | CS_HREDRAW;
wndclass.lpfnWndProc = HelloWndProc; // 由windows系统调用
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_WINLOGO));
wndclass.hCursor = LoadCursor(NULL, MAKEINTRESOURCE(IDC_HELP));
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = "hellownd"; // 此处是指定窗口类名称,用于关联窗口,CreateWindow第一个参数
// 注册窗口类
RegisterClass(&wndclass);
// 创建窗口
HWND hwnd = CreateWindow("hellownd", "Hello Wnd", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
// 显示窗口
ShowWindow(hwnd, SW_SHOW);
// 更新窗口
UpdateWindow(hwnd);
// 消息循环
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnregisterClass("hellownd", hInstance);
return msg.wParam; // 来自与PostQuitMessage中的参数
}
Win32 窗口
最新推荐文章于 2022-05-11 08:53:56 发布