【问题标题】:Is there a way I can hide a button when checkbox is unchecked?未选中复选框时,有没有办法隐藏按钮?
【发布时间】:2019-11-25 14:48:14
【问题描述】:

我正在制作一个程序,用户必须同意才能继续使用该程序。

用户必须选中复选框才能继续,如果用户选中复选框,则显示“运行”按钮,如果他取消选中复选框,则按钮隐藏。

我的程序有两个问题,当用户取消选中复选框时,“运行”按钮没有消失,第二个问题是当用户点击“运行”按钮时,我的程序认为用户单击复选框并选中或取消选中该复选框。

这是我的整个程序,如果你能帮助我,我会很高兴。 如果你愿意,你可以调试这个程序并查看我的问题。

#include <windows.h>
#include <iostream>
#include "resource.h"

using namespace std;

LRESULT CALLBACK WindowProcessMessages(HWND hwnd, UINT msg, WPARAM param, LPARAM lparam);
const char *title = "Check Box";

HWND agree, button;

int WINAPI WinMain(HINSTANCE currentInstance, HINSTANCE previousInstance, PSTR cmdLine, INT cmdCount)
{
    // Register the window class
    const char* CLASS_NAME = "myWin32WindowClass";
    WNDCLASS wc{};
    wc.hInstance = currentInstance;
    wc.lpszClassName = CLASS_NAME;
    wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
    wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
    wc.hbrBackground = CreateSolidBrush(RGB(20, 20, 20));
    wc.lpfnWndProc = WindowProcessMessages;
    RegisterClass(&wc);

    HWND main = CreateWindow(CLASS_NAME, "WastedBit  1.6.2",
        WS_OVERLAPPED | WS_VISIBLE | WS_BORDER | WS_MINIMIZEBOX | WS_SYSMENU,   // Window style
        CW_USEDEFAULT, CW_USEDEFAULT,               // Window initial position
        950, 750,                       // Window size
        nullptr, nullptr, nullptr, nullptr);

    // TopMost
    SetWindowPos(main, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);

    // Window loop
    MSG msg{};
    while (GetMessage(&msg, nullptr, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

LRESULT CALLBACK WindowProcessMessages(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    switch (msg)
    {
    case WM_CREATE: {
        button = CreateWindow("button", 0,
            WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
            20, 490, 15, 15,
            hwnd, (HMENU)1, ((LPCREATESTRUCT)lparam)->hInstance, NULL);
        CheckDlgButton(hwnd, 1, BST_UNCHECKED);
    }
    break;
    case WM_COMMAND: {
        BOOL checked = IsDlgButtonChecked(hwnd, 1);
        if (checked) {
            CheckDlgButton(hwnd, 1, BST_UNCHECKED);
        }
        else if (CheckDlgButton(hwnd, 1, BST_CHECKED) == TRUE) {
            CheckDlgButton(hwnd, 1, BST_CHECKED);
            agree = CreateWindow("button", "RUN", WS_VISIBLE | WS_CHILD, 750, 525, 150, 150, hwnd, (HMENU)button, 0, 0);
        }
        else if (CheckDlgButton(hwnd, 1, BST_UNCHECKED) == TRUE) {
            ShowWindow(agree, SW_HIDE);
        }
    }
    break;
    case WM_DESTROY: {
        PostQuitMessage(0);
    }
    break;
    default:
        return DefWindowProc(hwnd, msg, wparam, lparam);
    }
}

【问题讨论】:

    标签: c++ winapi


    【解决方案1】:

    在@duDE 的帮助下,我的程序运行良好。

    我将按钮和复选框放在WM_CREATE中,我使按钮在创建时不可见,但是当用户选中复选框时,按钮被创建。

    • 我不认为这是个问题,但是一旦按下“RUN”按钮,按钮就会消失,并且复选框会恢复为未选中状态。
    LRESULT CALLBACK WindowProcessMessages(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
    {
        switch (msg)
        {
        case WM_CREATE: {
            button = CreateWindow("button", 0,
                WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
                20, 490, 15, 15,
                hwnd, (HMENU)1, ((LPCREATESTRUCT)lparam)->hInstance, NULL);
            agree = CreateWindow("button", "RUN", WS_CHILD, 750, 525, 150, 150, hwnd, 0, 0, 0);
        }
        break;
        case WM_COMMAND: {
            BOOL checked = IsDlgButtonChecked(hwnd, 1);
            if (checked) {
                CheckDlgButton(hwnd, 1, BST_UNCHECKED);
                ShowWindow(agree, SW_HIDE);
            }
            else {
                CheckDlgButton(hwnd, 1, BST_CHECKED);
                ShowWindow(agree, SW_SHOW);
            }
        }
        break;
        case WM_DESTROY: {
            PostQuitMessage(0);
        }
        break;
        default:
            return DefWindowProc(hwnd, msg, wparam, lparam);
        }
    }
    

    【讨论】:

      【解决方案2】:

      我将在 WM_CREATE 中创建两个对象(复选框和按钮),您将得到 handleButton 回来。 并且隐藏应该发生在 WM_COMMAND 然后像这样:

      BOOL checked = IsDlgButtonChecked(hwnd, 1);
       if (checked)
          ShowWindow(handleButton, SW_HIDE);
       else
          ShowWindow(handleButton, SW_SHOW);
      

      【讨论】:

      • 谢谢!但它不起作用,按钮在程序启动时创建,我什至无法选中复选框。
      • 没关系,它现在可以工作了。但是当我点击“RUN”按钮时,该按钮未选中,“RUN”按钮消失了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多