Solved

Icons in a TreeView control in VB 5

Posted on 1997-10-02
14
328 Views
Last Modified: 2011-09-20
I am having troubles reducing the size of the + and _ signs in front of collapsed and expanded nodes. Furthermore, they are as big as the icons themselves.
I have tried to change the font size, but the gap between two lines remains the same. So it means that if originally my treeview was showing 6 lines with a font size of 8, it will still show 6 lines with a Small Font of 3 (and it becomes unreadable). So I gathered that the font size is not the real problem. The question is: how do you alter the distance between 2 lines?
How do you alter the size of the icons which are clearly too big for what I wish to do? (VB 5 does not let me change the picture's height and width (from 32 x 32 to 16 x 16) in the ImageList control!)

Thanks for your time.
0
Comment
Question by:emmle
  • 6
  • 4
  • 2
  • +2
14 Comments
 
LVL 9

Expert Comment

by:cymbolic
ID: 1436386
emmle, I can tell from your questions that you are just starting with the VB treeview control.  You really need to survey some of the third party treeview controls before you get deeply into your project.  Other treeview controls have much more capability, but there is so much diference in the way these controls work, that once you go down the road with one of them, you practically have to rewrite your app to use any other.  I've found the treeview to be very recalcitrant.  That is you can't easily (in VB) extend functonality, and you need to write a lot of code just to use the basic properties and methods.  You'll find yourself spending a lot of time with the nodes collection as well.

Anyway, you can't change the imagelist sizes at run time.  The best you can do is set the size when you build them at design time.  You are correct, your between line spacing is there to preserve space for Icons, but it's also there because the control reserves mouse space around each line as well.  This is typical behaviour with most controls.
0
 

Author Comment

by:emmle
ID: 1436387
cymbolic,


Have you got any lead on 3rd party treeview controls. I would like the TreeView to give the same results as Windows NT explorer, or SQL Server.
Still don't understand why the picture's height and width 16 x 16 in a Imagelist are given to us if we cannot use it.
But I must confess, it seems a lot of efforts for little results!
0
 
LVL 9

Expert Comment

by:cymbolic
ID: 1436388
The image list is only a container.  Unfortunately, the image size is only available at design time, although you can add images at run time.  We typically add the image control to the application, then populate it with images at run time.  This way we can substitute images after the executable is generated if we need to change them.  Once they are compiled into your application, then they are actually contained in your .frx files.

Really, the only way to get an exact look to Explorer is to use the Treeveiw that comes with VB, because that is the common control that your operating system uses as well.  Unfortunately, to get it to perform like Explorer, you need to get down and boogie at the Windows API level, and even then you won't get exactly the same results, because Microsoft developed control to it in "c".  Once you know the API calls, there are many things you can do with the treeview control that aren't available to you under the properties exposed to VB.  But that's a major educational undertaking on its own!

I noted 9 optional treeview control systems in the lates VBXtras catalog for VB.  I won't live long enough to try them all.  YOu might look at the component toolbox OCX from DBI Technologies.  It looks like it may have the functionality you are looking for.
0
 

Author Comment

by:emmle
ID: 1436389
Thanks cymbolic for your time. I start thinking that a TreeView was a bad idea, even if I can't think of a better control to show a relationship between items. Unfortunately, I have not got that much time to look for other products.

Does anyone know any API calls that would do what I am looking for?
0
 

Expert Comment

by:jubertg
ID: 1436390
Give me some Specs and I'll try to figure out your problem.
0
 
LVL 4

Expert Comment

by:rantanen
ID: 1436391
Have you tried to use pictures which are the size you want (16x16 pix)? Most of the graphics supplied with VB are size 32x32 and they can't be resized using ImageList, but if you create your graphics at the required size both ImageList and TreeView work just fine. I know, it's additional work but that's the way it is.
0
 

Author Comment

by:emmle
ID: 1436392
rantanen,

I did try to use few graphics packages in order to resize the icons I wish to use (from 32x32 to 16x16) but could not do it (they still save them as 32x32).
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 4

Expert Comment

by:rantanen
ID: 1436393
emmle,

I think this time I can help you (good to be rejected once and while...stimulates thinking :-)

Two possibilities:

Manual way, as I said in my first answer. The only thing I forgot to mention is that I have used BMP's (sorry).

Programmatic way:

Actually Win95 icon files contain two versions of the icon - 32x32 pixs and 16x16 pixs. Unfortunately normal VB functions retrieve only the first one (32x32). But Win95 API contains a function LoadImage which can be used to load the second one.

Put following declarations where you keep your declarations

Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long

Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long
Const IMAGE_ICON = 1
Const LR_LOADFROMFILE = &H10

Then add something like following example code

Sub LoadIcons()
    Dim hIcon As Long
    Dim bSuccess As Boolean
    Dim x As ListImage
   
    ' Add 3 images to a ListImages collection.
    hIcon = LoadImage(0&, "G:\VB5\Graphics\Icons\Mail\Mail01a.ico", IMAGE_ICON, 16, 16, LR_LOADFROMFILE)
    Picture1.Cls
    bSuccess = DrawIcon(Picture1.hdc, 0, 0, hIcon)
    Set x = ImageList1.ListImages.Add(, , Picture1.Image)
    hIcon = LoadImage(0&, "G:\VB5\Graphics\Icons\Mail\Mail01b.ico", IMAGE_ICON, 16, 16, LR_LOADFROMFILE)
    Picture1.Cls
    bSuccess = DrawIcon(Picture1.hdc, 0, 0, hIcon)
    Set x = ImageList1.ListImages.Add(, , Picture1.Image)
    hIcon = LoadImage(0&, "G:\VB5\Graphics\Icons\Mail\Mail04.ico", IMAGE_ICON, 16, 16, LR_LOADFROMFILE)
    Picture1.Cls
    bSuccess = DrawIcon(Picture1.hdc, 0, 0, hIcon)
    Set x = ImageList1.ListImages.Add(, , Picture1.Image)
End Sub

In addition of your ImageList control (image size set to 16x16 pixs) you need a picturebox, which can be invisible.

If you have problems, make a comment and I can send you a working example.

0
 

Author Comment

by:emmle
ID: 1436394
rantanen,

This looks promising. Whereabout do I put "Sub LoadIcons()"?


0
 
LVL 4

Expert Comment

by:rantanen
ID: 1436395
That is the drawback you have suffer here, you can't fill the imagelist with your pictures during design...you have to do it during runtime (and supply the icon files with your app, too).

Good place to put the call to LoadIcons is your forms load event
0
 

Author Comment

by:emmle
ID: 1436396
rantanen,

I have tried what you suggested but it is not loading the icons at all. The gap between the lines within the Treeview is now smaller but no icons to be seen!
By the way I am using NT 4 rather than Win95 (although I can't see that it would make any difference!).
0
 

Author Comment

by:emmle
ID: 1436397
rantanen,

I am awfully sorry but I have to reject your answer as I think I have fixed it myself. It is so simple that I don't know why it took me so long to find the solution to my problem.
Initially I was having problem changing the size of the icons within my TreeView  from 32x32 to 16x16 in the General Tab of the ImageList properties. VB kept complaining ("Property is read-only if image list contains images").

The way to go around it is to empty the ImageList first, go back to the General Tab, change the Image size to 16x16, click on Apply and then reload the images. The TreeView will then use the new format.

Thank you so much for your time. Your help was much appreciated.

0
 
LVL 4

Expert Comment

by:rantanen
ID: 1436398
OK (and in some of these things NT *is* different).

Glad you found a solution.
0
 
LVL 2

Accepted Solution

by:
cathys earned 100 total points
ID: 1436399
Answer from Emmle Submitted by CathyS
The answer is: "In reality, VB 5 will let you change the icon size from 32x32 to 16x16 but only when the ImageList is empty. Otherwise, it will complain that the icons property is read-only and cannot be altered (which was the problem I was having to start with)".  
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now