我的主 window 是否在创建时或单击更新区域时收到编辑控件?

Does my main window receive edit controls upon creation or when I click on the update region?

我是 Win32 的新手,所以请保持温和,因为我肯定没有很好地描述我的问题,但我非常认真地想了解更多。所以,我修改了我的 main window 以包括编辑、写入和删除文本的选项,但是为了继续前进,我需要知道 main window 到底知道在哪里创建编辑控件。

CreateWindowEx 的编辑控件创建部分是否创建主 window?或者它们是否仅在我单击更新区域时创建,然后通知我的 WM_CREATE 案例开始第二个 CreateWindowEx 实际上包括启用控件的编辑样式?这是我可以让事物的顺序有意义的唯一方法,但我需要确保我不只是在第一个 "logical" 结论下跳转。我的理论是正确的还是完全错误的?

#include <Windows.h>
#include "resource.h"


LPCWSTR g_szClassName{ L"My Window Class" };

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {
    case WM_CREATE:
    {
        HFONT hfDefault;
        HWND hEdit;

        hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, L"Edit", L"",
            WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL |
            ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
            0, 0, 100, 100,
            hwnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);
        if (hEdit == NULL) {
            MessageBox(hwnd, L"Could not create edit box.", L"Error!", MB_OK | MB_ICONERROR);
        }

        hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
        SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));

    break;
    }
    case WM_SIZE:
    {
        HWND hEdit;
        RECT rcClient;

        GetClientRect(hwnd, &rcClient);

        hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
        SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER);

    break;
    }
    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 32, 32, 0);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
    wc.lpszClassName = g_szClassName;
    wc.hIconSm = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 32, 32, 0);

    if (!RegisterClassEx(&wc)) {
        MessageBox(NULL, L"Windows registration failed!", L"Error!",
            MB_ICONEXCLAMATION | MB_OK);
    }

    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        L"The title of my window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, hInstance, NULL);

    if (hwnd == NULL) {
        MessageBox(NULL, L"Windows registration failed!", L"Error!",
            MB_ICONEXCLAMATION | MB_OK);
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

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

    return Msg.wParam;
}

CreateWindow/Ex() 退出之前,它向正在创建的 window 发送一条 WM_CREATE 消息(以及其他几条消息)。 CreateWindow/Ex() 在处理所有这些消息之前不会退出(除非发生错误)。

因此,当您的 main() 中的 CreateWindowEx() 退出时,您的主 window 已完全创建,包括其 WM_CREATE 处理程序创建的所有子控件.