WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

COMCONTROL
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

ImageList in TreeView


Question:

OK, I've been working on a TreeView in a dialog and I've got that down (see attached code). Now, I've added an ImageList control in order to add little images to the tree. But the problem is that I don't know how to load a picture/icon in a way that's understandable to the ImageList. See comments in the code that ask for help. Any help would be much appreciated.
;some constants for dlgProc
MSG_InitDialog    = 0
MSG_Timer         = 1
MSG_ButtonPressed = 2
MSG_Close         = 11
MSG_ComEvent      = 14

RET_Cancel  = 0
RET_Normal  = -1
RET_NoEnd   = -2

ID_TreeViewControl  = 003
ID_ImageListControl = 004

TVWFIRST = 0    ;node is placed before all other nodes at the same level of the node named in RELATIVE
TVWLAST = 1     ;node is placed after all other nodes at the same level of the node named in RELATIVE
TVWNEXT = 2     ;node is placed after the node named in RELATIVE
TVWPREVIOUS = 3 ;node is placed before the node named in RELATIVE
TVWCHILD = 4    ;node becomes a child of the node named in RELATIVE

TVWAUTOMATIC = 0  ;The beforeLabelEdit event is generated when the user clicks on the label of a selected node
TVWMANUAL = 1     ;The beforeLabelEdit event is generated only when the StartLabelEdit method is envoked
#DefineSubRoutine dlgProc(handle,msg,id,p4,p5)

   Switch msg

      ;INITIALIZE DIALOG   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
      Case MSG_InitDialog

         ;get object reference for TreeView control on the dialog and populate the tree with some items
         oTree = DialogObject(handle,ID_TreeViewControl,3)
         oTree.LineStyle = 1         ;include lines at the root items
         oTree.Indentation = 3       ;number of spaces to indent
         oTree.LabelEdit = TVWMANUAL ;don't allow editing node text (label). (can only edit if you invoke the StartLabelEdit method on a node)
         oNodes = oTree.Nodes
         oNodes.Add(,,"ONEKEY","ONE")
         oNodes.Add("ONEKEY",TVWCHILD,"TWOKEY","TWO")
         oNodes.Add("ONEKEY",TVWCHILD,"THREEKEY","THREE")
         oNodes.Add("THREEKEY",TVWCHILD,"FOURKEY","FOUR")
         oFive = oNodes.Add(,,"FIVEKEY","FIVE")
         oNodes.Add(oFive,TVWCHILD,"SIXKEY","SIX")
         oFive = 0

         ;get object reference for ImageList control on the dialog
         oImageList = DialogObject(handle,ID_ImageListControl,3)

;------------ HELP NEEDED HERE -----------------------------------------------------------------------------
         oImageList.ListImages.Add(,"FolderClosed",picture) ;documentation says use LoadPicture("file name")
         oImageList.ListImages.Add(,"FolderOpen",picture)   ;but that's not going to work. Now what?
         ;once I get these images on the ImageList control, then I can use them in the TreeView.
;------------ HELP NEEDED HERE -----------------------------------------------------------------------------

         ;tell dialog to capture treeview events
         DialogObject(handle,ID_TreeViewControl,1,"NodeClick",003001)
         DialogObject(handle,ID_TreeViewControl,1,"Expand",003002)

         Return RET_NoEnd
         Break


      ;ON DIALOG CLOSE ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
      Case MSG_Close
         oTree = 0
         oImageList = 0
         Return RET_Normal
         Break


      ;ON COM EVENTS  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
      Case MSG_ComEvent  ;COM event message

         ;check what kinds of COM events:
         Switch p4.identifier
            Case 003001   ;NODECLICK EVENTS
               oNode = p4.parameters(1).value
               nodeKey = oNode.key
               nodeText = oNode.text
               childCnt = oNode.children
               Message("Nodeclick Event",StrCat("Node Text: ",nodeText,@CRLF,"Node Key: ",nodeKey,@CRLF,"Child Count: ",childCnt))
               Break

            Case 003002   ;EXPAND EVENTS
               oNode = p4.parameters(1).value
               nodeKey = oNode.key
               nodeText = oNode.text
               childCnt = oNode.children
               Message("Expand Event",StrCat("Node Text: ",nodeText,@CRLF,"Node Key: ",nodeKey,@CRLF,"Child Count: ",childCnt))
               Break
         EndSwitch
         Return RET_NoEnd
         Break

   EndSwitch

#EndSubRoutine

DlgFormat=`WWWDLGED,6.1`

DlgCaption=`Tree Test`
DlgX=9999
DlgY=9999
DlgWidth=258
DlgHeight=255
DlgNumControls=004
DlgProcedure=`dlgProc`
DlgFont=`DEFAULT`
DlgTextColor=`DEFAULT`
DlgBackground=`DEFAULT,DEFAULT`
DlgConfig=0

Dlg001=`175,235,036,012,PUSHBUTTON,DEFAULT,"OK",1,1,32,DEFAULT,DEFAULT,DEFAULT`
Dlg002=`213,235,036,012,PUSHBUTTON,DEFAULT,"Cancel",0,2,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Dlg003=`005,005,244,224,COMCONTROL,DEFAULT,"MSComctlLib.TreeCtrl.2",DEFAULT,3,DEFAULT,DEFAULT,DEFAULT,"255|255|255"`
Dlg004=`011,233,072,020,COMCONTROL,DEFAULT,"MSComctlLib.ImageListCtrl.2",DEFAULT,5,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

ButtonPushed=Dialog("Dlg")

;FYI:
;SYNTAX FOR Node.Add(RELATIVE,RELATIONSHIP,KEY,TEXT,IMAGE,SELECTEDIMAGE)
;RELATIVE       OPTIONAL  The index number or key of a pre-existing node.  The relationship between the new node and this node is in the RELATIONSHIP argument.
;                         (If no Node object is named, the new node is placed in the last position of the top node hierarchy)
;RELATIONSHIP   OPTIONAL  Specifies the relative placement of the node (see TVW constants)
;KEY            OPTIONAL  A unique string that can be used to retrieve the Node with the Item method
;TEXT           OPTIONAL  The string that appears in the Node.
;IMAGE          OPTIONAL  The index of an image in an associated ImageList control
;SELECTEDIMAGE  OPTIONAL  The index of an image in an associated ImageList control that is shown when the Node is selected.

Answer:

You can't define your imagelist in the dialog, define it as a separate COM Object during initialization
oIMG = CreateObject("MSComctlLib.ImageListCtrl.2")
getIList()
The getlist() creates an imagelist from the 'special' 000 control in the Dialog.
#DefineSubRoutine getIList()
   cImg = StrCat(DirScript(),"nc.gif")
   oPict=DialogObject(handle, 000, 4, cImg)
   oIMG.ListImages.add(1, "", oPict)
   cImg = StrCat(DirScript(),"xpfolder.gif")
   oPict=DialogObject(handle, 000, 4, cImg)
   oIMG.ListImages.add(2, "", oPict)
   cImg = StrCat(DirScript(),"xpmydoc.gif")
   oPict=DialogObject(handle, 000, 4, cImg)
   oIMG.ListImages.add(3, "", oPict)
#EndSubRoutine
Then assign your imagelist to the TreeView
oTree.ImageList = oIMG
Finally, when adding nodes use the last parameter to indicate the index of the image
oNode = oNodes.Add(nLevel1, tvwChild, cKey,cLevel ,2)
assigns the xpfolder image to the node

User Reply:

What's this special 000 control? Any special explanation somewhere?

Answer:

Truth be told; any numeric value will work for the second parameter to DialogObject when the function is used for loading an image into a COM object. This is because the parameter is ignored except for having its type checked. The DialogObject function is sort of a Swiss Army Knife function. It can be used for several purposes related to the COMCONTROL controls and the second parameter is not needed to load images except as a place holder to get to the used parameters.
Article ID:   W17703
Filename:   ImageList in TreeView.txt
File Created: 2008:11:25:13:02:32
Last Updated: 2008:11:25:13:02:32