Results 1 to 9 of 9

Thread: [VB6] ListView / TreeView Extended and Custom Checkboxes

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,647

    [VB6] ListView / TreeView Extended and Custom Checkboxes

    Name:  lvtvc.jpg
Views: 1597
Size:  40.1 KB

    So I've mentioned this and posted snippets in a few threads, but thought it would be good to do a formal sample project on this, especially since I've never seen one done before.

    By default, the ListView and TreeView controls, whether it's from the OCX or manually created, only has the basic checked or unchecked state. But what if you want to add the Partial check state? Or even more? Or customize the regular checked and unchecked look? Most of the time people jump to owner draw, but there's a much simpler way: checkboxes are simply an imagelist, so all you have to do is create your own and assign it just like you do for the regular icons, no owner drawing required. The ListView/TreeView even manages the number of checkboxes for you; no special code is required to cycle through all the checkboxes then loop back to the beginning. There's 8 different checkboxes in the sample project, I'm not sure what the limit is but you almost certainly won't hit it.

    The only thing that makes this even a little complex is that you have to drop down to the API level to set the imagelist, and subclass it just to prevent VB from accidentally removing the imagelist. The good news though is that it's entirely possible to do it with the regular Common Controls 5.0 ListView/TreeView control, which is what the sample project uses.

    The new checkboxes are stored in a resource file and accessed from there, but I've also included the .ico's as normal files in the zip.

    How it works

    First we create a new API ImageList with our new checkboxes:
    Code:
    Dim hIco As Long
    
    himlCheck = ImageList_Create(32, 32, ILC_COLOR32 Or ILC_ORIGINALSIZE, 1, 1)
    ImageList_SetIconSize himlCheck, 16, 16
    hIco = ResIconTohIcon("CHK_STD_UNCHKD", 16, 16)
    Call ImageList_AddIcon(himlCheck, hIco)
    Call DestroyIcon(hIco)
    'rinse and repeat for all other checkboxes. Note that if you're doing this with a TreeView, 
    'you need a blank icon (not unchecked, entirely blank) as the first image, but with the ListView
    'you just start with the first box in the series- usually unchecked.
    The checkbox imagelist is the State ImageList, so when setting up the ListView, it's assigned as such:
    ListView_SetImageList hLVS, himlCheck, LVSIL_STATE

    That's all you have to do to get started- all items will default to the first checkbox in the list, then cycle through in order with each click, then after the last one returns to the beginning.

    If you want to set the check state through code, you need to use API since True/False isn't good enough,
    Code:
    Dim li As ListItem
    Dim lvi As LVITEM
    
        lvi.iItem = li.Index - 1 'get your li from ListView.Items.Add() and similar
        lvi.Mask = LVIF_STATE
        lvi.StateMask = LVIS_STATEIMAGEMASK
        lvi.State = IndexToStateImageMask(k) 'where k is the 1-based index of the checkbox you want
        ListView_SetItem ListView1.hWnd, lvi
    True/False also doesn't work for retrieving the check state either, so you just have to reverse how it was done when added,
    CheckIndex = StateImageMaskToIndex(ListView_GetItemState(hLVS, iItem, LVIS_STATEIMAGEMASK)) 'where iItem is zero-based

    The procedure for the TreeView is virtually identical, with the important step of adding the blank image mentioned earlier, and needing to get the hItem since the APIs don't use the index (TVITEM.hItem = pvGetHItem(Comctllib.Node))

    That covers the basic concept, all the other code is just standard setup.

    Requirements
    -Windows Vista or higher (although everything is listed as available in comctl32 6.0, it seems XP isn't going to work )
    -Common Controls 6.0 Manifest - The sample project has the cc6.0 manifest embedded in its resource file so it will work when compiled, but to work in the IDE your VB6.exe must also be set up to use the 6.0 controls. See LaVolpe's excellent manifest creator project to generate the manifest and startup code for your own projects.

    NOTE: Apparently, the Common Controls 6.0 OCX TreeView will work with this method, and the manifest isn't needed with that since it can't use it (change Comctllib.Node to MsComctllib.Node). The ListView doesn't so I had assumed the TreeView wouldn't either, but it does.

    Also, I don't know how the project compiled back when I uploaded this, but the modules have a number of #If (WIN32_IE >= &H600) Then statements that need to be removed/commented out to compile now; I don't have time to update the attachment right now though. Comment out/remove them all, even the &H300 or other numbers.
    Attached Images Attached Images  
    Attached Files Attached Files
    Last edited by fafalone; Mar 16th, 2022 at 04:30 AM. Reason: 6.0 TreeView apparently works.

  2. #2
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    673

    Re: [VB6] ListView / TreeView Extended and Custom Checkboxes

    thanks. why if i used in xp3

    maybe
    Name:  listv.JPG
Views: 4074
Size:  33.3 KB

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,647

    Re: [VB6] ListView / TreeView Extended and Custom Checkboxes

    Hm I'm not sure... the checkbox style, imagelist constant, item states... they're all listed as supported as of the 6.0 common controls, which are what XP has. For some reason though it looks like none of it is actually supported in the XP comctl 6.0 tho unfortunately

  4. #4
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: [VB6] ListView / TreeView Extended and Custom Checkboxes

    how to loadpicture by gdiplus ,windows api,without imagelist?
    i don't want use imagelist
    Code:
    Set iMG1 = GDIplusToStdPictureARGB(F2, 64, 64)
    Set iMG2 = GDIplusToStdPictureARGB(F3, 64, 64)
    ImageList1.ListImages.Add , "Closed", iMG1
    ImageList1.ListImages.Add , "Open", iMG2
    use like:
    Code:
     himlCheck = ImageList_Create(32, 32, ILC_COLOR32 Or ILC_ORIGINALSIZE, 1, 1)
    
    ListView_SetExtendedStyle hLVS, LVS_EX_CHECKBOXES
    ListView_SetImageList hLVS, hSysIL16, LVSIL_SMALL
    ListView_SetImageList hLVS, hSysIL32, LVSIL_NORMAL
    ListView_SetImageList hLVS, himlCheck, LVSIL_STATE

  5. #5
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: [VB6] ListView / TreeView Extended and Custom Checkboxes

    Load transparent PNG as the node graph of Treeview
    The picture loaded with the imagelist control is set as the node graph of the Treeview. If it is made transparent, there will be black edges and jagged.
    Maybe you can only use pure API to load images, such as loading png as ico as the node image of Treeview

    c++ - How can I add a transparent PNG as a toolbar icon? - Stack Overflow
    https://stackoverflow.com/questions/...a-toolbar-icon

    use ID_IMG_SPAWN?

    Code:
    himlCheck = ImageList_Create(32, 32, ILC_COLOR32 Or ILC_ORIGINALSIZE, 1, 1)
    ImageList_SetIconSize himlCheck, 16, 16
    hIco = ResIconTohIcon("CHK_STD_UNCHKD", 16, 16)
    Call ImageList_AddIcon(himlCheck, hIco)
    Call DestroyIcon(hIco)
    hIco = ResIconTohIcon("CHK_STD_CHKD", 16, 16)
    Call ImageList_AddIcon(himlCheck, hIco)
    Call DestroyIcon(hIco)
    hIco = ResIconTohIcon("CHK_EXT_PAR", 16, 16)
    Call ImageList_AddIcon(himlCheck, hIco)
    Call DestroyIcon(hIco)
    hIco = ResIconTohIcon("CHK_EXT_EXP", 16, 16)
    Call ImageList_AddIcon(himlCheck, hIco)
    Call DestroyIcon(hIco)
    hIco = ResIconTohIcon("CHK_EXT_DEF", 16, 16)
    Call ImageList_AddIcon(himlCheck, hIco)
    Call DestroyIcon(hIco)
    hIco = ResIconTohIcon("CHK_EXT_X", 16, 16)
    Call ImageList_AddIcon(himlCheck, hIco)
    Call DestroyIcon(hIco)
    hIco = ResIconTohIcon("CHK_CST_UNCHKD", 16, 16)
    Call ImageList_AddIcon(himlCheck, hIco)
    Call DestroyIcon(hIco)
    hIco = ResIconTohIcon("CHK_CST_CHKD", 16, 16)
    Call ImageList_AddIcon(himlCheck, hIco)
    Call DestroyIcon(hIco)

    How to use 32 bit icons in CTreeCtrl - CodeProject
    https://www.codeproject.com/articles...s-in-ctreectrl

    Code:
    // Create a 32bits ImageList
    m_imageList.Create (16, 16, ILC_COLOR32 , 1,1);
    
    //Add the bitmap to the ImageList
    m_bitmap.LoadBitmap(IDB_BITMAP_HIGHCOLOR);
    m_imageList.Add(&m_bitmap, RGB(255,0,255));
    
    //Manage your tree items......
    m_tree.SetImageList (&m_imageList, TVSIL_NORMAL);
    m_tree.InsertItem("RootItem",0,0,TVI_ROOT);
    Last edited by xiaoyao; Apr 9th, 2021 at 06:03 AM.

  6. #6
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: [VB6] ListView / TreeView Extended and Custom Checkboxes

    I've got a Treeview and imagelist using pure api calls and I'm trying to
    load and display a 32bit icon from shell32.dll on winxp in the treeview.

    32 bit icons in API created treeview
    https://microsoft.public.vb.general....eated-treeview

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,647

    Re: [VB6] ListView / TreeView Extended and Custom Checkboxes

    There's no way to avoid using an API imagelist to assign either custon checkbox (state) or regular icons. You can add an HBITMAP or HICON from any source to the imagelist, but you have to use the imagelist.

    Windows XP cannot display custom checkboxes (the imagelist assigned to LVSIL_STATE in the example).

  8. #8
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: [VB6] ListView / TreeView Extended and Custom Checkboxes

    Quote Originally Posted by fafalone View Post
    There's no way to avoid using an API imagelist to assign either custon checkbox (state) or regular icons. You can add an HBITMAP or HICON from any source to the imagelist, but you have to use the imagelist.

    Windows XP cannot display custom checkboxes (the imagelist assigned to LVSIL_STATE in the example).
    The main thing is that I want the picture in the image list box to be transparent.But I looked at hundreds of pages looking for a solution.It seems difficult to do so.The ImageList control will always give you a background color. Or use a specific color to cut out the image, so there will definitely be jagged, can not be smooth.
    I added a background image to the toolbar control. Then you want every button on it to be transparent.I can't handle him.If the screen coordinates of a button can be obtained, it is estimated that a screenshot will be used to calculate the button picture and the background picture and synthesize a new picture. Use this method to simulate the effect of transparency.

    Like the transparent button control I made, it actually intercepts the screen image behind the control.If the toolbar has eight buttons, it can be understood that there are eight button controls below.

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,647

    Re: [VB6] ListView / TreeView Extended and Custom Checkboxes

    You're using an API ImageList right? The VB one doesn't natively support transparency. Some members have projects that force it but I really doubt you'd be able to assign it to a ListView.

    You just specify 0 as the backcolor when adding an HBITMAP with transparency, in an ImageList created with 32bit color.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width