SAMPLE: How to Simulate Multiple-Selection TreeView Control

Last reviewed: February 15, 1996
Article ID: Q125587
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT version 3.51
        - Microsoft Windows 95 version 4.0
    

SUMMARY

The TVWSTATE sample demonstrates how to simulate a multiple-selection TreeView control. The Windows 95 TreeView control does not support multiple selection. If you want a multiple-selection TreeView, you can use state images to simulate it in your application.

The TVWSTATE sample accomplishes this by using a checkbox type of state image to indicate that the item is selected or cleared (de-selected). These checkboxes will retain their state even if the TreeView loses focus.

Download TVWSTATE.EXE, a self-extracting file, from the Microsoft Software Library (MSL) on the following services:

  • Microsoft Download Service (MSDL)

          Dial (206) 936-6735 to connect to MSDL
          Download TVWSTATE.EXE (size: 49318 bytes) 
    
  • Internet (anonymous FTP)

          ftp ftp.microsoft.com
          Change to the SOFTLIB\MSLFILES directory
          Get TVWSTATE.EXE (size: 49318 bytes) 
    

MORE INFORMATION

The multiple-selection TreeView control simulation is implemented by setting the state image list of the TreeView control to an image list that contains the checked and unchecked checkbox bitmaps. This image list is set by using the TVM_SETIMAGELIST message with lParam == TVSIL_STATE (see InitImageList in TVWSTATE.C). A TreeView control can have two image lists, a normal image list and a state image list. In the TreeView, the display order from left to right is: the expansion button, the state image (if present), the normal image (if present), and then the item text.

When processing the WM_NOTIFY message where (LPNMHDR)lParam->code == NM_CLICK (see MsgNotifyTreeView in TVWSTATE.C), the code checks to see if the user clicked the left mouse button in the checkbox. If this is the case, the state image index of the item is retrieved, the index is toggled between the checked and unchecked image list items, and then the new index is saved.

The state image index identifies which member of the state image list should be displayed. The state image index is stored in bits 12-16 of the item state value. Either TVIS_STATEIMAGEMASK or TVIS_USERMASK can be used to mask off the lower bits. To access just the state image index, use a statement similar to this:

   StateIndex = tvi.state & TVIS_STATEIMAGEMASK;

The INDEXTOSTATEIMAGEMASK macro offsets a value to the correct bits for the state image index. This is accomplished by shifting the given value left 12 places. If the desired state image index is 1, the state can be set using a statement similar to this:

   tvi.state = INDEXTOSTATEIMAGEMASK(1);

This sample can also be modified to implement selection methods similar to those of an extended-selection listbox where the user uses the SHIFT key to select a range of items and/or the CTRL key to select or clear individual items.


Additional reference words: 3.51 4.00
KBCategory: kbui kbcode kbfile
KBSubcategory: UsrCtl


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 15, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.