How to use up to 128x128 alpha channel icons as the application and form icons in VB6-VBForums
Results 1 to 4 of 4

Thread: How to use up to 128x128 alpha channel icons as the application and form icons in VB6

  1. #1

    Thread Starter
    Addicted Member Witis's Avatar
    Join Date
    Jan 2011
    Location
    VB Forums Online Freedom Mode: Operational
    Posts
    213

    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.

  2. #2
    Addicted Member
    Join Date
    Jul 2010
    Posts
    146

    Re: How to use up to 128x128 alpha channel icons as the application and form icons in

    Cool.

    That successfully set the form icon, which has been bugging me for a while. But Windows Explorer still isn't displaying the full size icon. It made it into the file, not sure why Explorer doesn't show it- any ideas?

    16 x 16 (256 colors) - Ordinal name: 1
    128 x 128 (16.8mil colors) - Ordinal name: 2
    96 x 96 (16.8mil colors) - Ordinal name: 3
    72 x 72 (16.8mil colors) - Ordinal name: 4
    64 x 64 (16.8mil colors) - Ordinal name: 5
    48 x 48 (16.8mil colors) - Ordinal name: 6
    32 x 32 (16.8mil colors) - Ordinal name: 7
    24 x 24 (16.8mil colors) - Ordinal name: 8
    16 x 16 (16.8mil colors) - Ordinal name: 9

    Attached is what Explorer shows.


    PS- How would one go about adding the icon to an existing resource file (VB says you're only allowed one per project).
    Attached Images Attached Images  
    Last edited by fafalone; Mar 14th, 2012 at 12:56 AM.

  3. #3

    Thread Starter
    Addicted Member Witis's Avatar
    Join Date
    Jan 2011
    Location
    VB Forums Online Freedom Mode: Operational
    Posts
    213

    Re: How to use up to 128x128 alpha channel icons as the application and form icons in

    Quote Originally Posted by fafalone View Post
    Cool.

    That successfully set the form icon, which has been bugging me for a while. But Windows Explorer still isn't displaying the full size icon. It made it into the file, not sure why Explorer doesn't show it- any ideas?

    16 x 16 (256 colors) - Ordinal name: 1
    128 x 128 (16.8mil colors) - Ordinal name: 2
    96 x 96 (16.8mil colors) - Ordinal name: 3
    72 x 72 (16.8mil colors) - Ordinal name: 4
    64 x 64 (16.8mil colors) - Ordinal name: 5
    48 x 48 (16.8mil colors) - Ordinal name: 6
    32 x 32 (16.8mil colors) - Ordinal name: 7
    24 x 24 (16.8mil colors) - Ordinal name: 8
    16 x 16 (16.8mil colors) - Ordinal name: 9

    Attached is what Explorer shows.
    Hi, I get the same result - if I set the icon size in explorer to large or extra large explorer does not display the larger sized icons in the icon file. At this stage I am not sure how to make explorer access and display the larger sized icons which is likely due to a VB6 limitation rather than a problem with the icons.

    Quote Originally Posted by fafalone View Post
    PS- How would one go about adding the icon to an existing resource file (VB says you're only allowed one per project).
    Yes, as you can only add one resource file per VB6 project you must create an RC script for all the resources that are used by your application.

    For example this RC script adds the custom icon for your application and the application manifest:
    Code:
    // Icons for this application
    AAA ICON PRELOAD MOVEABLE C:\Temp\ResourceCompiler\WhiteHDD5.ico
    
    // Manifest for this application
    #define CREATEPROCESS_MANIFEST_RESOURCE_ID 1
    #define RT_MANIFEST 24 
    CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST C:\Temp\ResourceCompiler\Checking\Project1.exe.manifest
    Then add the rest of your binary resources e.g.
    ViewScreen BITMAP C:\BITMAPS\VIEWSCRN.BMP
    SpaceShip BITMAP C:\BITMAPS\SPACESHIP.BMP
    Explosion SOUND C:\SOUNDS\EXPLOSION.WAV
    CutScene VIDEO C:\AVIS\CUTSCENE.AVI
    StartJPG CUSTOM E:\Resources\Startup.jpg

    And Strings e.g.
    STRINGTABLE
    BEGIN
    100, "My apps title"
    101, "Copyright etc"
    END

    To load binary resources in your Visual Basic code:
    use the LoadResPicture(index, format) for bitmaps, icons, and cursors, and
    use the LoadResData function to load wave files, AVI files and custom files.

    For string resources use the LoadResString(index) function.

    Note:
    Although the Visual Basic Help file says "The data that LoadResData loads from the resource file can be up to 64K," this is a limitation for the 16- bit versions of Visual Basic. The 32-bit versions of Visual Basic have no problem reading larger amounts of data.

    To use a custom resource, you can simply use the LoadResData function to retrieve the resource into a byte array, then save the byte array to a temporary file, use the file, and then delete the temporary file.
    Sources:
    http://www.vbaccelerator.com/home/VB...XE/article.asp
    http://support.microsoft.com/kb/194409
    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.

  4. #4
    Member
    Join Date
    Oct 2011
    Posts
    53

    Re: How to use up to 128x128 alpha channel icons as the application and form icons in

    Hi friends,

    I wanna ask a question.


    3. Create a resource compiler script

    I didn't understand this step

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.