MFC中列表控件的简单排序技术

编程入门 行业动态 更新时间:2024-10-27 16:39:06
本文介绍了MFC中列表控件的简单排序技术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在搜索用于MFC中列表控制的简单排序技术.我想根据列表中的数字进行排序.我遇到过一些技巧,但是几乎所有的技巧都非常古老且困难.我正在使用VC2010.任何示例都将有所帮助.

I am searching simple sorting technique for list control in MFC. i want to sort based on numbers in list. i have come across some techniques but almost all was very old and difficult. i am using VC2010. any example will be helpful.

推荐答案

旧示例仍然有效.但是,应该使用ListView_SortItemsEx()宏而不是SortItems()成员函数,该成员函数不会将索引传递给sort函数(它传递项目数据值).这需要COMCTL 5.8或更高版本(IE 5.0或更高版本). Old examples are still valid. However, you should use the ListView_SortItemsEx() macro rather than the SortItems() member function which did not pass the indexes to the sort function (it passes item data values). This requires COMCTL version 5.8 or later (IE 5.0 or later). #define MAX_LIST_CHARS 255 // see below class CMyListCtrl { bool m_bSortDesc; int m_nSortCol; static int CALLBACK CompareFuncEx(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort); int CompareFunc2(int nNdx1, int nNdx2) const; ... }; CMyListCtrl::CMyListCtrl() { m_bSortDesc = false; m_nSortCol = 0; } // Sort the list using the specified column and sort direction. CMyListCtrl::DoSort(int nSortColumn, bool bSortDesc) { m_nSortColumn = nSortColumn; m_bSortDesc = bSortDsc; ListView_SortItemsEx(m_hWnd, CompareFuncEx, reinterpret_cast<LPARAM>(this)); } // This compare function must be called using the ListView_SortItemsEx() macro! int CALLBACK CMyListCtrl::CompareFuncEx(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) { return reinterpret_cast<CMyListCtrl*>(lParamSort)->CompareFunc(static_cast<int>(lParam1), static_cast<int>(lParam2)); } int CMyListCtrl::CompareFunc2(int nNdx1, int nNdx2) const { // Avoid using the GetItemText() function returning CString here // for performance reasons. TCHAR lpszItem1[MAX_LIST_CHARS + 1]; TCHAR lpszItem2[MAX_LIST_CHARS + 1]; ListView_GetItemText(m_hWnd, nNdx1, m_nSortCol, lpszItem1, MAX_LIST_CHARS); ListView_GetItemText(m_hWnd, nNdx2, m_nSortCol, lpszItem2, MAX_LIST_CHARS); lpszItem1[MAX_LIST_CHARS] = lpszItem2[MAX_LIST_CHARS] = _T('\0'); // sort by content int nComp = _tcscmp(lpszItem1, lpszItem2); // sort by column specific data if (nComp) { switch (m_nSortColumn) { case MY_INT_COLUMN : nComp = _tstoi(lpszItem1) - _tstoi(lpszItem2); break; case MY_FLOAT_COLUMN : nComp = (_tstof(lpszItem1) >= _tstof(lpszItem2)) ? 1 : -1; break; case MY_TEXT_COLUMN : nComp = _tcscoll(lpszItem1, lpszItem2); break; case MY_TEXT_NOCASE_COLUMN : nComp = _tcsicoll(lpszItem1, lpszItem2); break; } } // revert if sorting in descending order return (m_bSortDesc && nComp) ? -nComp : nComp; }

更多推荐

MFC中列表控件的简单排序技术

本文发布于:2023-08-02 15:21:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1278858.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   简单   列表   技术   MFC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!