【问题标题】:ListView sorting by wrong columnListView 按错误列排序
【发布时间】:2015-07-12 04:55:44
【问题描述】:

在这个 winapi 程序中,我根据“日期”列对所有项目进行排序。但是,它是按“描述”列而不是“日期”列排序的。

这是 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 奇怪地返回 TRUE。 这是 CompareFunc 函数:

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;
    }
}

我尝试根据 3 而不是 2(基于 1 与基于 0)检查 iSubItem,但这也不起作用。 我做错了什么?

编辑

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;
}

【问题讨论】:

  • 由于我们看不到monthtoi 或您的operator&gt; 实现的实现,因此几乎没有什么帮助。我们也看不到您的LVITEMs。 lParam 成员是否设置正确?
  • 更改了答案@IInspectable

标签: c++ c sorting listview winapi


【解决方案1】:

输入lParam1lParam2 不是子项的字符串,但正如documentation 所说,它们是与该项关联的数据:

lParam1 参数是与第一个相关联的 32 位值 被比较的项目; lParam2 参数是关联的值 与第二项。这些是在 插入项目时 LVITEM 结构的 lParam 成员 进入列表。

你可以找到一个完整的 ListView 排序示例here,但它的基本原理如下:

          // 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;
    ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 2012-01-07
    • 1970-01-01
    相关资源
    最近更新 更多