WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

Working with Icons in Visual Basic
Pages: 1, 2, 3, 4

Using the ImageList Control

The ImageList control is a service component that provides stored images to interface objects capable of displaying them. In order to use the control, you'll have to add it to your project. It is included in the Microsoft Windows Common Controls library (comctl32.ocx). The control has one major restriction: Each image the control stores must be of the same size. If you want to store images of different sizes, you must use a separate control for each one. Once you select a size and add the first image, the size becomes read-only. The control stores its images in a ListImages collection, which consists of zero, one, or more ListImage objects. Each stored image is represented by a ListImage object.



Once you've added the ImageList control to a form, you can populate it with the icons you'd like the form to use. Typically, this is done in the design-time environment by right-clicking on the control and selecting Properties from its pop-up menu to open the control's Property Pages dialog box. (The Property window shows a different set of properties.) The General tab allows you to define the size of the images that the control will store. You should set it first, before actually assigning any images to the control. The Images tab, which is shown in Figure 3, lets you add pictures as well as define the one-based index of the image in the control's ListImages collection and the key to be assigned to the image. At runtime, you can retrieve a control from the ListImages collection's Item property by passing it either the index or the key of the image you want. Once you add an image to the control at design time, it is added to the form's .frx file; you no longer have to include the original image file with your project.

Figure 3
Figure 3. The Images tab of the ImageList control's Property Pages dialog box

You can also add images to the control dynamically at runtime by calling the ListImages collection's Add method. Although this is possible, it has the same problems as dynamically assigning an icon file to a form's Icon property: A separate set of icon files that you might overlook and that users might inadvertently delete must be included with the application.

To assign an icon to the form's Icon or DragIcon property, you use the ListImage object's ExtractIcon method. The method has no parameters and returns an IPictureDisp object representing an icon, which you can then assign to the property.

You can, for instance, download the source code for a sample phases of the moon program, IconList1.exe, that, when minimized, shows the current phase of the moon. The code that determines which icon to display and then calls the ExtractIcon method is as follows:

Private Sub ShowMoonIcon(datPhase As Date)

   Dim intMonth As Integer, intCtr As Integer, intPhase As Phases
   Dim fIcon As Boolean
   Dim datArray As Variant, datNextArray As Variant

   intMonth = Month(datPhase) - 1

   ' Iterate array
   datArray = aDates(intMonth)
   datNextArray = aDates(intMonth + 1)

   For intCtr = 0 To 3
      If datPhase > datArray(intCtr) Then
         If intCtr <= 2 Then
            If datPhase < datArray(intCtr + 1) Then
               intPhase = intCtr
               fIcon = True
               Exit For
            End If
         Else
            If datPhase < datNextArray(0) Then
               intPhase = intCtr
               fIcon = True
            End If
         End If
      End If
   Next

   ' Iterate next array if not found
   If Not fIcon Then
      For intCtr = 0 To 2
         If datPhase > datNextArray(intCtr) Then
            If datPhase < datNextArray(intCtr + 1) Then
               intPhase = intCtr
               fIcon = True
               Exit For
            End If
         End If
      Next
   End If


   Me.Icon = ImageList1.ListImages.Item(intPhase + 1).ExtractIcon

End Sub

In addition to being able to extract images from the ImageList control, we can also form icons by overlaying one icon on another. Windows does this regularly, for instance, whenever we create a shortcut file, or whenever we copy a file using a drag-and-drop operation. Overlaid icons are formed by calling the ImageList control's Overlay method, which returns an overlaid icon in which one color of the overlaying icon can be defined as a mask color--that is, a transparent color that allows the overlaid icon's colors to show through. The syntax for Overlay is:

IPictureDisp = ImageList.Overlay(index1, index2)

where index1 is the index or key of the image to be overlaid, and index2 is the index or key of the overlaying image. Before calling the method and returning the image, you may want to define the mask color by setting the MaskColor property. You can assign it a color value constant--a value returned by the QBColor function or assigned by the RGB function.

There is one wrinkle to using the ImageList control to create an overlaid icon: Although the function returns an IPictureDisp object, the returned image can be directly assigned only to a picture control, not to an icon. In order to work around this difficulty, we have to dynamically add the returned image to an ImageList control, then call ExtractIcon to use it as an icon. The following code fragment illustrates how we might overlay the a custom icon with a Windows shortcut icon:

Set combIcon = ImageList1.Overlay("CUSTOMICON", "SHORTCUT")
ImageList1.ListImages.Add , "CUSTOMSHORT", combIcon
Me.Icon = ImageList1.ListImages("CUSTOMSHORT").ExtractIcon

Note the syntax of the ListImages collection's Add method. The first two arguments let you define the index of the new image (which we've omitted in this case, so that the image is added to the end of the collection) and the key of the new image. The third argument is an IPictureDisp object, in this case the image returned by the ImageList control's Overlay method.

Pages: 1, 2, 3, 4

Next Pagearrow