MFC中列表控件的简单排序技术 [英] Simple Sorting Technique for List Control in MFC

查看:525
本文介绍了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中列表控件的简单排序技术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆