2015-05-02 65 views
2

在这个winapi程序中,我基于“日期”列对所有项目进行排序。但是,它是通过“描述”列而不是“日期”列进行排序的。 This is how my listview looksListView按错误列排序

这里是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; 
    } 
} 

我试图检查iSubItem针对3而不是2(1-基于VS从0开始),但也不能工作。 我在做什么错?

编辑

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

既然我们不能看到'monthtoi'实施或你的'运营商>'执行,几乎没有有助于预期。我们也看不到你的'LVITEM's。 lParam'成员是否正确设置? – IInspectable

+0

更改了答案@IInspectable – stackptr

回答

2

输入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; 
    ...