列表视图控件,对字符串列进行排序? [英] List view control, sorting string column?

查看:72
本文介绍了列表视图控件,对字符串列进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我需要按字符串列对列表视图控件进行排序.
我知道消息LVM_SORTITEMS.
并具有比较功能

所以
1.我必须将所有项目复制到字符串数组
2.将所有项lparam设置为数组的索引
3.发送消息LVM_SORTITEMS

Hi,
I need sort a list view control by string column.
I knew message LVM_SORTITEMS.
and have compare function

So
1. I must copy all items to string array
2. Set to all items lparam to index of the array
3. Send message LVM_SORTITEMS

char myArray[100][MAX_PATH]; //for example static array for store

int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort){
   int ind_1 = int( lParam1 );
   int ind_2 = int( lParam2 );
   return lstrcmp( myArray[ind_1], myArray[ind_2]);
}


OK还是这是错误的方式?


OK or this is wrong way?

推荐答案

当我需要这样做时,我使用添加到listView的每个项目的lParam指向包含以下内容的结构项目本身.这样,在进行比较功能时,您可以简单地使用两个lParam值来访问listView中每个项目所代表的所有数据.
例如,这允许您使用lParamSort参数作为告诉排序函数如何运行的方式.我经常使用它来指定要作为排序依据的列,使用负数或正数来指定方向.

这是一个addItems函数和listView附带的sortFunc



When I need to do it, I use the lParam of each item added to the listView to point to the struct that contains the item itself. This way, when in the compare function, you can simply use the two lParam values to access all of the data that each item in the listView represents.
This allows you (for instance) to use the lParamSort param as a way of telling your sorting function how to behave. I often use this to designate the column I''d like to base the sort on, using negative or positive to designate the direction.

Here''s an addItems func and the sortFunc that goes with that listView



/////// Example Add items to lisstView ////////
    if (numItems != 0)
    {
        HWND lvWnd = GetDlgItem(mainDlgHwnd, IDC_ITEM_LIST);

        clearListView(lvWnd);
        for (int i=0; i<numitems;>        {
            addListViewItem(lvWnd,
                            (char*)makeNiceDate(curFeedItems[i].pubdate).c_str(),
                            (char*)curFeedItems[i].title.c_str(),
                            (char*)curFeedItems[i].author.c_str(),
                            (LPARAM)&curFeedItems[i]);
        }
    }

/////// Example sort ////////
	lParamSort = 1 + nSortColumn;
	if (!bSortAscending)
		lParamSort = -lParamSort;

	// sort list
	ListView_SortItems(pLVInfo->hdr.hwndFrom, myCompFunc, lParamSort);




typedef struct
{
    string title;
    string description;
    string link;
    string author;
    string pubdate;
    string subject;
} rssItem_t;



void addListViewItem(HWND listView, char *timeStr, char *titleStr, char *authorStr, LPARAM ptrToInRssItem)
{
    LV_ITEM newItem;
    int insertIndex;

    insertIndex = ListView_GetItemCount(listView);

    newItem.mask = LVIF_TEXT|LVIF_PARAM;
    newItem.iItem = insertIndex;
    newItem.pszText = timeStr;
    newItem.cchTextMax = strlen(timeStr);
    newItem.iSubItem = 0;
    newItem.lParam = ptrToInRssItem;
    insertIndex = SendMessage(listView, LVM_INSERTITEM, 0, (LPARAM)&newItem);

    newItem.mask = LVIF_TEXT;
    newItem.pszText = titleStr;
    newItem.cchTextMax = strlen(titleStr);
    newItem.iSubItem = 1;
    SendMessage(listView, LVM_SETITEM, 0, (LPARAM)&newItem);

    newItem.mask = LVIF_TEXT;
    newItem.pszText = authorStr;
    newItem.cchTextMax = strlen(authorStr);
    newItem.iSubItem = 2;
    SendMessage(listView, LVM_SETITEM, 0, (LPARAM)&newItem);
}








int CALLBACK myCompFunc(LPARAM lp1, LPARAM lp2, LPARAM sortParam)
{
    bool isAsc = (sortParam > 0);
    int column = abs(sortParam)-1;
    rssItem_t *item1, *item2;

    item1 = (rssItem_t*) lp1;
    item2 = (rssItem_t*) lp2;
    switch (column)
    {
        case 0:
            if (isAsc) return parseDateStr(item1->pubdate) - parseDateStr(item2->pubdate);
            else return parseDateStr(item2->pubdate) - parseDateStr(item1->pubdate);
            break;

        case 1:
            if (isAsc) return strcasecmp(item1->title.c_str(), item2->title.c_str());
            else return strcasecmp(item2->title.c_str(), item1->title.c_str());

        case 2:
            if (isAsc) return strcasecmp(item1->author.c_str(), item2->author.c_str());
            else return strcasecmp(item2->author.c_str(), item1->author.c_str());
            break;
    }
    return 0;
}


这篇关于列表视图控件,对字符串列进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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