#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    static TCHAR szAppName[] = TEXT("HelloWnd");
    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 = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szAppName;

    if (!RegisterClass(&wndclass))
    {
        MessageBox(NULL, TEXT("This program requires windows NT."), szAppName, MB_ICONERROR);
        return 0;
    }

    hwnd = CreateWindow(szAppName, //窗口类名称
        TEXT("The Hello Program"), //窗口标题
        WS_OVERLAPPEDWINDOW, //窗口风格(窗口样式)
        CW_USEDEFAULT, //初始X坐标
        CW_USEDEFAULT, //初始Y坐标
        CW_USEDEFAULT, //初始X轴的的尺寸
        CW_USEDEFAULT, //初始Y轴的尺寸
        NULL, //父窗口句柄
        NULL, //窗口菜单句柄
        hInstance, 程序实例句柄
        NULL); //创建参数
    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);

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

    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wPram, LPARAM lPram)
{
    HDC hdc;
    PAINTSTRUCT ps;
    RECT rect;

    switch (message)
    {
    case WM_CREATE:
        PlaySound(TEXT("hellownd.wav"), NULL, SND_FILENAME| SND_ASYNC);
        return 0;
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        GetClientRect(hwnd, &rect);
        DrawText(hdc, TEXT("Hello Windows 11"), -1, &rect, DT_SINGLELINE | DT_CENTER| DT_VCENTER);
        EndPaint(hwnd, &ps);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }

    return DefWindowProc(hwnd, message, wPram, lPram);
}

WNDCLASS结构体说明

typedef struct tagWNDCLASSA {
    UINT        style;
    WNDPROC     lpfnWndProc;
    int         cbClsExtra;
    int         cbWndExtra;
    HINSTANCE   hInstance;
    HICON       hIcon;
    HCURSOR     hCursor;
    HBRUSH      hbrBackground;
    LPCSTR      lpszMenuName;
    LPCSTR      lpszClassName;
} WNDCLASSA, *PWNDCLASSA, NEAR *NPWNDCLASSA, FAR *LPWNDCLASSA;
参数类型 参数名 说明
UINT style  Specifies the class style(s). This member can be any combination of the Class Styles
WNDPROC lpfnWndProc  Pointer to the window procedure. You must use the CallWindowProc function to call the window procedure. For more information, see WindowProc
int cbClsExtra  Specifies the number of extra bytes to allocate following the window-class structure. The system initializes the bytes to zero
int cbWndExtra  Specifies the number of extra bytes to allocate following the window instance. The system initializes the bytes to zero. If an application uses WNDCLASS to register a dialog box created by using the CLASS directive in the resource file, it must set this member to DLGWINDOWEXTRA
HINSTANCE hInstance  Handle to the instance that contains the window procedure for the class
HICON hIcon  Handle to the class icon. This member must be a handle to an icon resource. If this member is NULL, the system provides a default icon
HCURSOR hCursor  Handle to the class cursor. This member must be a handle to a cursor resource. If this member is NULL, an application must explicitly set the cursor shape whenever the mouse moves into the application’s window
HBRUSH hbrBackground  Handle to the class background brush. This member can be a handle to the physical brush to be used for painting the background, or it can be a color value. A color value must be one of the following standard system colors (the value 1 must be added to the chosen color). If a color value is given, you must convert it to one of the following HBRUSH types:
 The system automatically deletes class background brushes when the class is unregistered by using UnregisterClass. An application should not delete these brushes.
 When this member is NULL, an application must paint its own background whenever it is requested to paint in its client area. To determine whether the background must be painted, an application can either process the WM_ERASEBKGND message or test the fErase member of the PAINTSTRUCT structure filled by the BeginPaint function
LPCSTR lpszMenuName  Pointer to a null-terminated character string that specifies the resource name of the class menu, as the name appears in the resource file. If you use an integer to identify the menu, use the MAKEINTRESOURCE macro. If this member is NULL, windows belonging to this class have no default menu.
LPCSTR lpszClassName  Pointer to a null-terminated string or is an atom. If this parameter is an atom, it must be a class atom created by a previous call to the RegisterClass or RegisterClassEx function. The atom must be in the low-order word of lpszClassName; the high-order word must be zero.
 If lpszClassName is a string, it specifies the window class name. The class name can be any name registered with RegisterClass or RegisterClassEx, or any of the predefined control-class names.
 The maximum length for lpszClassName is 256.
  • Class Styles
Style 说明
CS_BYTEALIGNCLIENT  在字节边界上(x方向上)定位窗口的用户区域的位置
CS_BYTEALIGNWINDOW  在字符边界上(x方向上)定位窗口的位置
CS_CLASSDC  该窗口类的所有窗口实例都共享一个窗口类DC
CS_DBLCLKS  当用户光标在窗口内双击时,允许发送双击消息给窗口过程
CS_DROPSHADOW  Windows XP:在窗口上启用阴影效果。通过SPI_SETDROPSHADOW打开和关闭效果
CS_GLOBALCLASS  1.当调用CreateWindow或CreateWindowEx函数来创建窗口时允许它的hInstance参数和注册窗口类时传递给RegisterClass的hInstace参数不同.
 2.如果不指定该风格,则这两个hInstace必须相同
CS_HREDRAW  移动或者调整窗口的宽度(水平方向)时,重绘整个窗口
CS_NOCLOSE  禁止系统菜单的关闭选项
CS_OWNDC  给予每一个窗口实例分配一个唯一的DC(注意,尽管这样很方便,但它必须慎重使用,因为每一个DC大约要占800个字节的内存)
CS_PARENTDC  1.将子窗口的裁剪区域设置到父窗口的DC中去,这样子窗口便可以在父窗口上绘制自身。(注意:这是子窗口从系统缓存中获取DC,而不是使用父窗口的DC。)
 2.指定该分格可以提高系统性能
CS_SAVEBITS  1.以位图形式保存被该窗口遮挡的屏幕部分,当该窗口移动以后,系统便可以用该保存的位图恢复屏幕移动的相应部分,从而系统不用向被该窗口遮挡的窗口发送WM_PAINT消息。
 2.该特性对于菜单类型的窗口比较合适,因为它通常是简短的显示一下之后便消失。
 3.设置该特性将增加显示该窗口的时间,因为它通常要先分配保存位图的内存。
CS_VREDRAW  移动或者调整窗口的高度(垂直方向)时,重绘整个窗口
  • HBRUSH
type 说明
COLOR_ACTIVEBORDER
COLOR_ACTIVECAPTION
COLOR_APPWORKSPACE
COLOR_BACKGROUND
COLOR_BTNFACE
COLOR_BTNSHADOW
COLOR_BTNTEXT
COLOR_CAPTIONTEXT
COLOR_GRAYTEXT
COLOR_HIGHLIGHT
COLOR_HIGHLIGHTTEXT
COLOR_INACTIVEBORDER
COLOR_INACTIVECAPTION
COLOR_MENU
COLOR_MENUTEXT
COLOR_SCROLLBAR
COLOR_WINDOW
COLOR_WINDOWFRAME
COLOR_WINDOWTEXT

Windfows常见的标识符缩写说明

前缀 常量说明
CS 类风格选项
CW 创建窗口选项
DT 文本绘制选项
IDI 图标的ID号
IDC 光标的ID号
MB 消息框选项
SND 声音选项
WM 窗口消息
WS 窗口风格
BS 滚动条通知码