【问题标题】:Setting Checkbox Options C++ [closed]设置复选框选项 C++ [关闭]
【发布时间】:2018-07-02 17:51:26
【问题描述】:

我有以下代码,用 C++ 编写,而不是只有一个复选框说“显示标题”,我希望能够为不同/更多复选框传递多个字符串。如何为多个复选框提供 CreateWindowW 函数多个字符串?以及 CreateWindowW 函数应该在 Select 函数中修改还是在 WndProc 函数中修改?

void Select(vector<string>& ret)
{
    HINSTANCE hInstance = NULL; //NULL = the current process
    WNDCLASSW wc = { 0 };
    MSG  msg;
    wc.lpszClassName = L"Check Box";
    wc.hInstance = hInstance;
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wc.lpfnWndProc = WndProc;
    wc.hCursor = LoadCursor(0, IDC_ARROW);

    RegisterClassW(&wc);
    CreateWindowW(wc.lpszClassName, L"Check Box",
        WS_OVERLAPPEDWINDOW | WS_VISIBLE,
        150, 150, 230, 150, 0, 0, hInstance, 0);

    while (GetMessage(&msg, NULL, 0, 0)) {

        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    //return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
    WPARAM wParam, LPARAM lParam) {

    bool checked = true;

    switch (msg) {

    case WM_CREATE:
        CreateWindowW(L"button", L"Show Title",
            WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
            20, 20, 185, 35, hwnd, (HMENU)1,
            NULL, NULL);
        CheckDlgButton(hwnd, 1, BST_CHECKED);
        break;
    case WM_COMMAND:
        checked = IsDlgButtonChecked(hwnd, 1);
        if (checked) {

            CheckDlgButton(hwnd, 1, BST_UNCHECKED);
            SetWindowTextW(hwnd, L"");
        }
        else {
            CheckDlgButton(hwnd, 1, BST_CHECKED);
            SetWindowTextW(hwnd, L"Check Box");
        }
        break;
    case WM_DESTROY:

        PostQuitMessage(0);
        break;
    }
    return DefWindowProcW(hwnd, msg, wParam, lParam);
}

编辑:这是新代码

void columnSelect(vector<string>& ret)
{
    HINSTANCE hInstance = NULL; //NULL = the current process
    WNDCLASSW wc = { 0 };
    MSG  msg;
    wc.lpszClassName = L"Check Box";
    wc.hInstance = hInstance;
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wc.lpfnWndProc = WndProc;
    wc.hCursor = LoadCursor(0, IDC_ARROW);

    RegisterClassW(&wc);
    CreateWindowW(wc.lpszClassName, L"Check Box",
        WS_OVERLAPPEDWINDOW | WS_VISIBLE,
        150, 150, 230, 150, 0, 0, hInstance, &ret);

    while (GetMessage(&msg, NULL, 0, 0)) {

        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    //return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
    WPARAM wParam, LPARAM lParam) {

    bool checked = true;

    switch (msg) {

    case WM_CREATE: {
        LPCREATESTRUCT lpcs = reinterpret_cast<LPCREATESTRUCT>(lParam);
        vector<string> *strings = reinterpret_cast<vector<string>*>(lpcs->lpCreateParams);
        for (int i = 0; i != strings->size(); i++)
        {
            CreateWindowA("button", (*strings)[i].c_str(),
                WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
                20, 20, 185, 35, hwnd, (HMENU)1,
                NULL, NULL);
            CheckDlgButton(hwnd, 1, BST_CHECKED);
        }


        break;
    }
    case WM_COMMAND: {
        checked = IsDlgButtonChecked(hwnd, 1);
        if (checked) {

            CheckDlgButton(hwnd, 1, BST_UNCHECKED);
            SetWindowTextW(hwnd, L"");
        }
        else {
            CheckDlgButton(hwnd, 1, BST_CHECKED);
            SetWindowTextW(hwnd, L"Check Box");
        }
        break;
    }
    case WM_DESTROY: {

        PostQuitMessage(0);
        break;
    }
    }
    return DefWindowProcW(hwnd, msg, wParam, lParam);
}

【问题讨论】:

    标签: c++ visual-studio winapi checkbox


    【解决方案1】:

    如何为CreateWindowW 函数提供多个复选框的多个字符串?

    CreateWindow() 每次调用只能创建 1 个窗口/控件。您必须手动拆分字符串,然后为每个复选框分别调用 CreateWindow()

    假设您的vector&lt;string&gt; 包含复选框字符串,您可以通过CreateWindow()lpParam 参数将vector 传递到您的窗口,然后在您的WM_CREATE 消息处理程序中访问它,例如:

    void Select(vector<string>& ret)
    {
        ...
        CreateWindowW(..., &ret);
        ...
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
        switch (msg) {
            case WM_CREATE: {
                LPCREATESTRUCT lpcs = reinterpret_cast<LPCREATESTRUCT>(lParam);
                vector<string> *strings = reinterpret_cast<vector<string>*>(lpcs->lpCreateParams);
                // use strings as needed ...
                break;
            }
            ...
        }
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-02
      • 2016-01-01
      • 1970-01-01
      • 2011-11-28
      • 1970-01-01
      • 2020-01-09
      • 2011-10-07
      • 2013-03-21
      • 1970-01-01
      相关资源
      最近更新 更多