高分求MFC CListBox 函数

选中项,1个或多个上移
选中项,1个或多个下移
2个选中项,交换位置
图片中,左侧是ListBox,右侧是按钮,按钮对左侧的ListBox中选中项进行位置操作。
不需要你做界面,你把代码写了贴出来就行,我测试没问题就给分
代码能用的话,再追加100分奖励,谢谢大家

CListBox不支持多选,所以第三个就不能实现了,而用clistcontrol就可以多选
至于前两个功能没看明白,最好先设计好界面追问

listbox不支持多选?? 肯定可以支持!

追答

下面是代码其中listbox的变量是c_list,selection选择extended(multiple不行)
先在对话框里定义一个成员函数void swap(int ,int);
void CMy1Dlg::swap(int s1,int s2)
{
CString ss1,ss2;
c_list.GetText(s1,ss1);
c_list.GetText(s2,ss2);
c_list.InsertString(s1,ss2);
c_list.DeleteString(s1+1);
c_list.InsertString(s2,ss1);
c_list.DeleteString(s2+1);
}

上移函数:
int listcount=c_list.GetCount();
int i;
for(i=1;i=0;--i)
{
if(c_list.GetSel(i)) swap(i,i+1);
}

交换
int listcount=c_list.GetCount();
int i;
int selectnum=0;
int select1=-1,select2=-1;
for(i=0;i<listcount;++i)
if(c_list.GetSel(i))
{
++selectnum;
if(select1!=-1 && select2==-1) select2=i;
if(select1==-1) select1=i;
}
if(selectnum!=2 )
{
MessageBox(_T("必须选中两项"));
}
else
{
swap(select1,select2);
}

追问

思路正确,但有个问题,循环来上移下移效率较差,特别是元素较多的时候,会导致界面响应迟延。此外还有ItemData没有一同被处理。谢谢你的解答,如果能在完善一下,就选择你的答案为最佳回答,并送上额外的100分

追答

要ItemData是用来做什么的?DWORD_PTR?

追问

这个是附加在每个列表项上的指针数据,不处理的话,会造成BUG的

追答

刚发给你的还有一个bug,就是选中多个数的情况第一个数移到最上面以后还可以继续移动,如果这种情况你想要的结果是多个数字移动到最前面或者最后面就停住的话要修改一下
在加两个成员函数
int CMy1Dlg::minsels()
{
int i;
int min=999;
for(i=0;imax) max=sels[i];
return max;

}
上移和下移改成
void CMy1Dlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
int i;
if(minsels()!=0)
{
for(i=0;i=0;--i)
{
if(sels[i]!=(c_list.GetCount()-1))
{
swap(sels[i],sels[i]+1);
c_list.SetSel(sels[i]+1,1);
sels[i]++;
}
}

交换的不变

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-11-21
很无奈啊,刷新数据这里我无法完成;数据已经改变了,但是更新UI没有很好地实现;
思路明显是对的,你看看代码,然后自己思索下;
可以用自绘来实现数据的更新。。

代码给出来吧
定义的CMyArray类:
CMyArray。h

#pragma once
#include "afxtempl.h"
class CMyArray :
public CArray<CString>
{
public:
CMyArray(void);
~CMyArray(void);
public:
void up(int index,bool bmore);
void Down(int index,bool bmore);
void Change(int one,int two);
};

CMyArray。cpp

#include "StdAfx.h"

#include "MyArray.h"
CMyArray::CMyArray(void)
{
}
CMyArray::~CMyArray(void)
{
}
void CMyArray::up(int index,bool bmore)
{if(!bmore)
{ if(index==0) return;
CString one=this->GetAt(index);
CString two=this->GetAt(index-1);
this->SetAt(index,two);
this->SetAt(index-1,one);
}
else{
}
}void CMyArray::Down(int index,bool bmore)
{
if(!bmore)
{
if(index==this->GetCount()-1) return;
CString one=this->GetAt(index);
CString two=this->GetAt(index+1);
this->SetAt(index,two);
this->SetAt(index+1,one);
}
else{
}
}
void CMyArray::Change(int one,int two)
{
CString ones=this->GetAt(one);
CString twos=this->GetAt(two);
this->SetAt(one,twos);
this->SetAt(two,ones);
}
CMFC_ListBoxDlg。h

#pragma once
#include "MyArray.h"
// CMFC_ListBoxDlg 对话框
class CMFC_ListBoxDlg : public CDialogEx
{
// 构造
public:
CMFC_ListBoxDlg(CWnd* pParent = NULL);// 标准构造函数
CMyArray m;
void fresh();
// 对话框数据
enum { IDD = IDD_MFC_LISTBOX_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX);// DDX/DDV 支持
// 实现
protected:
HICON m_hIcon;
// 生成的消息映射函数
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedButton2();
afx_msg void OnBnClickedButton1();
afx_msg void OnBnClickedButton3();
//afx_msg void OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct);
};

CMFC_ListBoxDlg。cpp

void CMFC_ListBoxDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
CListBox*l=(CListBox*)GetDlgItem(IDC_LIST1);
int index=l->GetCurSel();
m.Down(
index,false);
fresh();l->Invalidate(FALSE);
}
void CMFC_ListBoxDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
CListBox*l=(CListBox*)GetDlgItem(IDC_LIST1);
int index=l->GetCurSel();
m.up(
index,false);
fresh();
;
l->Invalidate(FALSE);
}
void CMFC_ListBoxDlg::OnBnClickedButton3()
{
CListBox*l=(CListBox*)GetDlgItem(IDC_LIST1);
int index=l->GetCurSel();
m.change(
index,0);//具体自己实现
fresh();
;
l->Invalidate(FALSE);
// TODO: 在此添加控件通知处理程序代码
}