VC ListCtrl 列表 单击排序怎么实现

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VC ListCtrl 列表 单击排序怎么实现相关的知识,希望对你有一定的参考价值。

VC ListCtrl 列表 单击排序怎么实现
最好事有实例,好研究
人笨了点,查了一上午的资料,还是没搞定···

我刚开始工作,很多东西要学
牛哥们能否给小弟些建议。
迫切需要,金融危机,我小命难保呀~~
就是单击列表头,排序。

单击列表头的时候会产生LVN_COLUMNCLICK通知消息
ON_NOTIFY(LVN_COLUMNCLICK, IDC_LIST1, OnColumnclickList1)
你在OnColumnclickList1这个函数里面取得当前单击的是哪个表头
然后在看看是不是需要排序的那一行 调用排序函数就可以了
CListctrl本身也有排序函数的SortItems()
http://www.jcwcn.com/html/VC/10_19_51_127.htm 稍微搜下都有例子的啊
参考技术A 添加OnColumnclickList消息响应函数,isSotred获得的是你单击的那一列,然后再调用回调函数CompareFunc进行处理。
void CP2PAssignmetDlg::OnColumnclickList(NMHDR* pNMHDR, LRESULT* pResult)

NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
// TODO: Add your control notification handler code here

static int iSorted=-1;//排列序号

if (pNMListView->iSubItem==iSorted) return;

iSorted=pNMListView->iSubItem;

m_list.SortItems((PFNLVCOMPARE)CompareFunc,iSorted);
*pResult = 0;


strAllData为你的数据组织结构,可以为Struct
int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2,LPARAM lParamSort)


CString text1,text2;
DEMO_DATA * pInfo1 = strAllData+lParam1;
DEMO_DATA * pInfo2 = strAllData+lParam2;

switch (lParamSort)

case 0L:
text1=pInfo1->num;
text2=pInfo2->num;
break;

case 1L:
text1=pInfo1->name;
text2=pInfo2->name;
break;

case 2L:
text1=pInfo1->isSub;
text2=pInfo2->isSub;
break;

case 3L:
text1=pInfo1->subTime;
text2=pInfo2->subTime;
break;
default:
break;


int i = strcmp(text1,text2);//结果为>0 =0 <0

return i;

参考技术B 能否说的清楚点 没看懂

vc怎么实现列表控件各项的图标拖动

参考技术A 拖动图标通过实现OnBeginDrag等消息函数完成:
void CMyListCtrl::OnBeginDrag(LPNMHDR pnmhdr, LRESULT *pResult)

CPoint ptItem, ptAction, ptImage;
NM_LISTVIEW *pnmListView = (NM_LISTVIEW *)pnmhdr;

ASSERT(!m_bDragging);
m_bDragging = TRUE;
m_iItemDrag = pnmListView->iItem;
ptAction = pnmListView->ptAction;
GetItemPosition(m_iItemDrag, &ptItem); // ptItem is relative to (0,0) and not the view origin
GetOrigin(&m_ptOrigin);

ASSERT(m_pimageListDrag == NULL);
m_pimageListDrag = CreateDragImage(m_iItemDrag, &ptImage);
m_sizeDelta = ptAction - ptImage; // difference between cursor pos and image pos
m_ptHotSpot = ptAction - ptItem + m_ptOrigin; // calculate hotspot for the cursor
m_pimageListDrag->DragShowNolock(TRUE); // lock updates and show drag image
m_pimageListDrag->SetDragCursorImage(0, m_ptHotSpot); // define the hot spot for the new cursor image
m_pimageListDrag->BeginDrag(0, CPoint(0, 0));
ptAction -= m_sizeDelta;
m_pimageListDrag->DragEnter(this, ptAction);
m_pimageListDrag->DragMove(ptAction); // move image to overlap original icon
SetCapture();


void CMyListCtrl::OnMouseMove(UINT nFlags, CPoint point)

long lStyle;
int iItem;
LV_ITEM lvitem;

lStyle = GetWindowLong(m_hWnd, GWL_STYLE);
lStyle &= LVS_TYPEMASK; // drag will do different things in list and report mode
if (m_bDragging)

ASSERT(m_pimageListDrag != NULL);
m_pimageListDrag->DragMove(point - m_sizeDelta); // move the image
if ((iItem = HitTest(point)) != -1)

m_iItemDrop = iItem;
m_pimageListDrag->DragLeave(this); // unlock the window and hide drag image
if (lStyle == LVS_REPORT || lStyle == LVS_LIST)

lvitem.iItem = iItem;
lvitem.iSubItem = 0;
lvitem.mask = LVIF_STATE;
lvitem.stateMask = LVIS_DROPHILITED; // highlight the drop target
SetItem(&lvitem);


point -= m_sizeDelta;
m_pimageListDrag->DragEnter(this, point); // lock updates and show drag image



CListCtrl::OnMouseMove(nFlags, point);


void CMyListCtrl::OnButtonUp(CPoint point)

if (m_bDragging) // end of the drag operation

long lStyle;
CString cstr;

lStyle = GetWindowLong(m_hWnd, GWL_STYLE) & LVS_TYPEMASK;
m_bDragging = FALSE;

ASSERT(m_pimageListDrag != NULL);
m_pimageListDrag->DragLeave(this);
m_pimageListDrag->EndDrag();
delete m_pimageListDrag;
m_pimageListDrag = NULL;

// The drop target's sub-item text is replaced by the dragged item's
// main text
if (lStyle == LVS_REPORT && m_iItemDrop != m_iItemDrag)

cstr = GetItemText(m_iItemDrag, 0);
SetItemText(m_iItemDrop, 1, cstr);


//the character string "**" is added to the end of the drop target's main text
if (lStyle == LVS_LIST && m_iItemDrop != m_iItemDrag)

cstr = GetItemText(m_iItemDrop, 0);
cstr += _T("**");
SetItemText(m_iItemDrop, 0, cstr);


// move the icon
if (lStyle == LVS_ICON || lStyle == LVS_SMALLICON)

point -= m_ptHotSpot; // the icon should be drawn exactly where the image is
point += m_ptOrigin;
SetItemPosition(m_iItemDrag, point); // just move the dragged item


::ReleaseCapture();



void CMyListCtrl::OnLButtonUp(UINT nFlags, CPoint point)

OnButtonUp(point);
CListCtrl::OnLButtonUp(nFlags, point);

在图标间划线可以确定图标的位置然后用Pen自画。
右键菜单相应OnRclick函数
void CMyListView::OnRclick(NMHDR* pNMHDR, LRESULT* pResult)

// TODO: Add your control notification handler code here
CListCtrl &theCtrl = GetListCtrl();
CPoint pt;
::GetCursorPos(&pt);

theCtrl.ScreenToClient(&pt);
LVHITTESTINFO lvinfo;
lvinfo.pt = pt;
lvinfo.flags = LVHT_ABOVE;

m_nItem = theCtrl.SubItemHitTest(&lvinfo);

if(m_nItem != -1)


CMenu menu ,* pSubMenu;

menu.LoadMenu(IDR_MENU1);
pSubMenu = menu.GetSubMenu(0);
theCtrl.ClientToScreen(&pt);
if(pSubMenu)
pSubMenu->TrackPopupMenu (TPM_LEFTALIGN, pt.x, pt.y, this);

*pResult = 0;

以上是关于VC ListCtrl 列表 单击排序怎么实现的主要内容,如果未能解决你的问题,请参考以下文章

listctrl控件怎么用?

ClistCtrl用法及总结(由怎样隐藏ListCtrl列表头的排序小三角形这个bug学习到的知识)

VC++6.0中,如何向文档窗口添加一个ClistView控件,并显示出来?

vc mfc 中的listctrl控件 垂直滚动条置于最底端

在MFC中使用listctrl控件,要修改某行某列的值

VC++如何获取并保存桌面背景?