Window API 创建窗体

本文介绍了一个简单的Windows程序框架实现过程,包括窗口类注册、实例初始化及消息循环等关键步骤,并展示了如何创建一个基本的窗口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <windows.h>

#define szWindowClass "MyWClass";


ATOM MyRegisterClass(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK MainProc(HWND, UINT, WPARAM, LPARAM);


int WINAPI WinMain(
                   HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nShowCmd
                   )
{
    MSG msg;

    MyRegisterClass(hInstance);

    InitInstance(hInstance, nShowCmd);
    
    


    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;

}


ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wce;

    wce.cbSize = sizeof(wce);
    wce.style = CS_HREDRAW|CS_VREDRAW;
    wce.lpfnWndProc = MainProc;
    wce.cbClsExtra = 0;
    wce.cbWndExtra = 0;
    wce.hInstance = hInstance;
    wce.hIcon = NULL;
    wce.hCursor = LoadCursor(NULL, IDC_ARROW);
    wce.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wce.lpszMenuName = NULL;
    wce.lpszClassName = szWindowClass;
    wce.hIconSm = NULL;

    return RegisterClassEx(&wce);
}

BOOL InitInstance(HINSTANCE hInstance, int nShowCmd)
{
    HWND hWnd;

    hWnd = CreateWindow(
                        "MyWClass",
                        "cocoDialog",
                        WS_SYSMENU,
                        150,
                        150,
                        800,
                        600,
                        NULL,
                        NULL,
                        hInstance,
                        NULL
                       );

    ShowWindow(hWnd, nShowCmd);
    UpdateWindow(hWnd);

    return TRUE;
}

LRESULT CALLBACK MainProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
    default :
        return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值