CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Dec 2007
    Location
    France
    Posts
    329

    Treeview icon change

    Hi,

    I have a doubt about icon changes in a treeview.

    Should it be handled in WM_NOTIFY by me or is it done by Windows as long as I fill in

    Code:
            tvi.iImage = 0;
            tvi.iSelectedImage = 1;

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Treeview icon change

    Could you explain more clear what your problem is?
    If you just want to change an item icon (or icons) then send TVM_SETITEM message or call TreeView_SetItem macro.
    Victor Nijegorodov

  3. #3
    Join Date
    Dec 2007
    Location
    France
    Posts
    329

    Re: Treeview icon change

    Well, they are not changing when I expand the tree. (opened folder, closed folder)

    I send TreeView_SetItem from WM_NOTIFY but there must be something wrong.
    Im checking for the TVN_ITEMEXPANDED message which value is 4294966890.
    But when I click on a node I get the value of 4294967294.

    So Im confused about for what message should I check?

    Code:
     case WM_NOTIFY:    {
            case ID_TREEVIEW:
            {
                // mouse click
                if(((LPNMHDR)lParam)->code == NM_CLICK)
                {
                    LPNMTREEVIEW nmtv = (LPNMTREEVIEW) lParam;
                    cout << "4294966890 " << nmtv->hdr.code << endl;
                    if (nmtv->hdr.code == TVN_ITEMEXPANDED)
                    {
                        TVITEM item = nmtv->itemNew;
                        item.stateMask = TVIS_STATEIMAGEMASK;
                    
                        if (nmtv->action == TVE_COLLAPSE)
                        {
                            item.state = 2;
                        }
                        else if (nmtv->action == TVE_EXPAND)
                        {
                            item.state = 0;
                        }
    
    
                        TreeView_SetItem(hwndTV, &item);
                    }
    
    
                }
            }
        }
        return 0;
    Last edited by MasterDucky; December 1st, 2014 at 12:43 PM.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Treeview icon change

    Message 4294967294 is NM_CLICK.

    As you are first checking for the code NM_CLICK, hdr.code will be NM_CLICK! Try this (not tested)
    Code:
    // mouse click
                if(((LPNMHDR)lParam)->code == TVN_ITEMEXPANDED)
                {
                    LPNMTREEVIEW nmtv = (LPNMTREEVIEW) lParam;
                        TVITEM item = nmtv->itemNew;
                        item.stateMask = TVIS_STATEIMAGEMASK;
                    
                        if (nmtv->action == TVE_COLLAPSE)
                        {
                            item.state = 2;
                        }
                        else if (nmtv->action == TVE_EXPAND)
                        {
                            item.state = 0;
                        }
    
    
                        TreeView_SetItem(hwndTV, &item);
                    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Dec 2007
    Location
    France
    Posts
    329

    Re: Treeview icon change

    Thank you very much 2kaud but still not working.

    But maybe the problem comes from my set up of the treeview cause I only get one image of the two I assign.

    Code:
    HTREEITEM AddItemToTree(HWND hwndTV,                        LPTSTR lpszItem,
                            int nLevel,
                            LPARAM lParam)
    {
        TVITEM tvi = {0};
        TVINSERTSTRUCT tvins = {0};
        HTREEITEM hti;
    
    
        tvi.mask = TVIF_TEXT | TVIF_IMAGE |
                   TVIF_SELECTEDIMAGE | TVIF_PARAM;
    
    
        // Set the text of the item.
        tvi.pszText = lpszItem;
        tvi.cchTextMax = sizeof(tvi.pszText)/sizeof(tvi.pszText[0]);
    
    
        // Save the heading level in the item's application-defined
        // data area.
        tvi.lParam = lParam;
        tvins.item = tvi;
        tvins.hInsertAfter = hPrev;
    
    
        // Set the parent item based on the specified level.
        if(nLevel == 1)
        {
            tvins.hParent = 0;
            tvi.iImage = 0;
            tvi.iSelectedImage = 1;
        }
        else if(nLevel == 2)
        {
            tvins.hParent = hPrevRootItem;
            tvi.iImage = 2;
            tvi.iSelectedImage = 2;
        }
    
    
        // Add the item to the tree-view control.
        hPrev =(HTREEITEM)SendMessage(hwndTV,
                                      TVM_INSERTITEM,
                                      0,
                                      (LPARAM)(LPTVINSERTSTRUCT)&tvins);
        if(!hPrev)
        {
            ReportError("TVM_INSERTITEM");
            return FALSE;
        }
    
    
        hPrevRootItem = hPrev;
    
    
    
    
        return hPrev;
    }

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Treeview icon change

    What exactly "not working"?
    Victor Nijegorodov

  7. #7
    Join Date
    Dec 2007
    Location
    France
    Posts
    329

    Re: Treeview icon change

    I have three icons loaded and verified: 1 closed folder, 1 opened folder and a file icon.

    Only the closed folder icon getting displayed even in the place of the other two.

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Treeview icon change

    Quote Originally Posted by MasterDucky View Post
    I have three icons loaded and verified: 1 closed folder, 1 opened folder and a file icon.

    Only the closed folder icon getting displayed even in the place of the other two.
    Then your code is somewhere wrong!
    Victor Nijegorodov

  9. #9
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Treeview icon change

    Quote Originally Posted by MasterDucky View Post
    Well, they are not changing when I expand the tree. (opened folder, closed folder)
    Treeview does NOT have this behaviour built in.

    You can give an image for the regular case and an image for when the node is selected.

    This is entirely unrelated at all to the state of a node being expanded, collapsed or 'not having children'.
    If you want different images when the item collapses/expands, then you'll need to handle the Expand/collapse notifications and change the icon explicitely with SetItem().

    Your code in #3 doesn't work because you aren't changing the image. you're only changing the state (or trying at least, the state value and state mask don't match up so the SetItem() in effect does nothing) which has nothing to do (at all) with the item image and item selected image.

  10. #10
    Join Date
    Dec 2007
    Location
    France
    Posts
    329

    Re: Treeview icon change

    Thanks for the explanation that clears it up.

    OK, then I will go one step at the time. Right now I just want it to change image when it's selected.

    This must be built in because I have another code where it's working without the WM_NOTIFY message. Though the treeview there is in a dialogbox but I don't think that matters.

  11. #11
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Treeview icon change

    just provide different images for nImage and nSelectedImage
    the tree will automatically change the image of the selected node without any work needed by you.
    if both nImage and nSelectedImage are identical, then nothing will appear to happen (though internally it'll still draw the select image which just happens to look the same).

    As said, if you want collapsed/expanded images (should be Obvious already that an item is collapsed/expanded because of the [-] and [+] boxes and the fact you see (or don't see) children. But if you really want this.
    Handle the collapse/expand notifications, and use SetItem() to set a new nImage and/or niSelectedImage. Don't forget to set the nMask properly. If you use MFC there's also the convenient SetItemImage() member.

  12. #12
    Join Date
    Dec 2007
    Location
    France
    Posts
    329

    Re: Treeview icon change

    Thanks Reubens!

    I verified the three images are loading and I assigned different images to iImage and iSelectedImage.
    Actually it doesn't even matter what number I assign (4,5 non existing images) it will always display the first image from the list.

    I post the relevant code to see if it could shed some light on the issue:

    Code:
    HTREEITEM AddItemToTree(HWND hwndTV,
                            LPTSTR lpszItem,
                            int nLevel,
                            LPARAM lParam)
    {
        TVITEM tvi = {0};
        TVINSERTSTRUCT tvins = {0};
        HTREEITEM hti;
    
    
        tvi.mask = TVIF_TEXT | TVIF_IMAGE |
                   TVIF_SELECTEDIMAGE | TVIF_PARAM;
    
    
        // Set the text of the item.
        tvi.pszText = lpszItem;
        tvi.cchTextMax = sizeof(tvi.pszText)/sizeof(tvi.pszText[0]);
        tvi.lParam = lParam;
        // Save the heading level in the item's application-defined
        // data area.
        tvins.item = tvi;
    
    
        // Set the parent item based on the specified level.
        if(nLevel == 1)
        {
            tvins.hParent = 0;
            tvi.iImage = 0;
            tvi.iSelectedImage = 1;
        }
        else if(nLevel == 2)
        {
            hti = TreeView_GetParent(hwndTV, hPrev);
            tvi.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE;
            tvi.hItem = hti;
            tvins.hParent = hPrevRootItem;
            tvi.iImage = 2;
            tvi.iSelectedImage = 2;
        }
    
    
        // Add the item to the tree-view control.
        hPrev =(HTREEITEM)SendMessage(hwndTV,
                                      TVM_INSERTITEM,
                                      0,
                                      (LPARAM)(LPTVINSERTSTRUCT)&tvins);
    
    
    
    
        // Save the handle to the item.
        hPrevRootItem = hPrev;
        return hPrev;
    }
    Code:
    BOOL InitTreeViewItems(HWND hwndTV)
    {
        HTREEITEM hti;
    
    
        int i=0,j=1,k=1;
      
        {
            // Add the item to the tree-view control.
            hti = AddItemToTree(hwndTV,"Parent",j,k++);
            hti = AddItemToTree(hwndTV,"Child",2,k++);
        }
        return TRUE;
    }

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

Featured