下面测试代码完全可以使用LVM_SORTITEMSEX代替,而且不需要设置lParam。
#define expect(condition, r) if ((condition) != (r)) return r;
static INT WINAPI test_CallBackCompare(LPARAM first, LPARAM second, LPARAM lParam)
{
if (first == second) return 0;
CHAR str1[5] = {0};
CHAR str2[5] = {0};
INT r = 0;
HWND hwnd = (HWND)lParam;
LV_ITEMA item = {0};
item.cchTextMax = 5;
item.iSubItem = 0;
item.pszText = str1;
r = SendMessageA(hwnd, LVM_GETITEMTEXTA, first, (LPARAM)&item);
expect(TRUE, r);
item.pszText = str2;
r = SendMessageA(hwnd, LVM_GETITEMTEXTA, second, (LPARAM)&item);
expect(TRUE, r);
return atoi(str1) > atoi(str2)? 1 : (atoi(str1) == atoi(str2) ? 0: -1);
}
INT test_sort(HWND hwnd_listview)
{
static CHAR before_sort_array[][5] = {"6","3","1","4","2"};
static CHAR after_sort_arary[][5] = {"1","2","3","4","6"};
INT r = 0;
LV_ITEMA item = {0};
item.mask = LVIF_PARAM | LVIF_TEXT;
item.iSubItem = 0;
item.cchTextMax = 5;
INT i = 0;
for (i = 0; i < sizeof(before_sort_array)/5; i++)
{
item.iItem = i;
item.lParam = i;
item.pszText = &before_sort_array[i][0];
r = SendMessageA(hwnd_listview, LVM_INSERTITEMA, 0, (LPARAM) &item);
expect(i, r);
}
r = SendMessageA(hwnd_listview, LVM_SORTITEMS, (WPARAM)(LPARAM)hwnd_listview, (LPARAM)test_CallBackCompare);
expect(TRUE, r);
for (i = 0; i < sizeof(before_sort_array)/5; i++)
{
CHAR str[5];
item.iItem = i;
item.cchTextMax = 5;
item.iSubItem = 0;
item.pszText = str;
r = SendMessageA(hwnd_listview, LVM_GETITEMTEXTA, i, (LPARAM)&item);
expect(TRUE, r);
expect(0, strcmp(str, &after_sort_arary[i][0]));
}
MessageBoxW(0,L"ok",L"ok",0);
return 1;
}