白季飞龙的个人主页

Win32开发大杂烩

CMakeLists.txt

cmake_minimum_required(VERSION 3.8)
project(untitled1)

set(CMAKE_C_STANDARD 99)

set(SOURCE_FILES main.c)

add_executable(untitled1 ${SOURCE_FILES})

main.c

// HelloWindow: 你好视窗
// BaiJiFeiLong@163.com
// 2015-04-17 12:51

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
    static TCHAR szAppName[] = TEXT("HelloWindow");

    // 声明窗体、消息与窗体类
    HWND hWnd;
    MSG msg;
    WNDCLASS wndClass;

    // 配置窗体类
    wndClass.style = CS_HREDRAW | CS_VREDRAW;
    wndClass.lpfnWndProc = WndProc;
    wndClass.cbClsExtra = 0;
    wndClass.cbWndExtra = 0;
    wndClass.hInstance = hInstance;
    wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndClass.hbrBackground = GetStockObject(WHITE_BRUSH);
    wndClass.lpszMenuName = NULL;
    wndClass.lpszClassName = szAppName;

    // 注册窗体类
    if (!RegisterClass(&wndClass)) {
        MessageBox(NULL, TEXT("此程序需要运行于NT系统!"), szAppName, MB_ICONERROR);
        return 0;
    }

    // 创建窗体
    hWnd = CreateWindow(szAppName,
                        TEXT("你好视窗"),
                        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;
}

// 实现窗体消息处理程序
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    HDC hdc;
    PAINTSTRUCT ps;
    RECT rect;

    switch (message) {
        case WM_PAINT:
            hdc = BeginPaint(hWnd, &ps);
            GetClientRect(hWnd, &rect);

            DrawText(hdc, TEXT("你好,视窗Windows98!"), -1, &rect,
                     DT_SINGLELINE | DT_CENTER | DT_VCENTER);
            EndPaint(hWnd, &ps);
            return 0;

        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;

        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
}

注意


漫漫路,莫论逍遥;潜心修,只为悟道