• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

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,
0
vbdev04
Asked:
vbdev04
  • 7
  • 4
  • 4
  • +3
1 Solution
 
jimbobmcgeeCommented:
Have you tried redrawing the form?
Or changing the icon while the form's visible property is false?
0
 
david_barkerCommented:
Have you tried changing the third parameter ColorDepth to vbLPVGAColor or vbLPColor ie.
Me.iCon = LoadPicture(iconFileName,,vbLPVGAColor )

0
 
vbdev04Author Commented:

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.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Erick37Commented:
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.
0
 
Shahid ThaikaSole ProprietorCommented:
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.
0
 
vbdev04Author Commented:

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,
0
 
Shahid ThaikaSole ProprietorCommented:
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?
0
 
zzzzzoocCommented:
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
0
 
vbdev04Author Commented:

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.

0
 
Erick37Commented:
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.
0
 
zzzzzoocCommented:
>>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.
0
 
vbdev04Author Commented:
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.
0
 
vbdev04Author Commented:

zzzzzooc, it worked as in loading the file but didnt fix the problem. Result was the same as using LoadLibrary.
0
 
Erick37Commented:
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?  
0
 
zzzzzoocCommented:
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).
0
 
vbdev04Author Commented:

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?
0
 
vbdev04Author Commented:

File size of this icon file appears to be large - 27KB.
0
 
zzzzzoocCommented:
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
0
 
Erick37Commented:
Hi vbdev04,

I just came across this article and thought it may be useful:
"Providing a proper VB Application Icon, Including Large Icons and 32-Bit Alpha Images"
http://www.vbaccelerator.com/home/VB/Tips/Setting_the_App_Icon_Correctly/article.asp

Have you tried running the compiled exe to see if the icon problem still exists?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 7
  • 4
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now