首页 > 解决方案 > 选择/取消选择所有复选框 WIN32

问题描述

我在为程序启动菜单动态创建的复选框列表中创建了一个“全选/取消全选”复选框。When the "select/unselect all" check box is selected, I would like all the boxes in the list to show up as checked or unchecked. 目前,点击时只会选中“全选/取消全选”复选框。我不知道如何解决这个问题,因为这些框是动态创建的,并且只有在启动菜单中单击“确定”按钮时才会读取复选框的值。

        // create the structure to store the check boxes so we can populate it on the fly
        CheckBoxOptionStruct* Temp = new CheckBoxOptionStruct;
        Temp->OptionsListIndex = i;
        m_CheckBoxList.push_back(Temp);

        // create the check boxes
        for(int j = 0; j < m_OptionsList[i]->Labels.size(); j++)
        {
            CButton* CheckBox = new CButton();
            CheckBox->Create(m_OptionsList[i]->Labels[j], WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX, CRect(Left + 10, *Top, Right - 10, *Top + 15), this, ControlCounter++);
            CheckBox->SetFont(DialogFont);
            Temp->TheControls.push_back(CheckBox);

            // increment the row counter
            *Top += 20;
        }

        // increment the next row position
        *Top += 10;}

目前如何运作:

图片

我希望它如何工作:

图片

标签: c++winapi

解决方案


您可以创建用户定义的消息及其消息处理程序。

链接:如何创建用户定义的消息及其消息处理程序?

使用 SendMessage 函数触发消息,MFC 将检查消息映射并执行关联的消息处理程序。

您可以在消息处理程序中检查并选择复选框的状态。

链接:如何在 MFC 中选中和取消选中以及启用和禁用复选框控件?

另请注意,您为每个复选框控件设置了唯一 ID。

细节:当你按下OK按钮时,通过自定义消息触发消息处理程序来检查复选框的状态

更多信息:<a href="https://stackoverflow.com/questions/13606686/get-the-notification-code-from-listview-control-checkboxes">从Listview控件复选框中获取通知代码


推荐阅读