Link to home
Start Free TrialLog in
Avatar of vbdev04
vbdev04

asked on

Problem with Assigning Icon at run-time

Hello,

I have an icon with transparent background. When I assign it to a form at design time, it looks fine but when I do the same at run-time, icon background appears black.

Me.iCon = LoadPicture(iconFileName)

Thanks,
Avatar of jimbobmcgee
jimbobmcgee
Flag of United Kingdom of Great Britain and Northern Ireland image

Have you tried redrawing the form?
Or changing the icon while the form's visible property is false?
Avatar of david_barker
david_barker

Have you tried changing the third parameter ColorDepth to vbLPVGAColor or vbLPColor ie.
Me.iCon = LoadPicture(iconFileName,,vbLPVGAColor )

Avatar of vbdev04

ASKER


Icon is set in the Form_Load and it doesn't change later.

Tried following without luck

Private Sub Form_Load()

    Me.Visible = False
    Me.iCon ...
    Me.Visible = True

I can use an icon without transparent background but then I will have to maintain two icons and the icon wont use title bar's background color.
Does this happen with any .ICO file or only a specific one?  Try searching your drive for all .ICO files and test with another one.  Usually MSOffice has MSN.ico that is a simple 256 color icon you can test with.
In the desktop properties, are you sure you have the 'Show Icon using all possible colors' menu checked. This may be causing the problem. Maybe visual basic shows your icon using all colours during runtime, but Windows may not.
Avatar of vbdev04

ASKER


David,

LoadPicture with vbLPColor removes the blank background but the colors are off. If I set the same iCon at design time, there is no problm with colors. I suspect the LoadPicture can't handle more than 256 colors.

I tried with MSN.ico as Eric suggested. It shows up properly both ways. Is there a requirement that the iCon can only be 256 colors?

Thanks,
No, the icon can have and show more than 256 colours. You just have to have the 'Show icons using all colors' option checked in the desktop properties window. Did you try checking it?
LoadPicture() may/may not limit icons to 256 colors. I'm not sure but you can try the below instead.


Form1:
-----------------
Option Explicit

Private Type GUID
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(7) As Byte
End Type

Private Type PictDesc
    Size As Long
    Type As Long
    hBmp As Long
    hPal As Long
    Reserved As Long
End Type

Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal dwImageType As Long, ByVal dwDesiredWidth As Long, ByVal dwDesiredHeight As Long, ByVal dwFlags As Long) As Long
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (lpPictDesc As PictDesc, riid As GUID, ByVal fPictureOwnsHandle As Long, ipic As IPicture) As Long

Private Const IMAGE_ICON = 1
Private Const LR_LOADFROMFILE = &H10
Private Function LoadIcon(ByVal strFile As String, ByVal intWidth As Integer, ByVal intHeight As Integer) As IPicture
    Dim lngIcoHandle As Long
    Dim typPictDest As PictDesc, typGUID As GUID
    lngIcoHandle = LoadImage(App.hInstance, strFile, IMAGE_ICON, intWidth, intHeight, LR_LOADFROMFILE)
    If lngIcoHandle <> 0 Then
        With typPictDest
            .Size = Len(typPictDest)
            .Type = vbPicTypeIcon
            .hBmp = lngIcoHandle
        End With
        With typGUID
            .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
        Call OleCreatePictureIndirect(typPictDest, typGUID, 1, LoadIcon)
        Call DeleteObject(lngIcoHandle)
    End If
End Function
Private Sub Form_Load()
    Me.Icon = LoadIcon("c:\1.ico", 32, 32)
End Sub
Avatar of vbdev04

ASKER


Thanks zzzzzooc. But that didn't work.

eeshahidt, application icon shows up properly on desktop, file explorer and task bar. It is the icon at left top corner of application window and sub forms that has this problem. Also note that icon shows up properly if I load it at design time.

How many icons are in the ICO file?

You should have at least a 16x16 256 color and a 32x32 256 color to support systems that cannot display fuill color icons.  You can also add your 16x16 and 32x32 full color icons to the file as well.

The full color icons really only make a difference in the larger sizes for the desktop.
>>But that didn't work.
Where/how did it fail? Worked fine for me and I experienced the same issue with icons that had more than 256 colors (solid black background). LoadPicture() couldn't load it but the above did.
Avatar of vbdev04

ASKER

Just one and it is 16x16. I get an error when I try to load it into resource file. msn.ico got added without any error.

I am working on XP.
Avatar of vbdev04

ASKER


zzzzzooc, it worked as in loading the file but didnt fix the problem. Result was the same as using LoadLibrary.
You should have both 16x16 and 32x32, and at least one set in 256 color.

What tool are you using to create the ICO file?  
Did you load the icon using 32x32 as I did in my example? It doesn't seem to work at 16x16 but works for 32x32 (even with 16x16 icons).
Avatar of vbdev04

ASKER


Yes I copied your code as is. Just changed the file name.

I get the icon from someone who is making it for me. Visual Studio .Net opens it in edit mode but I could not find a way to change the color depth. Any suggestions?
Avatar of vbdev04

ASKER


File size of this icon file appears to be large - 27KB.
Wow.. 27kb for the icon? It should be less than 10k.

Try downloading Microangelo:
http://www.download.com/Microangelo-Value-Pack/3000-2195-10296500.html?tag=lst-0-1
ASKER CERTIFIED SOLUTION
Avatar of Erick37
Erick37
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial