Page 65 - DCAP408_WEB_PROGRAMMING
P. 65

Unit 4: Windows Controls




                                                                                                Notes


             Notes  As you can observe, this is a quite simple task. If the listbox has the LBS_SORT style,
             the new item will be added in alphabetical order, or else it will just be added to the end of
             the list.
          This message returns the index of the new item either manner and we can utilize this to perform
          other tasks on the item, like relating  some data with it. Generally this will be  things like a
          pointer to a struct including more information, or maybe an ID that you will use to identify the
          item, it’s up to you.
                  SendDlgItemMessage(hwnd,  IDC_LIST,  LB_SETITEMDATA,  (WPARAM)index,
          (LPARAM)nTimes);

          4.6.2 Getting Data from the ListBox

          Now that we recognize the selection has changed, or at the request of the user, we require to
          obtain the selection from the listbox and do something functional with it.

                 Example: In  this example I’ve used a multi-selection list box, so obtaining the list of
          chosen items is a little trickier. If it were  a single selection listbox, than you could just send
          LB_GETCURSEL to retrieve the item index.

          First we require to obtain the number of selected items, so that we can assign a buffer to save the
          indexes in.
                  HWND  hList  =  GetDlgItem(hwnd,  IDC_LIST);
                  int  count  =  SendMessage(hList,  LB_GETSELCOUNT,  0,  0);
          Then we allocate a buffer based on the number of items, and send LB_GETSELITEMS to fill in the
          array.
                  int  *buf  =  GlobalAlloc(GPTR,  sizeof(int)  *  count);
                  SendMessage(hList,  LB_GETSELITEMS,  (WPARAM)count,  (LPARAM)buf);


                  //  ...  Do  stuff  with  indexes


                  GlobalFree(buf);
          In  this  example,  buf[0]  is  the  first  index,  and  so  on  up  to  buf[count  -  1].
          One of the things you would likely want to do with this list of  indexes, is recover the data
          connected with each item, and perform some processing with it. This is just as uncomplicated as
          setting the data was originally, we just send another message.
                  int  data  =  SendMessage(hList,  LB_GETITEMDATA,  (WPARAM)index,  0);
          If the data was some other type of value (anything that is  32-bits) you could just cast to the
          suitable type.


                 Example: For example if you accumulated HBITMAPs instead of ints...
              HBITMAP hData = (HBITMAP)SendMessage(hList, LB_GETITEMDATA, (WPARAM)index, 0);






                                           LOVELY PROFESSIONAL UNIVERSITY                                   59
   60   61   62   63   64   65   66   67   68   69   70