Jump to content
BuckMaster

Treeview Icon Problems

Recommended Posts

BuckMaster

I've been having some problems adding a icon from a .dll into a treeview,

Does the function _GUICtrlTreeView_SetIcon only allow you to use Shell icons or am i doing something wrong?

My main script is way to large to post up so I've created a short example to illustrate my problem

the resource file is attatched

#include <GUITreeView.au3>
#include <TreeViewConstants.au3>
#include <GUIConstants.au3>

Global $GUITree, $hTree

$hGUI = GUICreate("Example", 500, 500, -1, -1)
GUICtrlCreateLabel("Add Item Type ( 1 - 22 )", 220, 20)
$hCombo = GUICtrlCreateCombo("", 220, 40, 50, 22, 0x0003)
GUICtrlSetData(-1, "1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22", "1")
$hCreate = GUICtrlCreateButton("Create Tree Item", 280, 39, 100, 22)
$hTree = GUICtrlCreateTreeView( 10, 10, 200, 480, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS))
GUISetState()

_GUICtrlTreeView_BeginUpdate($hTree)
$GUITree = _GUICtrlTreeView_Add($hTree, 0, "hGUI")
_GUICtrlTreeView_SetIcon($GUITree, $GUITree, @ScriptDir&"\Resources.dll", 119)
_GUICtrlTreeView_EndUpdate($hTree)

While 1
    $hMsg = GUIGetMsg()
    Switch $hMsg
        Case -3
            Exit
        Case $hCreate
            $hTreeItem = _AddTreeItem("Control Type "&GUICtrlRead($hCombo), GUICtrlRead($hCombo))
            _GUICtrlTreeView_Expand($hTree)
    EndSwitch
WEnd

Func _AddTreeItem($aName, $aType)
    _GUICtrlTreeView_BeginUpdate($hTree)
    $hItem = _GUICtrlTreeView_AddChild($hTree, $GUITree, $aName)
    _GUICtrlTreeView_SetIcon($hTree, $hItem, @ScriptDir&"\Resources.dll", _GetIcon($aType))
    _GUICtrlTreeView_EndUpdate($hTree)
    ConsoleWrite("+ _AddTreeItem("&$aName&", "&$aType&")    Icon # "&_GetIcon($aType)&@CRLF)
    Return $hItem
EndFunc

Func _GetIcon($Type)
    If $Type = 1 Then
        Return 122
    ElseIf $Type = 2 Then
        Return 102
    ElseIf $Type = 3 Then
        Return 121
    ElseIf $Type = 4 Then
        Return 114
    ElseIf $Type = 5 Then
        Return 105
    ElseIf $Type = 6 Then
        Return 129
    ElseIf $Type = 7 Then
        Return 107
    ElseIf $Type = 8 Then
        Return 123
    ElseIf $Type = 9 Then
        Return 111
    ElseIf $Type = 10 Then
        Return 127
    ElseIf $Type = 11 Then
        Return 120
    ElseIf $Type = 12 Then
        Return 128
    ElseIf $Type = 13 Then
        Return 135
    ElseIf $Type = 14 Then
        Return 138
    ElseIf $Type = 15 Then
        Return 126
    ElseIf $Type = 16 Then
        Return 136
    ElseIf $Type = 17 Then
        Return 133
    ElseIf $Type = 18 Then
        Return 124
    ElseIf $Type = 19 Then
        Return 116
    ElseIf $Type = 20 Then
        Return 113
    ElseIf $Type = 21 Then
        Return 118
    ElseIf $Type = 22 Then
        Return 104
    EndIf
EndFunc

Any help would be appreciated :)

Resources.zip

Share this post


Link to post
Share on other sites
AZJIO

#include <GUITreeView.au3>

#include <TreeViewConstants.au3>

#include <GUIConstants.au3>



Global $GUITree, $hTree



$hGUI = GUICreate("Example", 500, 500, -1, -1)

GUICtrlCreateLabel("Add Item Type ( 1 - 22 )", 220, 20)

$hCombo = GUICtrlCreateCombo("", 220, 40, 50, 22, 0x0003)

GUICtrlSetData(-1, "1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22", "1")

$hCreate = GUICtrlCreateButton("Create Tree Item", 280, 39, 100, 22)

$hTree = GUICtrlCreateTreeView(10, 10, 200, 480, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS))

GUISetState()



$GUITree = _GUICtrlTreeView_Add($hTree, 0, "hGUI")

_GUICtrlTreeView_SetIcon($hTree, $GUITree, @ScriptDir & "Resources.dll", -119)

; _GUICtrlTreeView_SetIcon($hTree, $GUITree, "shell32.dll", 39)



While 1

    Switch GUIGetMsg()

        Case -3

            Exit

        Case $hCreate

            $hTreeItem = _AddTreeItem("Control Type " & GUICtrlRead($hCombo), GUICtrlRead($hCombo))

            _GUICtrlTreeView_Expand($hTree)

    EndSwitch

WEnd



Func _AddTreeItem($aName, $aType)

    $hItem = _GUICtrlTreeView_AddChild($hTree, $GUITree, $aName)

    _GUICtrlTreeView_SetIcon($hTree, $hItem, @ScriptDir & "Resources.dll", _GetIcon($aType))

    ; _GUICtrlTreeView_SetIcon($hTree, $hItem, "shell32.dll", _GetIcon($aType))

    Return $hItem

EndFunc



Func _GetIcon($Type)

    Switch $Type

        Case 1

            Return -122

        Case 2

            Return -102

        Case 3

            Return -121

        Case 4

            Return -114

        Case 5

            Return -105

        Case 6

            Return -129

        Case 7

            Return -107

        Case 8

            Return -123

        Case 9

            Return -111

        Case 10

            Return -127

        Case 11

            Return -120

        Case 12

            Return -128

        Case 13

            Return -135

        Case 14

            Return -138

        Case 15

            Return -126

        Case 16

            Return -136

        Case 17

            Return -133

        Case 18

            Return -124

        Case 19

            Return -116

        Case 20

            Return -113

        Case 21

            Return -118

        Case 22

            Return -104

    EndSwitch

EndFunc

Share this post


Link to post
Share on other sites
BuckMaster

AZJIO Thank You very much, i would have never of thought to make it negative.

BuckMaster

Share this post


Link to post
Share on other sites
DickG

I'm trying to set a treeview icon using an icon from the Web, like this:

InetGet("https://www.fastmail.com/static/favicons/icon-64x64.png", @ScriptDir & "fastmail.png", 1, 1)
If Not FileExists(@ScriptDir & "fastmail.png") Then
    MsgBox(0, "Test", "Whoops! " & @ScriptDir & "\fastmail.png does not exist.")
EndIf

$SetIcon = _GUICtrlTreeView_SetIcon($Tree, $L2_item, @ScriptDir & "fastmail.png")
If $SetIcon = "False" Then
    MsgBox(0, "Test", "Whoops! Icon not set.")
EndIf

But it's not setting it. It does save the file, though.

I think the problem is that _GUICtrlTreeView_SetIcon() expects to retrieve an icon from an .exe or .dll rather than directly from an image file, like GUICtrlSetImage() does when setting an icon for a listview item or a button. But I don't know how to do that. Any idea? I'm building a treeview that shows bookmarks, and I want to display the icon for each bookmark.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Similar Content

    • Sven-Seyfert
      By Sven-Seyfert
       
      Hi Community,

      I have a problem with the usage of _GUICtrlTreeView_AddChild and setting different item text colors (and different background colors), separately for the TreeView levels.

      I use the UDF GUITreeViewEx.au3 of 'Melba23' and I renamed the functions to increase my understanding about the content. I already talked to him about his UDF to respect his rights and intellectual property. I read many forum threads, I tried many ways to get it, but it's seems to be impossible in the way that I use the UDF.

      Question:
      How can I set different item text colors, separately for the TreeView levels?
      How can I set different item background colors, separately for the TreeView levels?

      I found out that _GUICtrlTreeView_AddChild don't let my set this properties directly. But after loading the TreeView content I can't change the items (children) too. Only the font weight to bold I could set with _GUICtrlTreeView_SetBold but I want to do this in a generic way directly at the creation (loading) of the TreeView.

      Of course I tried the different functions to set text color or bkgColor, but it didn't work - maybe I do it wrong.
      I really hope you can help me, give me a hint and understand what I want to do.
      [Solved] final solution made by @LarsJ (see below)

      Thanks for your help - I'm grateful!
      Sven

      CODE
      BEFORE
      AFTER
      ExecutionPlan.zip
    • gillesg
      By gillesg
      Hello,
      I am struggling in merging GUITreeViewEx, Shelltristate and enhancing to handle a third state that means : some items under are selected.
      I have difficulties handling expand order and key Space (especially when node is collapsed).
      Here the zip with UDF and and example.
       
      The problem I might need some advice to handle : 
      1- When load Treeview, have a correct settings of the checkbox for a tristate tree
      2 - Handle keyboard used for walking in tree
           Chicken is checked and  Steak is unchecked
          When walking with arrow to Meat, it gets unchecked
      3 - When node is collapsed and checked thru keyboard (space)
         the middle state is possible which should not
      Here is joined an animated gif showing the 3 problems
       
      Thanks for your advices
       
       
       
       
       
       
       
       
       
       
       

      GUITreeview3Ex.zip
    • gillesg
      By gillesg
      Hello,
      I just bumped into this problem with _GUICtrlTreeView_Sort.
      Just used doc example and comment out the child creation
      #include <GUIConstantsEx.au3> #include <GuiTreeView.au3> #include <MsgBoxConstants.au3> #include <WindowsConstants.au3> Example() Func Example() Local $aidItem[10], $iX = 9, $iY = 29, $idTreeView Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS) GUICreate("TreeView Sort", 400, 300) $idTreeView = GUICtrlCreateTreeView(2, 2, 396, 268, $iStyle, $WS_EX_CLIENTEDGE) GUISetState(@SW_SHOW) _GUICtrlTreeView_BeginUpdate($idTreeView) For $x = 0 To 3 $aidItem[$x] = GUICtrlCreateTreeViewItem(StringFormat("[%02d] New Item", $iX), $idTreeView) $iX -= 1 For $y = 1 To 3 ; GUICtrlCreateTreeViewItem(StringFormat("[%02d] New Child", $iY), $aidItem[$x]) $iY -= 1 Next Next _GUICtrlTreeView_Expand($idTreeView) _GUICtrlTreeView_EndUpdate($idTreeView) MsgBox($MB_SYSTEMMODAL, "Information", "Sort") _GUICtrlTreeView_Sort($idTreeView) _GUICtrlTreeView_SelectItem($idTreeView, $aidItem[9]) ; Boucle jusqu'à ce que l'utilisateur quitte. Do Until GUIGetMsg() = $GUI_EVENT_CLOSE GUIDelete() EndFunc ;==>Example The tree is not sorted.
       
      There is a similar problem with deeper tree
      Also based on the example
      #include <GUIConstantsEx.au3> #include <GuiTreeView.au3> #include <MsgBoxConstants.au3> #include <WindowsConstants.au3> Example() Func Example() Local $aidItem[10], $bidItem[10], $iX = 9, $iY = 29, $idTreeView Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS) GUICreate("TreeView Sort", 400, 300) $idTreeView = GUICtrlCreateTreeView(2, 2, 396, 268, $iStyle, $WS_EX_CLIENTEDGE) GUISetState(@SW_SHOW) _GUICtrlTreeView_BeginUpdate($idTreeView) $index=0 For $x = 0 To 3 $aidItem[$x] = GUICtrlCreateTreeViewItem(StringFormat("[%02d] New Item", $iX), $idTreeView) $iX -= 1 For $y = 1 To 2 $BidItem[$y]= GUICtrlCreateTreeViewItem(StringFormat("[%02d] New Child", $iY), $aidItem[$x]) $iY -= 1 For $z= 1 To 2 GUICtrlCreateTreeViewItem(StringFormat("[%02d] New Child", $iY), $BidItem[$y]) $iY -= 1 Next Next Next _GUICtrlTreeView_Expand($idTreeView) _GUICtrlTreeView_EndUpdate($idTreeView) MsgBox($MB_SYSTEMMODAL, "Information", "Sort") _GUICtrlTreeView_Sort($idTreeView) _GUICtrlTreeView_SelectItem($idTreeView, $aidItem[9]) ; Boucle jusqu'à ce que l'utilisateur quitte. Do Until GUIGetMsg() = $GUI_EVENT_CLOSE GUIDelete() EndFunc ;==>Example The tree is now 2 level deep and it is not properly sorted.
       
      Any advices ?
       
      Regards
    • buymeapc
      By buymeapc
      Hi all,
      I'm trying to create a listview with subitem icons and I achieved this by simply rearranging the order of the columns, so that the first column is in the middle, thus appearing as a subitem with an icon - this method prevents the "icon clipping" that happens when using subitem icons in a listview. My issue is that when I select a row that does not have an icon, the space where the icon would be is white. Is there a way to make this transparent, so it looks like the entire row is selected?
      Here's an example - click the second listview item/row to see a blank space where the icon would be.
      #include <GuiConstantsEx.au3> #include <GuiListView.au3> #include <GuiImageList.au3> #include <WindowsConstants.au3> _Main() Func _Main() Local $hGui, $listview, $hImage Local $iLV_EXStyle_NoChecks = BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES, $LVS_EX_DOUBLEBUFFER, $LVS_EX_HEADERDRAGDROP) $hGui = GUICreate("Listview Test With Icons", 400, 300) $listview = GUICtrlCreateListView("Col 1|Col 2|Col 3", 2, 2, 394, 268, BitOR($LVS_REPORT, $LVS_SHAREIMAGELISTS)) _GUICtrlListView_SetExtendedListViewStyle($listview, $iLV_EXStyle_NoChecks) GUISetState() ; Create the image list $hImage = _GUIImageList_Create(16, 16, 5, 3) _GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 165) _GUICtrlListView_SetImageList($listview, $hImage, 1) ; Set the order so the first column is moved to the middle, ; acting like a subitem with an icon _GUICtrlListView_SetColumnOrder($listview, "1|0|2") ; Add the items GUICtrlCreateListViewItem("Item1|Item2|Item3", $listview) GUICtrlCreateListViewItem("Item4|Item5|Item6", $listview) ; Set an icon for just the first item _GUICtrlListView_SetItemImage($listview, 0, 0) Do Until GUIGetMsg() = $GUI_EVENT_CLOSE GUIDelete() EndFunc ;==>_Main  
    • GordonFreeman
      By GordonFreeman
      Hi, i searched some topics but cannot found a simple way to do that:
      #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> $Form1 = GUICreate("Form1", 615, 438, 192, 124) GUISetState(@SW_SHOW) ; I tried this but user reported that it work in 2006, not seems work actually $filemenu = GUICtrlCreateMenu ("&File") $fileitem = GUICtrlCreateMenuitem ("Open",$filemenu) GuiCtrlSetImage($fileitem, "shell32.dll", 4) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Also tried:
      #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> $Form1 = GUICreate("Form1", 615, 438, 192, 124) GUISetState(@SW_SHOW) $filemenu = TrayCreateMenu("&File") $fileitem = TrayCreateItem("Open",$filemenu) ;GuiCtrlSetImage($fileitem, "shell32.dll", 4) ;GuiCtrlSetImage(TrayItemGetHandle($fileitem), "shell32.dll", 4) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Well.. thanks in advance!
       
      Ok. I found this working example from Yashied. If i cannot find a simple then i will go with it
      #Include <GUIConstantsEx.au3> #Include <GUIMenu.au3> #Include <Constants.au3> #Include <WinAPI.au3> #Include <WindowsConstants.au3> Opt('MustDeclareVars', 1) Global $hMenu, $hForm, $hFile = 1000, $idNew, $idExit $hForm = GUICreate('Menu', 400, 300) $hFile = _GUICtrlMenu_CreateMenu() _GUICtrlMenu_InsertMenuItem ($hFile, 0, ' &Favorites', $idNew) _GUICtrlMenu_InsertMenuItem ($hFile, 1, '', 0) _GUICtrlMenu_InsertMenuItem($hFile, 2, ' E&xit', $idExit) $hMenu = _GUICtrlMenu_CreateMenu() _GUICtrlMenu_InsertMenuItem($hMenu, 0, '&File', 0, $hFile) _GUICtrlMenu_SetMenu($hForm, $hMenu) _GUICtrlMenu_SetItemBmp($hFile, 0, _CreateBitmapFromIcon(_WinAPI_GetSysColor($COLOR_MENU), @SystemDir & '\shell32.dll', 43, 16, 16)) _GUICtrlMenu_SetItemBmp($hFile, 2, _CreateBitmapFromIcon(_WinAPI_GetSysColor($COLOR_MENU), @SystemDir & '\shell32.dll', 27, 16, 16)) GUISetState() Do Until GUIGetMsg() = $GUI_EVENT_CLOSE Func _CreateBitmapFromIcon($iBackground, $sIcon, $iIndex, $iWidth, $iHeight) Local $hDC, $hBackDC, $hBackSv, $hIcon, $hBitmap $hDC = _WinAPI_GetDC(0) $hBackDC = _WinAPI_CreateCompatibleDC($hDC) $hBitmap = _WinAPI_CreateSolidBitmap(0, $iBackground, $iWidth, $iHeight) $hBackSv = _WinAPI_SelectObject($hBackDC, $hBitmap) $hIcon = _WinAPI_PrivateExtractIcon($sIcon, $iIndex, $iWidth, $iHeight) If Not @error Then _WinAPI_DrawIconEx($hBackDC, 0, 0, $hIcon, 0, 0, 0, 0, $DI_NORMAL) _WinAPI_DestroyIcon($hIcon) EndIf _WinAPI_SelectObject($hBackDC, $hBackSv) _WinAPI_ReleaseDC(0, $hDC) _WinAPI_DeleteDC($hBackDC) Return $hBitmap EndFunc ;==>_CreateBitmapFromIcon Func _WinAPI_PrivateExtractIcon($sIcon, $iIndex, $iWidth, $iHeight) Local $hIcon, $tIcon = DllStructCreate('hwnd'), $tID = DllStructCreate('hwnd') Local $Ret = DllCall('user32.dll', 'int', 'PrivateExtractIcons', 'str', $sIcon, 'int', $iIndex, 'int', $iWidth, 'int', $iHeight, 'ptr', DllStructGetPtr($tIcon), 'ptr', DllStructGetPtr($tID), 'int', 1, 'int', 0) If (@error) Or ($Ret[0] = 0) Then Return SetError(1, 0, 0) EndIf $hIcon = DllStructGetData($tIcon, 1) If ($hIcon = Ptr(0)) Or (Not IsPtr($hIcon)) Then Return SetError(1, 0, 0) EndIf Return $hIcon EndFunc ;==>_WinAPI_PrivateExtractIcon Ok, i noticed that is a pain create submenu with _GUICtrlMenu_CreatePopup to do like in Tray, and Tray better located, then i need images/icon with Tray functions, some workaround?? 
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.