Trevin Corkery Trevin Corkery - 1 year ago 116
C++ Question

C++ Win32 - Tracking ListView items for private chat

Alright so what I am doing is creating a encrypted chat room for my friends and I to mess around in.

I wanted to make a private chat where you could right click their name on the List View and message them.

Currently I a structure with all "users" information like name, time logged on, and List View Number. But that List View number becomes incorrect when a user disconnects because it shifts everyone down by one.

How would I be able to essentially "tie" a user to a List View item? That way no matter how many people log on / off when I click on their name it will PM that person. (I tried to use LVN_ITEMCHANGE because I thought the iItem would change but it didn't to my experiance)

**My Idea*
My Idea was to add a random character field in the ListView item called "token" that way when someone clicks on the name it will find the "token" that would be unique and do it that way. But maybe their is an easier way so that is why I am asking.

Answer Source

Your data structure should not be tracking the ListView items at all. It is just extra data.

Your ListView can associate each list item with a data structure. The LVITEM structure has an lParam field for handling user-defined values:

  • When you want to add a new user to the ListView, dynamically allocate your data structure and assign that pointer to the LVITEM::lParam field before using the LVM_INSERTITEM message.

  • When you want to access a particular user's data, retrieve that list item's LVITEM using the LVM_GETITEM message, and then type-cast its lParam to access your data structure.

  • When you want to remove a user, simply remove that list item using the LVM_DELETEITEM message, and then use the LVN_DELETEITEM notification to free the associated data structure.

Alternatively, use the ListView in virtual mode via the LVS_OWNERDATA window style (see Virtual List-View Style and How to Use Virtual List-View Controls), and then you don't need to use the LVITEM::lParam field at all. Store your data structures in a separate array/container somewhere off to the side, and then use the LVN_GETDISPINFO notification to provide display data to the ListView only when it needs data. When you add/remove users from your array/container, simply use the LVM_SETITEMCOUNT message to update the ListView's item count to match the new array/container count. That way there is always a direct relation between ListView items and container items. Let LVN_GETDISPINFO tell you which array/container item to access.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download