Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

LoadImage replaces Transparent Background with Black, how can I fix that?

346 views
Skip to first unread message

Andrés Giraldo

unread,
Jul 24, 2004, 4:06:19 PM7/24/04
to
Hi!

I've got an Icon File that has a transparent background and I need to set
this icon like the icon's form in run time. Whe I do this with LoadImage
function, the transparent background is replaced with black. I can't use a
resource file, I need to set the icon from an icon file, because this file
depends on the end user.

Any idea of how can I fix that?

Private Sub Form_Load()
Me.Icon = LoadPicture(App.Path & "\x.ico")
End Sub

Thanks!


Mike D Sutton

unread,
Jul 24, 2004, 4:37:46 PM7/24/04
to
> I've got an Icon File that has a transparent background and I need to set
> this icon like the icon's form in run time. Whe I do this with LoadImage
> function, the transparent background is replaced with black. I can't use a
> resource file, I need to set the icon from an icon file, because this file
> depends on the end user.

LoadImage() won't fill the background of an icon, I'm guessing though that you're using a 32-bit icon with an alpha channel which
isn't supported natively however this article may be of some use to you:
http://www.vbaccelerator.com/home/VB/Tips/Setting_the_App_Icon_Correctly/article.asp
Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: ED...@mvps.org
WWW: Http://www.mvps.org/EDais/


Andrés Giraldo

unread,
Jul 25, 2004, 12:54:38 AM7/25/04
to
Hi Mike!

Thanks for answer me, but the big proble to me is that I can't use a
resource file because the end user can create any icon file and the
application has to set that icon in the mdi main form, the icon is 16x16

Thanks for your help


"Mike D Sutton" <ED...@mvps.org> escribió en el mensaje
news:%23tXQU4b...@TK2MSFTNGP12.phx.gbl...

Mike D Sutton

unread,
Jul 25, 2004, 5:21:32 AM7/25/04
to
> Thanks for answer me, but the big proble to me is that I can't use a
> resource file because the end user can create any icon file and the
> application has to set that icon in the mdi main form, the icon is 16x16

It's a while since I've looked at the article, but doesn't it just take an API icon handle? (IIRC the icon was only saved in a
resource for convenience since it didn't need to be changed and re-loaded from disk every time.) If so then you can use the
LoadImage() API call to load that from disk rather than using VB's LoadPicture() which does the bit-depth conversion nastiness
behind the scenes.

Andrés Giraldo

unread,
Jul 25, 2004, 1:35:46 PM7/25/04
to
Hi Mike!

Thanks again for answer me!

I've never used LoadImage or LoadIcon to do that, and I'm not getting to
much info about that, could you give me an example please?

The icon file has this conditions:

- 16x16 size (and only that image device)
- 16 color depth
- transparent background

I create those icons with MS-Visual C++ Icon Editor.

Thanks for your help again!


"Mike D Sutton" <ED...@mvps.org> escribió en el mensaje

news:e0xZdjic...@TK2MSFTNGP11.phx.gbl...

Mike D Sutton

unread,
Jul 25, 2004, 3:11:59 PM7/25/04
to
> Thanks again for answer me!
>
> I've never used LoadImage or LoadIcon to do that, and I'm not getting to
> much info about that, could you give me an example please?
>
> The icon file has this conditions:
>
> - 16x16 size (and only that image device)
> - 16 color depth
> - transparent background
>
> I create those icons with MS-Visual C++ Icon Editor.
>
> Thanks for your help again!

'***
Private Declare Function LoadImage Lib "User32.dll" Alias "LoadImageA" ( _
ByVal hInst As Long, ByVal lpszName As String, ByVal uType As Long, _
ByVal cxDesirded As Long, ByVal cyDesired As Long, ByVal fuLoad As Long) As Long
Private Declare Function DrawIconEx Lib "User32.dll" (ByVal hDC As Long, _
ByVal xLeft As Long, ByVal yTop As Long, ByVal hIcon As Long, _
ByVal cxWidth As Long, ByVal cyWidth As Long, ByVal iStepIfAniCur As Long, _
ByVal hBrFlickerFreeDraw As Long, ByVal diFlags As Long) As Long
Private Declare Function DestroyIcon Lib "User32.dll" (ByVal hIcon As Long) As Long

Private Const IMAGE_ICON As Long = &H1
Private Const LR_LOADFROMFILE As Long = &H10
Private Const DI_NORMAL As Long = &H3

Private Sub Form_Load()
Dim hIcon As Long

Const IconFile As String = "X:\Path\File.ico"

Me.AutoRedraw = True

hIcon = LoadImage(App.hInstance, IconFile, IMAGE_ICON, 32, 32, LR_LOADFROMFILE)
Call DrawIconEx(Me.hDC, 0, 0, hIcon, 0, 0, 0, 0, DI_NORMAL)
Call DestroyIcon(hIcon)

hIcon = LoadImage(App.hInstance, IconFile, IMAGE_ICON, 16, 16, LR_LOADFROMFILE)
Call DrawIconEx(Me.hDC, 32, 0, hIcon, 0, 0, 0, 0, DI_NORMAL)
Call DestroyIcon(hIcon)
End Sub
'***

Andrés Giraldo

unread,
Jul 25, 2004, 3:27:22 PM7/25/04
to
Ok, I've got it, now I can draw the icon on the form... but how can I assign
that icon to the icon's form property?

Thanks!

"Mike D Sutton" <ED...@mvps.org> escribió en el mensaje

news:OX6QDtnc...@TK2MSFTNGP11.phx.gbl...

Mike D Sutton @ Work

unread,
Jul 26, 2004, 4:22:25 AM7/26/04
to
> Ok, I've got it, now I can draw the icon on the form... but how can I assign
> that icon to the icon's form property?

Wasn't that what the vbaccelerator article demonstrated?..

0 new messages