How to differentiate some LVITEMS (file or directory)?
-
11-03-2021 - |
質問
So I am implementing a CListCtrl using PostMessage(LVN_INSERTITEM,0,(LPARAM) lvitem)
. And I need a way to differentiate lvitems, so that later on when I will get a lvitem, to tell if it's a file or directory. I need to implement using PostMessage
, so I don't know exactly when the item is inserted. I am allocating the item dynamically (storing the dynamically allocated memory pointer in the lParam attribute of the LVITEM structure, so after it is inserted I treat it's notification and deallocate the memory getting the address from the item).
解決
You can use the lParam
member of the LVITEM
structure to set custom data for you list item:
// custom structure to hold some information
struct listItem {
int value;
char path[MAX_PATH];
};
// initialize a custom object to hold a value and a path
LVITEM lvi;
listItem* pItem = new listItem();
pItem->value = 666;
sprintf(pItem->path,"c:\\\\xampp\\htdocs");
// initialize a LVITEM object
memset(&lvi, 0, sizeof(lvi)),
lvi.pszText = "My Folder";
lvi.mask = LVIF_PARAM | LVIF_TEXT;
// lParam points to our custom object
lvi.lParam = (LPARAM)pItem;
SendMessage(g_hwndLV, LVM_INSERTITEM, 0, (LPARAM)&lvi);
Note: In this case you should free the memory pointed to by lParam
using delete
.
他のヒント
You can set arbitrary data with CListCtrl::SetItemData
, but you should use the method CListCtrl::InsertItem
instead of using PostMessage
(which is a low-level Win32 call, not MFC).