The new vbAccelerator Site - more VB and .NET Code and Controls

Create a VB Picture from an API Icon Handle

Author:

Steve McMahon(steve@vbaccelerator.com)

Keywords:

API,GDI,Graphics

Updated:

12/10/98

Other Tips
All Tips
By Date
By Subject


API (33)
Bit
Manipulation (3)

Clipboard (3)
Combo
Box (5)

Desktop (3)
GDI (13)
Graphics (13)
Internet (2)
Interprocess
Comms (3)

Keyboard (2)
Mouse (1)
Shell (1)
Sprites (1)
Subclassing (3)
Text
Box (2)

Windows (11)
Windows
Controls (10)



Submit


This tip shows you how create a VB Picture object from an API icon handle. This is useful if you are dealing with real API icons.

Start a new project and add a module. Then add the following code to the module:

Option Explicit

Private Type PictDesc
    cbSizeofStruct As Long
    picType As Long
    hImage As Long
    xExt As Long
    yExt As Long
End Type
Private Type Guid
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(0 To 7) As Byte
End Type
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (lpPictDesc As PictDesc, riid As Guid, ByVal fPictureOwnsHandle As Long, ipic As IPicture) As Long

Public Function IconToPicture(ByVal hIcon As Long) As IPicture
   
    If hIcon = 0 Then Exit Function
       
   
    Dim oNewPic As Picture
    Dim tPicConv As PictDesc
    Dim IGuid As Guid
   
    With tPicConv
    .cbSizeofStruct = Len(tPicConv)
    .picType = vbPicTypeIcon
    .hImage = hIcon
    End With
   
   
' Fill in magic IPicture GUID {7BF80980-BF32-101A-8BBB-00AA00300CAB}
    With IGuid
        .Data1 = &H7BF80980
        .Data2 = &HBF32
        .Data3 = &H101A
        .Data4(0) = &H8B
        .Data4(1) = &HBB
        .Data4(2) = &H0
        .Data4(3) = &HAA
        .Data4(4) = &H0
        .Data4(5) = &H30
        .Data4(6) = &HC
        .Data4(7) = &HAB
    End With
    OleCreatePictureIndirect tPicConv, IGuid, True, oNewPic
   
    Set IconToPicture = oNewPic
   
End Function


To try out a the function, add a Command Button and a Picture Box to your project's form. Copy an icon to the project's directory, and rename it TEST.ICO.

Then add this code to the form:

Option Explicit

Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" _
    (ByVal hInst As Long, ByVal lpsz As String, _
    ByVal iType As Long, _
    ByVal cx As Long, ByVal cy As Long, _
    ByVal fOptions As Long) As Long
' iType options:
Private Const IMAGE_BITMAP = 0
Private Const IMAGE_ICON = 1
Private Const IMAGE_CURSOR = 2
' fOptions flags:
Private Const LR_LOADMAP3DCOLORS = &H1000
Private Const LR_LOADFROMFILE = &H10
Private Const LR_LOADTRANSPARENT = &H20


Private Sub Command1_Click()
Dim hIcon As Long
   
' Load an icon called Test.Ico from the directory:
   
   
' If the icon contains more than one size of image,
    ' set cx and cy to the width and height to load
    ' the appropriate image in:
    hIcon = LoadImage(App.hInstance, App.Path & "\TEST.ICO", IMAGE_ICON, 0, 0, LR_LOADFROMFILE Or LR_LOADMAP3DCOLORS)
   
' Set the picture to this icon:
    Set Picture1.Picture = IconToPicture(hIcon)
End Sub

Start the project. When you click the button, the icon will be loaded as an API icon handle, converted to a picture and then shown in the PictureBox. Whilst this simple example could be achieved more quickly using the LoadPicture method, the method shown has several advantages:

  • You are not restricted to 32x32 icons as you normally are with the Picture object. The LoadImage function can load icons of any size.
  • Similarly, you are not restricted to 16 colour icons. The project will load icons with 256 and millions of colour depths. (Note if the icon file contains both a 16 colour and a higher colour icon of the same size, the icon that is loaded will be the one which matches your system settings for icon colour depth).
  • You can specify that Gray (RGB 192,192,192) and Dark Gray (128,128,128) are mapped to the appropriate system colours (vbButtonFace and vbButtonShadow respectively).


 

Related Tips and Articles:

 
 

About  Contribute  Send Feedback  Privacy

Copyright © 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 12/10/98

g="0" cellspacing="0" width="100%">  

About  Contribute  Send Feedback  Privacy

Copyright © 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 12/10/98