-
Mar 10th, 2012, 09:55 PM
#1
Thread Starter
Addicted Member
How to use up to 128x128 alpha channel icons as the application and form icons in VB6
These instructions allow you to set the application icon, any form's icon, the ALT+TAB icon and the taskbar icon to a non standard icon including alpha channel icons containing images up to 128x128 pixels.
1. Get the Resource Compiler files and copy them into a temporary directory
1a. Search your harddrive for the Resource Compiler files RC.exe and RCDLL.DLL, if you don't have them download the files by looking for the VB5 Resource Add-In at MSDN.
1b. Create the temporary directory C:\Temp\ResourceCompiler
1c. Copy the RC.exe and RCDLL.DLL files into the C:\Temp\ResourceCompiler directory
2. Copy an icon file into the same directory, and make sure that the maximum size of the largest icon image is 128x128 pixels, as icons that contain images that are 256x256 pixels will not work. (Download IcoFx version 1.6.4 for no charge if you need to check the icon).
3. Create a resource compiler script
3a. Open notepad or wordpad and copy the following 2 lines of text being careful to change the icon name from White.ico to the name of the icon you just copied into the C:\Temp\ResourceCompiler directory:
// Icons for this application
AAA ICON PRELOAD MOVEABLE C:\Temp\ResourceCompiler\White.ico
3b. Save the textfile with the name MakeAAAicon.RC
4. Compile the resource compiler script you just made.
4a. Open a command window (cmd.exe)
4b. Type: cd C:\Temp\ResourceCompiler
4c. Type: RC /r /fo AAAicon.res MakeAAAicon.RC
4d. You should now see the compiled AAAicon.res file appear in the directory.
5. Add the resource file to your project
5a. Right click the project in the project explorer on the top right hand side of the IDE and select Add -> Add File.
5b. Browse to the C:\Temp\ResourceCompiler directory and select the AAAicon.res file you just compiled.
5c. You should now see the resource file appear in the project explorer in the Related Documents section.
6. Add the following code to a new module:
Code:
Option Explicit
'---------------------------------------------------------------------
' Declaration needed for GetLastSystemError Function
Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Long, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Args As Any) As Long
Private Const MAX_PATH As Long = 260
Private Const LB_SETTABSTOPS As Long = &H192
Private Const FORMAT_MESSAGE_FROM_SYSTEM As Long = &H1000
Private Const FORMAT_MESSAGE_IGNORE_INSERTS As Long = &H200
Private Const FORMAT_MESSAGE_MAX_WIDTH_MASK As Long = &HFF
Private Const FORMAT_MESSAGE_ARGUMENT_ARRAY As Long = &H2000
'---------------------------------------------------------------------
' Declarations needed for the SetIcon Function
Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
Private Const SM_CXICON = 11
Private Const SM_CYICON = 12
Private Const SM_CXSMICON = 49
Private Const SM_CYSMICON = 50
Private Declare Function LoadImageAsString Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal uType As Long, ByVal cxDesired As Long, ByVal cyDesired As Long, ByVal fuLoad As Long) As Long
Private Const LR_DEFAULTCOLOR = &H0
Private Const LR_MONOCHROME = &H1
Private Const LR_COLOR = &H2
Private Const LR_COPYRETURNORG = &H4
Private Const LR_COPYDELETEORG = &H8
Private Const LR_LOADFROMFILE = &H10
Private Const LR_LOADTRANSPARENT = &H20
Private Const LR_DEFAULTSIZE = &H40
Private Const LR_VGACOLOR = &H80
Private Const LR_LOADMAP3DCOLORS = &H1000
Private Const LR_CREATEDIBSECTION = &H2000
Private Const LR_COPYFROMRESOURCE = &H4000
Private Const LR_SHARED = &H8000&
Private Const IMAGE_ICON = 1
Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_SETICON = &H80
Private Const ICON_SMALL = 0
Private Const ICON_BIG = 1
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Const GW_OWNER = 4
Private Declare Function GetWindowLongA Lib "user32" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Const GWL_HWNDPARENT As Long = -8&
Public Sub SetIcon(ByVal hWnd As Long, ByVal sIconResName As String, Optional ByVal bSetAsAppIcon As Boolean = True)
Dim i&, lhWndTop&, lres&, cx&, cy&, hIconLarge&, hIconSmall&
' LoadImage will not load application resoures in the IDE, as in IDE mode App.hInstance returns the hMod of the VB IDE itself.
' Thus hIconLarge will return 0 and report the error 1813, "The specified resource type cannot be found in the image file" in the IDE.
' However they will work properly in the compiled exe.
If InIDE = True Then Exit Sub ' Exit as unable to LoadImage if running in the IDE
' Find VB's hidden top level window
lhWndTop = hWnd
Do
i = i + 1
lres = GetWindow(lhWndTop, GW_OWNER) ' this will return the handle to the parent window, or 0 if it is a top level window
If lres <> 0 Then lhWndTop = lres
Loop Until lres = 0 Or i = 40
' get the dimensions of the system icons in pixels
cx = GetSystemMetrics(SM_CXICON)
cy = GetSystemMetrics(SM_CYICON)
hIconLarge = LoadImageAsString(App.hInstance, sIconResName, IMAGE_ICON, cx, cy, LR_LOADTRANSPARENT) ' load image from resource (sIconResName = name used in rc.exe script)
If hIconLarge = 0 Then MsgBox "Error in SetIcon calling LoadImage for large icon, " & GetLastSystemError ' must replace msgbox with proper error loging
SendMessageLong hWnd, WM_SETICON, ICON_BIG, hIconLarge ' set big icon for the form
If bSetAsAppIcon = True Then SendMessageLong lhWndTop, WM_SETICON, ICON_BIG, hIconLarge ' set the big icon for the app's parent window (ALT + TAB)
' get the dimensions of the small system icons in pixels
cx = GetSystemMetrics(SM_CXSMICON)
cy = GetSystemMetrics(SM_CYSMICON)
hIconSmall = LoadImageAsString(App.hInstance, sIconResName, IMAGE_ICON, cx, cy, LR_LOADTRANSPARENT) ' load image from resource (sIconResName = name used in rc.exe script)
If hIconSmall = 0 Then MsgBox "Error in SetIcon calling LoadImage for large icon, " & GetLastSystemError ' must replace msgbox with proper error loging
SendMessageLong hWnd, WM_SETICON, ICON_SMALL, hIconSmall ' set the small icon of the form
If bSetAsAppIcon = True Then SendMessageLong lhWndTop, WM_SETICON, ICON_SMALL, hIconSmall ' set the small icon for the app's parent window (taskbar)
End Sub
Private Function InIDE() As Boolean
On Error Resume Next
Debug.Print 1 / 0 ' error in IDE, however no debug in compiled exe thus no error in a compiled app.
If Err.Number <> 0 Then InIDE = True
On Error GoTo 0 ' necessary as there is a blue in VB6 which stops the error number from being reset to zero at the end of the function.
End Function
Private Function GetLastSystemError() As String
' Returns the API error if successful or a zero length string
Dim sError As String * MAX_PATH, lErrNum&, ret&
lErrNum = Err.LastDllError
ret = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_IGNORE_INSERTS, ByVal 0&, lErrNum, 0&, sError, Len(sError), 0)
If ret > 0 Then GetLastSystemError = "Error Number: " & lErrNum & vbCrLf & "Error Description: " & Left$(sError, ret)
End Function
7. Add code to the load events of the application's forms
7a. Add this code to the load event of the main form
SetIcon Me.hWnd, "AAA", True
7b. Add this code to the load event of all the other forms in the project
SetIcon Me.hWnd, "AAA", false
8. Check that it has worked.
The icon only displays in the compiled version of your application, so compile your application and see if your non standard icon is now your application. Also make sure it appears on all of the forms in your project, and when you click ALT+TAB when your application is running.
9. Don't forget to include the application icon in your installer appplication so that it uses a large version of your icon for the desktop shortcut.
Key Notes:
The icon displayed by explorer is the first icon resource saved in an VB6 executable. Alpha names come before numeric names, so setting the name of the icon to AAA ensures that it is used by explorer to represent your application. The Alt+Tab icon, the taskbar icons and the form icons are all set via the above code which uses the WM_SETICON message to set the window icons directly when the forms load.
Sources:
http://www.vbaccelerator.com/home/VB...ly/article.asp
http://www.vbaccelerator.com/home/VB...ol/article.asp
http://www.vbaccelerator.com/home/VB...XE/article.asp
http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
All men have an inherent right to life, the right to self determination including freedom from forced or compulsory labour, a right to hold opinions and the freedom of expression, and the right to a fair trial and freedom from torture. Be aware that these rights are universal and inalienable (cannot be given, taken or otherwise transferred or removed) although you do risk losing the aforementioned rights should you fail to uphold them e.g Charles Taylor; United Nations sources: http://www.un.org/en/documents/udhr/, http://www.ohchr.org/EN/Professional...ages/CCPR.aspx. Also Charles I was beheaded on the 30th of January of 1649 for trying to replace parliamentary democracy with an absolute monarchy, the same should happen to Dr Phil and Stephen Fry; source: http://www.vbforums.com/showthread.p...ute-Monarchism.
The plural of sun is stars you Catholic turkeys.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|