#include <Windows.h>
#include <iostream>LPCTSTR clsName = "MyProgram";
LPCTSTR msgName = "test";LRESULT CALLBACK MyWinPro(HWND unnamedParam1,UINT unnamedParam2,WPARAM unnamedParam3,LPARAM unnamedParam4
);int WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{// 定义窗口配置对象WNDCLASS wndCls;wndCls.cbClsExtra = NULL;wndCls.cbWndExtra = NULL;wndCls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);wndCls.hCursor = LoadCursor(NULL, IDC_ARROW);wndCls.hIcon = LoadIcon(NULL, IDI_APPLICATION);wndCls.hInstance = hInstance;// 定义交互响应wndCls.lpfnWndProc = MyWinPro;wndCls.lpszClassName = clsName;wndCls.lpszMenuName = NULL;wndCls.style = CS_HREDRAW | CS_VREDRAW;// 注册窗口类RegisterClass(&wndCls);// 创建窗口HWND hwnd;hwnd = CreateWindow(clsName, msgName, WS_OVERLAPPEDWINDOW, 200, 300, 500, 400, NULL, NULL, hInstance, NULL);// 显示和刷新ShowWindow(hwnd, SW_SHOWNORMAL);UpdateWindow(hwnd);// 消息循环MSG msg;while (GetMessage(&msg, NULL, NULL, NULL)){TranslateMessage(&msg); // 将按键消息转换为字符消息DispatchMessage(&msg);}return msg.wParam;
}LRESULT CALLBACK MyWinPro(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam
)
{// uMsg消息类型int ret;HDC hdc;PAINTSTRUCT ps;switch (uMsg){case WM_CHAR:char szChar[20];sprintf_s(szChar, "你刚才按下了: %c", wParam);MessageBox(hwnd, szChar, "hint", NULL);break;case WM_LBUTTONDOWN:MessageBox(hwnd, "按下鼠标左键", "hint", NULL);break;case WM_PAINT:hdc = BeginPaint(hwnd, &ps);TextOut(hdc, 0, 0, "sd4sa5d45s", strlen("sd4sa5d45s"));EndPaint(hwnd, &ps);MessageBox(hwnd, "重绘", "hint", NULL);break;case WM_CLOSE:ret = MessageBox(hwnd, "确定退出?", "msg", MB_YESNO);if (ret == IDYES)DestroyWindow(hwnd);break;case WM_DESTROY:PostQuitMessage(0);break;default:return DefWindowProc(hwnd, uMsg, wParam, lParam); // 调用Windows提供的窗口过程}return 0;
}
运行结果图: