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
&nbsp &nbsp cbSizeofStruct As Long
&nbsp &nbsp picType As Long
&nbsp &nbsp hImage As Long
&nbsp &nbsp xExt As Long
&nbsp &nbsp yExt As Long
End Type
Private Type Guid
&nbsp &nbsp Data1 As Long
&nbsp &nbsp Data2 As Integer
&nbsp &nbsp Data3 As Integer
&nbsp &nbsp 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
&nbsp &nbsp
&nbsp &nbsp If hIcon = 0 Then Exit Function
&nbsp &nbsp &nbsp &nbsp
&nbsp &nbsp
&nbsp &nbsp Dim oNewPic As Picture
&nbsp &nbsp Dim tPicConv As PictDesc
&nbsp &nbsp Dim IGuid As Guid
&nbsp &nbsp
&nbsp &nbsp With tPicConv
&nbsp &nbsp .cbSizeofStruct = Len(tPicConv)
&nbsp &nbsp .picType = vbPicTypeIcon
&nbsp &nbsp .hImage = hIcon
&nbsp &nbsp End With
&nbsp &nbsp
&nbsp &nbsp
' Fill in magic IPicture GUID {7BF80980-BF32-101A-8BBB-00AA00300CAB}
&nbsp &nbsp With IGuid
&nbsp &nbsp &nbsp &nbsp .Data1 = &H7BF80980
&nbsp &nbsp &nbsp &nbsp .Data2 = &HBF32
&nbsp &nbsp &nbsp &nbsp .Data3 = &H101A
&nbsp &nbsp &nbsp &nbsp .Data4(0) = &H8B
&nbsp &nbsp &nbsp &nbsp .Data4(1) = &HBB
&nbsp &nbsp &nbsp &nbsp .Data4(2) = &H0
&nbsp &nbsp &nbsp &nbsp .Data4(3) = &HAA
&nbsp &nbsp &nbsp &nbsp .Data4(4) = &H0
&nbsp &nbsp &nbsp &nbsp .Data4(5) = &H30
&nbsp &nbsp &nbsp &nbsp .Data4(6) = &HC
&nbsp &nbsp &nbsp &nbsp .Data4(7) = &HAB
&nbsp &nbsp End With
&nbsp &nbsp OleCreatePictureIndirect tPicConv, IGuid, True, oNewPic
&nbsp &nbsp
&nbsp &nbsp Set IconToPicture = oNewPic
&nbsp &nbsp
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" _
&nbsp &nbsp (ByVal hInst As Long, ByVal lpsz As String, _
&nbsp &nbsp ByVal iType As Long, _
&nbsp &nbsp ByVal cx As Long, ByVal cy As Long, _
&nbsp &nbsp 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
&nbsp &nbsp
' Load an icon called Test.Ico from the directory:
&nbsp &nbsp
&nbsp &nbsp
' If the icon contains more than one size of image,
&nbsp &nbsp ' set cx and cy to the width and height to load
&nbsp &nbsp ' the appropriate image in:
&nbsp &nbsp hIcon = LoadImage(App.hInstance, App.Path & "\TEST.ICO", IMAGE_ICON, 0, 0, LR_LOADFROMFILE Or LR_LOADMAP3DCOLORS)
&nbsp &nbsp
' Set the picture to this icon:
&nbsp &nbsp 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).

&nbsp

Related Tips and Articles:

&nbsp

AboutContributeSend FeedbackPrivacy

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