How to use SetItem() and GetItem() for ListCtrl having JPEG images?
-
Hi All, I am using listctrl in which I add JPEG images loading from images saves on hard disk. To Set item in ListCtrl control I use SetItem() function as follows CString pstr = "C:\\JPEG_image"; BOOL blnCheck = mWebPagePrvListCtrl.SetItemData(iItemindex,(DWORD)pstr); But the above statement not set the value of pstr in ListCtrl. To identify which images is click by user I use following code to access data of image. as int itemIndex = mWebPagePrvListCtrl.GetHotItem(); CString* pStr = (CString*) mWebPagePrvListCtrl.GetItemData(itemIndex); Plz send me what is the problem in above code. Thanks in Advance Atul
-
Hi All, I am using listctrl in which I add JPEG images loading from images saves on hard disk. To Set item in ListCtrl control I use SetItem() function as follows CString pstr = "C:\\JPEG_image"; BOOL blnCheck = mWebPagePrvListCtrl.SetItemData(iItemindex,(DWORD)pstr); But the above statement not set the value of pstr in ListCtrl. To identify which images is click by user I use following code to access data of image. as int itemIndex = mWebPagePrvListCtrl.GetHotItem(); CString* pStr = (CString*) mWebPagePrvListCtrl.GetItemData(itemIndex); Plz send me what is the problem in above code. Thanks in Advance Atul
-
Atulmahajan wrote:
Yes It is Local.
So I think you are mistaken. Actually you are trying to save the image name along with each list control item, right? SetItemData is holding only a 4 byte value. We are using this to hold a pointer. Here in your case, it is a pointer to the string. Since the string is local when you exit from the function, the pointer will not be valid. So the stored pointer has no use. One solution is that you can dynamically allocate the string in heap (using new CString). But need to delete the memory when you close the window, or delete the list control item. Another method is that you can use a CStringArray object as member variable. Then you can store each string in the array and can keep the index in the list control item data. Here also removal of the string item from the array is needed, if you delete a list control item (just for saving the memory). Hope you got the idea.
- NS -