Windows程序设计之第一个基本窗口程序示例

#include <Windows.h>
#include <tchar.h>


// 窗口处理函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0); // 发送WM_QUIT消息,结束程序
        break;
    }
    return DefWindowProc(hWnd, uMsg, wParam, lParam); // 系统默认窗口处理函数
}

INT WINAPI _tWinMain(
    _In_ HINSTANCE hInstance,
    _In_opt_ HINSTANCE hPrevInstance,
    _In_ LPTSTR lpCmdLine,
    _In_ INT nShowCmd)
{
    WNDCLASS wc;
    HWND hWnd;
    MSG msg;

    // 注册窗口类
    ZeroMemory(&wc, sizeof(wc));
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = TEXT("BasicWindow");

    RegisterClass(&wc);
    
    // 创建窗口
    hWnd = CreateWindow(TEXT("BasicWindow"), TEXT("BasicWindow"),
        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
 
    // 显示窗口
    ShowWindow(hWnd, nShowCmd);
    UpdateWindow(hWnd);

    // 消息循环
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

Leave a Reply

Your email address will not be published.