简体   繁体   中英

win32 retrieve index of all selected items from listview

does anyone know how to fix this i wanna execute code for every selected item in the list

            itemint=SendMessage(hwndList,LVM_GETNEXTITEM,-1,LVNI_SELECTED))
              {
                while (itemint != -1)
                  {
                    itemint =SendMessage(hwndList,LVM_GETNEXTITEM, itemint, LVNI_SELECTED);
                      //// 
                  }

which fails to find first(with lowest index) item out of selected ones

Your iteration code is in essence correct. The problem is that you are doing your work after the second call to SendMessage , as evidenced by the placement of your comment line. Move the work to be before the second call to SendMessage and you won't skip the first selected item.

itemint = SendMessage(hwndList, LVM_GETNEXTITEM, -1, LVNI_SELECTED))
{
    while (itemint != -1)
    {
        // do work on selected item here
        itemint = SendMessage(hwndList, LVM_GETNEXTITEM, itemint, LVNI_SELECTED); 
    }
}

For what it is worth, your code would miss the first selected item, but also present you with a final item with index of -1.

You don't need to function calls to SendMessage. Write for loop as this.

for (itemInt = -1; (itemInt = SendMessage(hwndList, LVM_GETNEXTITEM, itemInt , LVNI_SELECTED)) != -1; )
{
   // do work on selected item here
   DoIt(itemInt);
}

Well, this is functionally the same as examples above. ListView_GetNextItem is just a convenient macro which translates to SendMessage LVM_GETNEXTITEM. I like this one for readability.

int itemint=-1;
while((itemint=ListView_GetNextItem(hwndList,itemint,LVNI_SELECTED))!=-1)
{
  ...
}

You will see in CommCtrl.h that it is defined like this:

#define ListView_GetNextItem(hwnd, i, flags) \
    (int)SNDMSG((hwnd), LVM_GETNEXTITEM, (WPARAM)(int)(i), MAKELPARAM((flags), 0))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM