CListBox实现list行上移下移

1.函数

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

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

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

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

2.实现方法

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);
}

你可能感兴趣的:(MFC)