#include <Windows.h> #include <stdio.h> LRESULT CALLBACK WinProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); BOOL RegisterWndClass(HINSTANCE hIntance, const char* szClass); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) { HWND hwnd = NULL; static char szAppName[] = "WinTest"; if (!RegisterWndClass(hInstance, szAppName)) { MessageBox(NULL, "注册窗口失败!", "创建窗口", MB_OK); return 0; } hwnd = CreateWindow(szAppName, "测试窗口", WS_OVERLAPPEDWINDOW, 0, 0, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); if (NULL == hwnd) { MessageBox(NULL, "创建窗口失败", "创建窗口", MB_OK); return 0; } ShowWindow(hwnd, SW_SHOWNORMAL); UpdateWindow(hwnd); MSG msg = {0}; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return int(msg.wParam); } BOOL RegisterWndClass(HINSTANCE hIntance, const char* szClass) { WNDCLASS wndClass; wndClass.cbClsExtra = 0; wndClass.cbWndExtra = 0; wndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); wndClass.hCursor = LoadCursor(NULL, IDC_CROSS); wndClass.hIcon = LoadIcon(NULL, IDI_ERROR); wndClass.hInstance = hIntance; wndClass.lpfnWndProc = WinProc; wndClass.lpszClassName = szClass; wndClass.lpszMenuName = NULL; wndClass.style = CS_HREDRAW | CS_VREDRAW; return ::RegisterClass(&wndClass); } LRESULT CALLBACK WinProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_CHAR: { char szChar[MAX_PATH] = {0}; sprintf(szChar, "您刚刚输入的字符ASSIC: %d", wParam); MessageBox(hwnd, szChar, "输入字符", MB_OK); } break; case WM_LBUTTONDOWN: { MessageBox(hwnd, "您按下了左键", "鼠标点击", MB_OK); HDC hdc = GetDC(hwnd); char *szText = "鼠标点击事件记录"; TextOut(hdc, 0, 50, szText, strlen(szText)); ReleaseDC(hwnd, hdc); } break; case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); char* szText = "我们的第一个Windows窗口"; TextOut(hdc, 0, 0, szText, strlen(szText)); EndPaint(hwnd, &ps); } break; case WM_CLOSE: if (IDYES == MessageBox(hwnd, "退出程序?", "退出", MB_YESNO)) { DestroyWindow(hwnd); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, uMsg, wParam, lParam); } return 0; }