Win32 developers: create an application window

First, create an application window

code show as below:

// 头文件
#include <windows.h>
 
// 全局变量
WCHAR g_lpszClassName[] = L"CLASSNAME";
WCHAR g_lpszWindowName[] = L"哈喽,新的征程";
 
// 函数声明
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
 
// 应用程序主函数
INT APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, INT nCmdShow)
{
    // 1.设计一个窗口类
    WNDCLASSEX wcex;
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_HREDRAW | CS_VREDRAW;        
    wcex.cbClsExtra = 0;    
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    wcex.lpfnWndProc = WndProc;
    wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName = g_lpszClassName;
    wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
 
    // 2.注册这个窗口类
    if (RegisterClassEx(&wcex) == ((ATOM)0))
    {
        MessageBox(NULL, L"注册窗口类失败!", L"错误", MB_YESNO | MB_ICONERROR);
        exit(-1);
    }
 
    // 3.创建窗口
    HWND hWnd = CreateWindowEx(NULL, g_lpszClassName, g_lpszWindowName, WS_OVERLAPPEDWINDOW, 10, 10, 800, 800, NULL, NULL, hInstance, NULL);
    if (hWnd == NULL)
    {
        MessageBox(NULL, L"创建窗口失败!", L"错误", MB_YESNO | MB_ICONERROR);
        exit(-1);
    }
 
    // 4.更新和显示窗口
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);
 
    // 5.应用程序消息循环
    MSG msg = { 0 };
    BOOL bRet;
    // 调用 GetMessage 函数从调用线程的消息队列中取得一个消息
    while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
    {
        if (bRet != -1)
        {
            TranslateMessage(&msg); // 将虚拟键消息转换为字符消息
            DispatchMessage(&msg); // 将消息分发到窗口处理
        }
    }
 
    return msg.wParam;
}
 
// 应用程序消息处理回调函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_DESTROY:
        PostQuitMessage(0); // 程序退出
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam); // 默认的窗口处理程序
    }
    return 0;
}


Second, program analysis

1. First design a window class.

I do not understand can look at this article: the Win32 application development: window class structure WNDCLASS and WNDCLASSEX  , here is not to go into details.


2. Register the window class previously designed.

RegisterClassEx function call registration window. Prototype:

// 返回值(ATOM,原子):成功返回注册的原子,失败返回 0。
ATOM WINAPI RegisterClassExW(_In_ CONST WNDCLASSEXW *); // 参数:为我们设计的窗口类


3. Create window.

Call CreateWindowEx function to create the window. Prototype:

// 返回值:创建成功返回创建的窗口句柄,失败返回NULL
HWND WINAPI CreateWindowExW(
    _In_ DWORD dwExStyle, // 窗口扩展风格
    _In_opt_ LPCWSTR lpClassName, // 我们注册的窗口类的名称
    _In_opt_ LPCWSTR lpWindowName, // 窗口的名称(说白了就是窗口的标题)
    _In_ DWORD dwStyle, // 窗口创建的风格(跟dwExStyle不同)
    _In_ int X, // 窗口左上角的点位于屏幕的横坐标
    _In_ int Y, // 窗口左上角的点位于屏幕的纵坐标
    _In_ int nWidth, // 窗口的宽度
    _In_ int nHeight, // 窗口的高度
    _In_opt_ HWND hWndParent, // 窗口所有者的句柄
    _In_opt_ HMENU hMenu, // 菜单句柄
    _In_opt_ HINSTANCE hInstance, // 所在模块的实例句柄
    _In_opt_ LPVOID lpParam); // 在WM_CREATE中进行传递的参数


4. Update and display window.

Call the ShowWindow function to display the window. Prototype:

// 返回值:成功返回非0,失败返回0
BOOL WINAPI ShowWindow(
    _In_ HWND hWnd, // 显示的窗口句柄
    _In_ int nCmdShow); // 窗口显示的方式(全屏,最大化窗口,最小化窗口,······)

Call function update UpdateWindow window. Prototype:

// 返回值:成功返回非0,失败返回0
BOOL WINAPI UpdateWindow(
    _In_ HWND hWnd); // 更新的窗口


The application message loop.

Calls GetMessage function to get a message from the calling thread's message queue. Prototype:

// 返回值:获取错误返回-1,消息为WM_QUIT返回0,其他为非0
BOOL WINAPI GetMessageW(
    _Out_ LPMSG lpMsg, // 接收从消息队列中获取的消息
    _In_opt_ HWND hWnd, // 接收消息的窗口句柄
    _In_ UINT wMsgFilterMin, // 指定被检测的最小消息值
    _In_ UINT wMsgFilterMax); // 指定被检测的最大消息值

TranslateMessage function call virtual key messages into character messages. Prototype:

// 返回值:成功返回非0,失败返回0
BOOL WINAPI TranslateMessage(
    _In_ CONST MSG *lpMsg); // 接收的消息

DispatchMessage call function to distribute the message to the window handle. Prototype:

// 返回值:窗口处理过程的返回值
LRESULT WINAPI DispatchMessageW(
    _In_ CONST MSG *lpMsg); // 分发的消息

Example of use:

while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
{
    if (bRet != -1)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

End of while loop, when the acquired message GetMessage is WM_QUIT exit window, return 0 (false) is returned when the GetMessage value is not -1 (acquisition failure message) when and virtual conversion key distribution message.


6. The window message handling process.

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_DESTROY:
        PostQuitMessage(0); // 程序退出
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam); // 默认的窗口处理程序
    }
 
    return 0;
}

parameter:

  • hWnd: Window handle Window treatment process

  • message: Message ID

  • wParam: Additional information

  • lParam: additional messages


reference:

Win32 developers: create an application window


Guess you like

Origin www.cnblogs.com/linuxAndMcu/p/12075434.html