通过错列的ListView排序 [英] ListView sorting by wrong column

查看:228
本文介绍了通过错列的ListView排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此WINAPI节目,我整理的基础上的日期列中的所有项目。但是,它是由描述一栏,而不是日期列排序。

下面是WM_NOTIFY的code:

 静态字符szText [10];
NM_LISTVIEW * PNM =(NM_LISTVIEW *)lParam的;开关(((LPNMHDR)的lParam) - GT; code){
    案例LVN_COLUMNCLICK:
        如果(pNm-> iSubItem == 2)
            如果(ListView_SortItems(pNm-> hdr.hwndFrom,CompareFunc,
                               (LPARAM)(pNm-> iSubItem))== FALSE)
                的MessageBox(HWND,假,,MB_OK);
        打破;
 / *其他WM_NOTIFY code * /
}

ListView_SortItems 返回TRUE,奇怪。
下面是CompareFunc功能:

  INT回调CompareFunc(LPARAM lParam1,LPARAM lParam2,LPARAM lParamSort)
{
    如果(lParamSort == 2){
        日期D1,D2; //应用程序定义的日期级
        B1重型坦克[32],B2 [32];        sscanf的((字符*)lParam1,%s的%D,B1,放大器; d1.day,&安培; d1.yr);
        sscanf的((字符*)lParam2,%s的%D,B2和放大器; d2.day,&安培; d2.yr);        d1.month = monthtoi(B1); //转换每月串号
        d2.month = monthtoi(B2);        如果(D1> D2)//超载>中和&下;运营商
            返回1;
        否则,如果(D1< D​​2)
            返回-1;
        返回0;
    }
}

我试过检查iSubItem对3而不是2(从1比0为主),但也不能工作。
我在做什么错了?

修改

  INT monthtoi(字符* S)
{
    INT I;    对于(i = 0; I< 12; ++ I){
        //个月的char *的一个全局数组,包含个月
        如果(的strcmp(月[I],S)== 0)
            返回我;
    }
    返回-1;
}
布尔日期::运算>(常量日期和放大器; X)
{
    开关(这 - > CMP(X)){// CMP是一个私有成员函数
    情况下0:
    情况1:
        返回false;
    情况1:
        返回true;
    }
    返回false;
}
布尔日期::运算≤(const的日期和放大器; X)
{
    开关(这 - > CMP(X)){
    情况下0:
    情况1:
        返回false;
    情况1:
        返回true;
    }
    返回false;
}
INT日期:: CMP(常量日期和放大器; X)
{
    如果(这 - >&年GT; x.yr)
        返回1;
    否则,如果(这个 - >&年LT; x.yr)
        返回-1;    如果(这 - >年== x.yr){
        如果(这 - >当月> x.month)
            返回1;
        否则,如果(这个 - >当月< x.month)
            返回-1;
        否则,如果(这个 - >日间及GT; x.day)
            返回1;
        否则,如果(这个 - >日间及LT; x.day)
            返回-1;
        其他
            返回0;
    }
    返回0;
}


解决方案

输入 lParam1 lParam2 不该分项的文件,但作为字符串说,他们是与该项目相关的数据:


  

的lParam1参数是与所述第一相关联的32位值
  项目进行比较;并且lParam2参数是相关联的值
  与第二项。这些是在所指定的值
  当他们被插入的项'LVITEM结构的lParam成员
  到列表


您可以找到一个完整的ListView排序例如这里 ,但它的基本功能如下:

  //自定义类型存储该项目的信息,或者给它一个链接
结构myitemdata_t
{
    字符* pFood;
    字符* pDesc​​ription;
    日期日期;
    ...
};     //当项目添加到列表视图设置的项目数据
m_ctlListView.InsertItem(我,食品);
m_ctlListView.SetItemText(I,1,周六购物);
...
     //设置项数据列表项目
m_ctlListView.SetItemData(ⅰ,(LPARAM)GetItemData(ⅰ));    //你的排序功能看起来应该像
INT回调SortFunc(LPARAM lParam1,LPARAM lParam2,LPARAM lParamSort)
{
    myitemdata_t * pData1 =(myitemdata_t *)lParam1;
    myitemdata_t * pData2 =(myitemdata_t *)lParam2;
    ...

In this winapi program, I am sorting all the items based on the "Date" column. However, it is sorting by the "Description" column instead of the "Date" column.

Here is the code in WM_NOTIFY:

static char szText[10];
NM_LISTVIEW *pNm = (NM_LISTVIEW *)lParam;

switch (((LPNMHDR)lParam)->code) {
    case LVN_COLUMNCLICK:
        if (pNm->iSubItem == 2)
            if (ListView_SortItems(pNm->hdr.hwndFrom, CompareFunc, 
                               (LPARAM) (pNm->iSubItem)) == FALSE)
                MessageBox(hwnd, "FALSE", "", MB_OK);
        break;
 /* other WM_NOTIFY code */
}

ListView_SortItems returns TRUE, strangely. Here is the CompareFunc function:

int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
    if (lParamSort == 2) {
        date d1, d2;              // app-defined "date" class
        char b1[32], b2[32];

        sscanf((char *) lParam1, "%s %d %d", b1, &d1.day, &d1.yr);
        sscanf((char *) lParam2, "%s %d %d", b2, &d2.day, &d2.yr);

        d1.month = monthtoi(b1);    // converts month as string to number
        d2.month = monthtoi(b2);

        if (d1 > d2)                // overloading the ">" and "<" operators
            return 1;
        else if (d1 < d2)
            return -1;
        return 0;
    }
}

I tried checking iSubItem against 3 rather than 2 (1-based vs 0-based), but that didn't work either. What am I doing wrong?

Edit:

int monthtoi(char *s)
{
    int i;

    for (i = 0; i < 12; ++i) {
        // MONTHS is a global array of char *, containing the months
        if (strcmp(MONTHS[i], s) == 0)
            return i;
    }
    return -1;
}
bool date::operator>(const date &x)
{
    switch (this->cmp(x)) { // cmp is a private member function
    case 0:
    case -1:
        return false;
    case 1:
        return true;
    }
    return false;
}
bool date::operator<(const date &x)
{
    switch (this->cmp(x)) {
    case 0:
    case 1:
        return false;
    case -1:
        return true;
    }
    return false;
}
int date::cmp(const date &x)
{
    if (this->yr > x.yr)
        return 1;
    else if (this->yr < x.yr)
        return -1;

    if (this->yr == x.yr) {
        if (this->month > x.month)
            return 1;
        else if (this->month < x.month)
            return -1;
        else if (this->day > x.day)
            return 1;
        else if (this->day < x.day)
            return -1;
        else
            return 0;
    }
    return 0;
}

解决方案

The input lParam1 and lParam2 are not the string of the sub-item but as the documentation says they are the data associated with the item:

The lParam1 parameter is the 32-bit value associated with the first item being compared; and the lParam2 parameter is the value associated with the second item. These are the values that were specified in the lParam member of the items' LVITEM structure when they were inserted into the list.

You can find a complete ListView sorting example here but the basics of it are as follows:

          // Custom type storing the item's information, or a link to it
struct myitemdata_t 
{
    char* pFood;
    char* pDescription;
    date  Date;
    ...
};

     // When adding items to a listview set the item data
m_ctlListView.InsertItem(i, "food");
m_ctlListView.SetItemText(i, 1, "Saturday shopping");
...
     // Set the item data for the list item
m_ctlListView.SetItemData(i, (LPARAM) GetItemData(i));

    // Your sort function should look like
int CALLBACK SortFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
    myitemdata_t* pData1 = (myitemdata_t *)lParam1;
    myitemdata_t* pData2 = (myitemdata_t *)lParam2;
    ...

这篇关于通过错列的ListView排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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