CListBox添加复选框(Check):同时实现list行上移下移按钮,对话框记忆功能

   MFC中其实有一个CCheckListBox类支持复选框风格,所以可以直接使用CListBox控件,然后初始化时把它转化成CCheckListBox,再设置一下控件风格参数就行。但要注意一下,关键是要修改ListBox控件的两处属性设置,分别是“Owner draw”:设置为Fixed(LBS_OWNERDRAWFIXED),“Has strings”设置为True(LBS_HASSTRINGS),否则初始化失败。
具体实现步骤举例如下——

1.创建一个对话框资源,为它绑定一个类,.h和.cpp;

   但要注意一下,关键是要修改ListBox控件的两处属性设置,分别是“Owner draw”:设置为Fixed(LBS_OWNERDRAWFIXED),“Has strings”设置为True(LBS_HASSTRINGS),否则初始化失败。
CListBox添加复选框(Check):同时实现list行上移下移按钮,对话框记忆功能_第1张图片

2.修改头文件

private:
    // 复选框列表
    CCheckListBox m_stuffCheckList;

protected:
    // Summary:初始化CXTPCheckListBox控件(对话框自动记忆)
    // --------------------------------------------------------------------------
    void InitCheckListBox();

    // --------------------------------------------------------------------------
    // Summary:初始化CXTPCheckListBox控件(默认值初始化)
    // --------------------------------------------------------------------------
    void DefaultInitCheckListBox();
public// Summary:复选框全选
    afx_msg void OnBnClickedBtnSelectaAll();

    // Summary:复选框取消全选
    afx_msg void OnBnClickedBtnCancelAll();

    // 获取移动前的顺序和选中状态
    void GetAllTypeNameAndCheckStatus(std::mapint>& mapTypeNameCheckStatus);

    // 设置移动后的各类型选中状态(确保与移动前保持一致)
    void SetAllTypeNameAndCheckStatus(std::mapint>& mapTypeNameCheckStatus, int nSelect);

    //调整顺序——上移
    afx_msg void OnBnClickedBtnUp();

    //调整顺序——下移
    afx_msg void OnBnClickedBtnDown();

    // Summary:改变复选框状态时响应函数
    afx_msg void OnCheckchangeListCtrl();

3.修改.cpp文件

void CMaterialContentDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_LIST_BOX, m_stuffCheckList);
}

BEGIN_MESSAGE_MAP(CMaterialContentDlg, CPipeBaseDialog)
    ON_BN_CLICKED(IDOK, &CMaterialContentDlg::OnBnClickedOk)
    ON_CLBN_CHKCHANGE(IDC_LIST_BOX, &CMaterialContentDlg::OnCheckchangeListCtrl)
    ON_BN_CLICKED(IDC_BTN_SELECTALL, &CMaterialContentDlg::OnBnClickedBtnSelectaAll)
    ON_BN_CLICKED(IDC_BTN_CANCELALL, &CMaterialContentDlg::OnBnClickedBtnCancelAll)
    ON_BN_CLICKED(IDC_BTN_UP, &CMaterialContentDlg::OnBnClickedBtnUp)
    ON_BN_CLICKED(IDC_BTN_DOWN, &CMaterialContentDlg::OnBnClickedBtnDown)
END_MESSAGE_MAP()

BOOL CTunnelMaterialContentDlg::OnInitDialog()
{
    CPipeBaseDialog::OnInitDialog();

    // TODO:  在此添加额外的初始化
    InitCheckListBox();
    return TRUE; 
}

void CMaterialContentDlg::InitCheckListBox()
{
    m_stuffCheckList.Initialize();

    for (int i = 0; i < 7; i++)
    {
        CString strTemp;
        CString strTempStatus;
        strTemp.Format(_T("CHECK%d"), i+1);
        strTempStatus.Format(_T("CHECKSTATUS%d"), i+1);

        CString strItemName;
        DWORD dValue;
        GetDialogData(strTemp, strItemName);
        GetDialogData(strTempStatus, dValue);
        if (strItemName.IsEmpty())
        {
            DefaultInitCheckListBox();
            return;
        }

        m_stuffCheckList.InsertString(i, strItemName);
        m_stuffCheckList.SetCheck(i, (int)dValue);
    }
}

void CMaterialContentDlg::DefaultInitCheckListBox()
{
    m_stuffCheckList.ResetContent();
    m_aryStuffName.Add(_T("a"));
    m_aryStuffName.Add(_T("b"));
    m_aryStuffName.Add(_T("c"));
    m_aryStuffName.Add(_T("d"));
    m_aryStuffName.Add(_T("e"));
    m_aryStuffName.Add(_T("f"));
    m_aryStuffName.Add(_T("g"));

    for (int i = 0; i < m_aryStuffName.GetCount(); i++)
    {
        m_stuffCheckList.InsertString(i, m_aryStuffName.GetAt(i));
    }

    // 默认全部选中
    OnBnClickedBtnSelectaAll();
}

void CTunnelMaterialContentDlg::OnCheckchangeListCtrl()
{


}

void CTunnelMaterialContentDlg::OnBnClickedBtnSelectaAll()
{
    for (int i = 0; i < m_stuffCheckList.GetCount(); i++)
    {
        m_stuffCheckList.SetCheck(i, 1);
    }
}

void CTunnelMaterialContentDlg::OnBnClickedBtnCancelAll()
{
    for (int i = 0; i < m_stuffCheckList.GetCount(); i++)
    {
        m_stuffCheckList.SetCheck(i, 0);
    }
}

void CTunnelMaterialContentDlg::GetAllTypeNameAndCheckStatus(std::mapint>& mapTypeNameCheckStatus)
{
    m_aryStuffName.RemoveAll();

    for (int i = 0; i < m_stuffCheckList.GetCount(); i++)
    {
        CString strName;
        int nChecked = 0;
        m_stuffCheckList.GetText(i, strName);
        nChecked = m_stuffCheckList.GetCheck(i);

        m_aryStuffName.Add(strName);
        mapTypeNameCheckStatus.insert(std::make_pair(strName, nChecked));
    }
}

void CTunnelMaterialContentDlg::SetAllTypeNameAndCheckStatus(std::mapint>& mapTypeNameCheckStatus, int nSelect)
{
    m_stuffCheckList.ResetContent();

    for (int i = 0; i < m_aryStuffName.GetCount(); i++)
    {
        m_stuffCheckList.InsertString(i, m_aryStuffName.GetAt(i));

        std::mapint>::iterator iter;
        for (iter = mapTypeNameCheckStatus.begin(); iter != mapTypeNameCheckStatus.end(); iter++)
        {
            if ( (iter->first).CompareNoCase(m_aryStuffName.GetAt(i)) == 0 )
            {
                m_stuffCheckList.SetCheck(i, iter->second);
                break;
            }
        }
    }

    m_stuffCheckList.SetCurSel(nSelect);
}

void CTunnelMaterialContentDlg::OnBnClickedBtnUp()
{
    int nItem= m_stuffCheckList.GetCurSel();
    if (nItem <= 0 || nItem > (m_stuffCheckList.GetCount() - 1) )
    {
        return;
    }

    std::mapint> mapTypeNameCheckStatus;
    GetAllTypeNameAndCheckStatus(mapTypeNameCheckStatus);

    CString strCurName;
    m_stuffCheckList.GetText(nItem, strCurName);
    for (int i = 0; i < m_aryStuffName.GetCount(); i++)
    {
        if (strCurName.CompareNoCase(m_aryStuffName.GetAt(i)) == 0)
        {
            m_aryStuffName.SetAt(i, m_aryStuffName.GetAt(i - 1));
            m_aryStuffName.SetAt(i -1, strCurName);
            break;
        }
    }

    SetAllTypeNameAndCheckStatus(mapTypeNameCheckStatus, nItem - 1);
}

void CTunnelMaterialContentDlg::OnBnClickedBtnDown()
{
    int nItem= m_stuffCheckList.GetCurSel();
    if ( nItem >= (m_stuffCheckList.GetCount() - 1)  || nItem < 0)
    {
        return;
    }

    std::mapint> mapTypeNameCheckStatus;
    GetAllTypeNameAndCheckStatus(mapTypeNameCheckStatus);

    CString strCurName;
    m_stuffCheckList.GetText(nItem, strCurName);
    for (int i = 0; i < m_aryStuffName.GetCount(); i++)
    {
        if (strCurName.CompareNoCase(m_aryStuffName.GetAt(i)) == 0)
        {
            m_aryStuffName.SetAt(i, m_aryStuffName.GetAt(i + 1));
            m_aryStuffName.SetAt(i + 1, strCurName);
            break;
        }
    }

    SetAllTypeNameAndCheckStatus(mapTypeNameCheckStatus, nItem + 1);
}

void CTunnelMaterialContentDlg::OnBnClickedOk()
{
    // TODO: 在此添加控件通知处理程序代码
    for (int i = 0; i < m_stuffCheckList.GetCount(); i++)
    {
        CString strName;
        m_stuffCheckList.GetText(i, strName);
        int nCheckStatus = m_stuffCheckList.GetCheck(i);

        // 对话框自动记忆功能
        CString strTempName;
        CString strTempStatus;
        strTempName.Format(_T("CHECK%d"), i+1);
        strTempStatus.Format(_T("CHECKSTATUS%d"), i+1);
        SetDialogData(strTempName, strName);
        SetDialogData(strTempStatus,(DWORD)nCheckStatus);
        >>>>>>>>
    }

    // 复选框勾选数量
    int nCheckNum = 0;
    for (int i = 0; i < m_stuffCheckList.GetCount(); i++)
    {
        if (m_stuffCheckList.GetCheck(i))
        {
            nCheckNum++;
        }
    }
    OnOK();
}

4.效果:

CListBox添加复选框(Check):同时实现list行上移下移按钮,对话框记忆功能_第2张图片

你可能感兴趣的:(MFC)