Solved

Problem with Assigning Icon at run-time

Posted on 2004-10-12
19
202 Views
Last Modified: 2012-05-05
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
Comment
Question by:vbdev04
  • 7
  • 4
  • 4
  • +3
19 Comments
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12287933
Have you tried redrawing the form?
Or changing the icon while the form's visible property is false?
0
 
LVL 6

Expert Comment

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

0
 

Author Comment

by:vbdev04
ID: 12288083

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
 
LVL 32

Expert Comment

by:Erick37
ID: 12288152
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
 
LVL 9

Expert Comment

by:Shahid Thaika
ID: 12288331
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
 

Author Comment

by:vbdev04
ID: 12289971

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
 
LVL 9

Expert Comment

by:Shahid Thaika
ID: 12290116
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
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 12294838
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
 

Author Comment

by:vbdev04
ID: 12297776

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 32

Expert Comment

by:Erick37
ID: 12297932
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
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 12298048
>>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
 

Author Comment

by:vbdev04
ID: 12298101
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
 

Author Comment

by:vbdev04
ID: 12298124

zzzzzooc, it worked as in loading the file but didnt fix the problem. Result was the same as using LoadLibrary.
0
 
LVL 32

Expert Comment

by:Erick37
ID: 12298169
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
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 12298432
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
 

Author Comment

by:vbdev04
ID: 12298503

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
 

Author Comment

by:vbdev04
ID: 12298520

File size of this icon file appears to be large - 27KB.
0
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 12298826
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
 
LVL 32

Accepted Solution

by:
Erick37 earned 250 total points
ID: 12314360
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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now