Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Icon Backround 2

Status
Not open for further replies.

ICISYD

Programmer
Dec 18, 2002
36
GB
I am using the DrawIcon api from "user32.dll" to put an icon into a picture box, for use in an imagelist. Unfortunately when doing this the icon looses all areas of transparency and ends up with a white backround. Is there any way I can fix this? Here is the code that does it:

Sub AddIcon(ByVal sExecutable As String, ByVal lIconNumber As String, ByVal sExtension As String)
Dim lIcon As Long
Dim lError As Long
frmBrowser.picTemp.Picture = LoadPicture("")
lIcon = ExtractIcon(App.hInstance, sExecutable, lIconNumber)
lError = DrawIcon(frmBrowser.picTemp.hdc, 0, 0, lIcon)
lError = DestroyIcon(lIcon)
frmBrowser.picTemp.Picture = frmBrowser.picTemp.Image
frmBrowser.imglIcons.ListImages.Add , sExtension, frmBrowser.picTemp.Picture
End Sub

Any help would be much appreciated.
 
You should use the OleCreatePictureIndirect function to convert the icon handle returned by ExtractIcon function to a picture object.

The DrawIcon method looses the transparency of the icon because the picture object returned by the image property of the picture box is not of type icon, but type bitmap which looses tranparency.

On the other hand the following function, IconHandleToPicture preserves the transparency. It does not even need a picture box to work with. See the example code that follows.

Place an image list (ImageList1) on the form and insert the following code.
___
[tt]
Private Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (lpPictDesc As PICTDESC, riid As Any, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
Private Declare Function CopyIcon Lib "user32" (ByVal hIcon As Long) As Long
Private Declare Function CLSIDFromString Lib "ole32" (ByVal lpstrCLSID As Long, lpCLSID As Any) As Long
Private Type PICTDESC
cbSizeofStruct As Long
picType As Long
hImage As Long
xExt As Long
yExt As Long
End Type
Public Function IconHandleToPicture(ByVal hIcon As Long) As IPicture
Dim pd As PICTDESC, IPic(15) As Byte
If hIcon = 0 Then Exit Function
pd.cbSizeofStruct = Len(pd)
pd.picType = vbPicTypeIcon
pd.hImage = hIcon
CLSIDFromString StrPtr("{7BF80980-BF32-101A-8BBB-00AA00300CAB}"), IPic(0)
OleCreatePictureIndirect pd, IPic(0), True, IconHandleToPicture
End Function

Private Sub Form_Click()
BackColor = vbWhite * Rnd
End Sub

Private Sub Form_Load()
Dim hIcon As Long

'extract icon handle from dll
hIcon = ExtractIcon(0, "user32.dll", 1)

'convert icon handle to picture
'and add it to the image list
ImageList1.ListImages.Add , , IconHandleToPicture(hIcon)

'assign to picture property the
'first image in the image list
Me.Picture = ImageList1.ListImages(1).Picture

Randomize
End Sub[/tt]
___

It will extract an icon from user32.dll, convert it to a picture object using IconHandleToPicture function preserving its transparency, add this picture to the image list and assign this image list picture to the picture property of the form.

When you click on the form, it will randomly change the background color of the form and you will see that this change in background color is also reflected in the transparent areas of the icon.

Note that you do not need to destroy the icon handle using DestroyIcon function. When the icon handle in converted to a picture object, the object instance takes care of the destruction of the icon itself.
 
Thanks for the help, it will be extremely useful.
 
Old Thread I know, but useful

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top