我正在尝试为 WINAPI 创建一个窗口创建者类。
我一直在弄清楚如何让窗口动态添加控件并为它们注册消息。
#include <windows.h>
#include <iostream>
#include <vector>
#include <tuple>
#include <thread>
using namespace std;
class WinForm
{
    private:
        HWND WindowHandle = nullptr;
        std::thread Thread;
        std::vector<std::tuple<std::string, std::size_t, HWND>> ControlHandles;
    public:
        ~WinForm();
        WinForm(std::string ClassName, std::string WindowName, bool Threaded = false, int Width = CW_USEDEFAULT, int Height = CW_USEDEFAULT, WNDPROC WindowProcedure = nullptr, WNDCLASSEX WndClass = {0});
        bool AddButton(std::string ButtonName, POINT Location, int Width, int Height);
};
WinForm::~WinForm()
{
    if (Thread.joinable())
    {
        Thread.join();
    }
}
WinForm::WinForm(std::string ClassName, std::string WindowName, bool Threaded, int Width, int Height, WNDPROC WindowProcedure, WNDCLASSEX WndClass)
{
    if (WindowProcedure == nullptr)
    {
        WindowProcedure = [](HWND window, UINT msg, WPARAM wp, LPARAM lp) -> LRESULT __stdcall
        {
            switch(msg)
            {
                case WM_PAINT:
                break;
                case WM_DESTROY:
                    PostQuitMessage(0);
                    return 0;
                default:
                    return DefWindowProc(window, msg, wp, lp);
            }
            return 0;
        };
    }
    if (WndClass.cbSize == 0)
    {
        WndClass =
        {
            sizeof(WNDCLASSEX), CS_DBLCLKS, WindowProcedure,
            0, 0, GetModuleHandle(nullptr), LoadIcon(nullptr, IDI_APPLICATION),
            LoadCursor(nullptr, IDC_ARROW), HBRUSH(COLOR_WINDOW+1),
            nullptr, ClassName.c_str(), LoadIcon (nullptr, IDI_APPLICATION)
        };
    }
    if (RegisterClassEx(&WndClass))
    {
        if (Threaded)
        {
            Thread = std::thread([ClassName, WindowName, Width, Height, this]{
                WindowHandle = CreateWindowEx(0, ClassName.c_str(), WindowName.c_str(), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, Width, Height, nullptr, nullptr, GetModuleHandle(nullptr), nullptr);
                if(WindowHandle)
                {
                    MSG msg = {nullptr};
                    ShowWindow(WindowHandle, SW_SHOWDEFAULT);
                    while(GetMessage(&msg, nullptr, 0, 0))
                    {
                        TranslateMessage(&msg);
                        DispatchMessage(&msg);
                    }
                }
            });
        }
        else
        {
            WindowHandle = CreateWindowEx(0, ClassName.c_str(), WindowName.c_str(), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, Width, Height, nullptr, nullptr, GetModuleHandle(nullptr), nullptr);
            if(WindowHandle)
            {
                MSG msg = {nullptr};
                ShowWindow(WindowHandle, SW_SHOWDEFAULT);
                while(GetMessage(&msg, nullptr, 0, 0))
                {
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);
                }
            }
        }
    }
}
bool WinForm::AddButton(std::string ButtonName, POINT Location, int Width, int Height)
{
    for (std::vector<std::tuple<std::string, std::size_t, HWND>>::iterator it = ControlHandles.begin(); it != ControlHandles.end(); ++it)
    {
        if (ButtonName == std::get<0>(*it))
        {
            return false;
        }
    }
    std::size_t ID = 1;
    for (std::vector<std::tuple<std::string, std::size_t, HWND>>::iterator it = ControlHandles.begin(); it != ControlHandles.end(); ++it, ++ID)
    {
        if (std::get<1>(*it) != ID)
        {
            break;
        }
    }
    HWND ButtonHandle = CreateWindowEx(0, "Button", ButtonName.c_str(), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, Location.x, Location.y, Width, Height, WindowHandle, (HMENU)ID, GetModuleHandle(nullptr), nullptr);
    ControlHandles.push_back(std::make_tuple(ButtonName, ID, ButtonHandle));
    SendMessage(WindowHandle, WM_CREATE, 0, 0);
    return true;
}
int main()
{
    WinForm Form("Class", "Title", true);
    Form.AddButton("NewButton", {50, 50}, 25, 25);
}
在上面,它编译得很好并且显示窗口很好。它只是没有显示我试图动态添加到窗口的按钮。有谁知道如何将按钮动态添加到窗口并允许按钮注册消息?