Results 1 to 7 of 7

Thread: [RESOLVED] VB6 System Tray Icon Color Depth

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    5

    Resolved [RESOLVED] VB6 System Tray Icon Color Depth

    Hello, new to these forums, been using VB6 since 1998! Glad to be here


    I have an application that uses a tray icon, and for the life of me, I cannot get the icon to show more than 16 colors, which is completely ugly!

    This is the Module I am using to set the tray icon:
    Code:
          Public Type NOTIFYICONDATA
           cbSize As Long
           hWnd As Long
           uId As Long
           uFlags As Long
           uCallBackMessage As Long
           hIcon As Long
           szTip As String * 64
          End Type
    
          'constants required by Shell_NotifyIcon API call:
          Public Const NIM_ADD = &H0
          Public Const NIM_MODIFY = &H1
          Public Const NIM_DELETE = &H2
          Public Const NIF_MESSAGE = &H1
          Public Const NIF_ICON = &H2
          Public Const NIF_TIP = &H4
          Public Const WM_MOUSEMOVE = &H200
          Public Const WM_LBUTTONDOWN = &H201     'Button down
          Public Const WM_LBUTTONUP = &H202       'Button up
          Public Const WM_LBUTTONDBLCLK = &H203   'Double-click
          Public Const WM_RBUTTONDOWN = &H204     'Button down
          Public Const WM_RBUTTONUP = &H205       'Button up
          Public Const WM_RBUTTONDBLCLK = &H206   'Double-click
    
          Public Declare Function SetForegroundWindow Lib "user32" _
          (ByVal hWnd As Long) As Long
          Public Declare Function Shell_NotifyIcon Lib "shell32" _
          Alias "Shell_NotifyIconA" _
          (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
    
          Public nid As NOTIFYICONDATA
    and this is the command I am using to set the icon to the form:
    Code:
           With nid
            .cbSize = Len(nid)
            .hWnd = Me.hWnd
            .uId = vbNull
            .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
            .uCallBackMessage = WM_MOUSEMOVE
            .hIcon = Me.Icon
            .szTip = Me.Caption & vbNullChar
           End With
           
           Shell_NotifyIcon NIM_ADD, nid

    I know VB6 has some limitations when it comes to using icons, but I know there has to be a way to do this, and if anyone can help me out here, I would be forever in your debt.

    Thanks in advance for any help everyone!

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: VB6 System Tray Icon Color Depth

    There are a few solutions. But first... Older operating sytems are restricted to color depth also. I think Win2K was the first that supported more than 16 colors on the systray. XP and above support all levels. But I'm a little foggy on the history.

    1. Don't use VB's picture/icon properties. If you do, you''ll want to make that icon resource only consist of one icon of the color depth you want, you should be able to get up to true color. Keep in mind VB doesn't support 32bpp icons directly.

    2. Load your icons manually using LoadImage API. You'll want to destroy them also, or load them with the LR_Shared flag. Doing it this way, you should be able to get true alpha-blended icons on the systray.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    5

    Re: VB6 System Tray Icon Color Depth

    Quote Originally Posted by LaVolpe View Post
    There are a few solutions. But first... Older operating sytems are restricted to color depth also. I think Win2K was the first that supported more than 16 colors on the systray. XP and above support all levels. But I'm a little foggy on the history.

    1. Don't use VB's picture/icon properties. If you do, you''ll want to make that icon resource only consist of one icon of the color depth you want, you should be able to get up to true color. Keep in mind VB doesn't support 32bpp icons directly.

    2. Load your icons manually using LoadImage API. You'll want to destroy them also, or load them with the LR_Shared flag. Doing it this way, you should be able to get true alpha-blended icons on the systray.
    Fantastic! Thank you! I have gotten it perfect to load from an external file, here is my code, green highlighted to show how to use the function:

    Code:
    Public Function LoadIconFromFile(FileName)
        Dim IconFile As Long
        If FileName = "" Then Exit Function
        IconFile = LoadImage(0, FileName, IMAGE_ICON, 0, 0, LR_LOADFROMFILE)
        If IconFile > 0 Then LoadIconFromFile = IconFile
    End Function
    
    Form Load()
           With nid
            .cbSize = Len(nid)
            .hWnd = Me.hWnd
            .uId = vbNull
            .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
            .uCallBackMessage = WM_MOUSEMOVE
            .hIcon = LoadIconFromFile(App.Path & "\100.ico")
            .szTip = Me.Caption & vbNullChar
           End With
    End Sub


    HOWEVER, Is there ANY way I can do this from an included Resource file? For example, I add the same ICO file as a "CUSTOM" resource named "101"?? This is the last step to accomplish my problem, just the same as my original question, but loading from a resource file in my app.


    Thanks again, you guys are great!!!

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: VB6 System Tray Icon Color Depth

    Yes. Look at this link. If the icon you loaded into your custom section of the res file contains multiple icons within the file, you'll need to know which one you want to load. Dim a byte array and make it hold the result of a LoadResData() call. Use that array in the link I provided.

    Edited: I don't recall if NIM_DELETE deletes the icon (handle) for you or whether you need to delete it yourself. You may want to research that so 1) you don't create a memory leak by not destroying something you created, or 2) expect it to remain usable after NIM_Delete, but it is actually destroyed.
    Last edited by LaVolpe; Feb 10th, 2011 at 06:22 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    5

    Re: VB6 System Tray Icon Color Depth

    Quote Originally Posted by LaVolpe View Post
    Yes. Look at this link. If the icon you loaded into your custom section of the res file contains multiple icons within the file, you'll need to know which one you want to load. Dim a byte array and make it hold the result of a LoadResData() call. Use that array in the link I provided.

    Edited: I don't recall if NIM_DELETE deletes the icon (handle) for you or whether you need to delete it yourself. You may want to research that so 1) you don't create a memory leak by not destroying something you created, or 2) expect it to remain usable after NIM_Delete, but it is actually destroyed.
    Wow, absolutely perfect! Thank you so much, THAT WORKED!! You are a life saver!!

    Due to my lack of that API knowledge, I was only able to get it to load an icon with one image in it.... would you be so kind as to show me how to do this with a multiple icon file with my working code below?

    Just for example sake, lets say i have an ICO file with 8 images in it and I want to load the 5th....

    Here is my code I got to work with the resource with one icon in the file, what to modify to make it load one with multiple images:

    Code:
    Public Function LoadIconFromRES(ResID, ResName)
        Const ICRESVER As Long = &H30000
        Dim IconFile As Long
        Dim IconRes() As Byte
        Dim hIcon As Long
        
        'Load the icon from desired resource
        IconRes= LoadResData(ResName, ResID)
    
        'Create the Icon File
        hIcon = CreateIconFromResourceEx(IconRes(22), UBound(IconRes) - 21&, True, ICRESVER, 16, 16, 0&)
        
        'If there is data, set the icon to the desired source
        If hIcon > 0 Then LoadIconFromRES = hIcon
        
    End Function

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    5

    Re: VB6 System Tray Icon Color Depth

    Well, I am pretty sure I got this right, if you could take a peek at it and deem it correct if its right....it seems to work great! Each time I make a change to the 'IconIndex' it changes the icon in order from how it is in the file.



    Here is the working code so far, and once again THANK YOU SO MUCH, I have been after this for at least a week now!

    Code:
    Public Function LoadIconFromMultiRES(ResID, ResName, IconIndex, Optional PixelsX = 16, Optional PixelsY = 16)
        Const ICRESVER As Long = &H30000
        Dim IconFile As Long
        Dim IconRes() As Byte
        Dim hIcon As Long
        Dim lDirPos As Long, lSize As Long, lBMPpos As Long
        
        'Load the icon from desired resource
        IconRes = LoadResData(ResName, ResID)
        
        'Grab the chosen icon from the file Index; 0 = 1st Icon
        lDirPos = 6 + (IconIndex) * 16
    
        CopyMemory lSize, IconRes(lDirPos + 8), 4&
    
        CopyMemory lBMPpos, IconRes(lDirPos + 12), 4&
    
        'Create the Icon File
        hIcon = CreateIconFromResourceEx(IconRes(lBMPpos), UBound(IconRes) - 21&, True, ICRESVER, PixelsX, PixelsY, 0&)
        
        'If there is data, set the icon to the desired source
        If hIcon > 0 Then LoadIconFromMultiRES = hIcon
    End Function
    If there is anything i can do to repay you in some way, please let me know.

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: VB6 System Tray Icon Color Depth

    Glad you got it working and glad you answered your own question. If you didn't I was about to point you right back to the link & say read the 2nd post. Anyway, you are good to go with minor adjust mentioned below. Just ensure the icon index you pass is zero-bound and doesn't exceed the number of icons in the file, less one. Kind of how a ListBox .ListIndex works.

    Use the Size you are calculating. Change your main call to this:
    Code:
    hIcon = CreateIconFromResourceEx(IconRes(lBMPpos), lSize, True, ICRESVER, PixelsX, PixelsY, 0&)
    hIcon will fail to be created in these cases
    1) corrupt image data or invalid entries within the file
    2) trying to load an embedded PNG within the file on a system of XP or lower. Ok on Vista+
    3) trying to load icon from an animated cursor file
    4) won't fail, but loading an alphablended icon on a system lower than XP will display a lousy copy
    Actually the code I provided in that link & what you have above, as-is, will load cursors as icons. That is what I think VB does also, whenever you select a cursor image.

    As far as repayment goes... The big thank you was quite enough.
    Don't forget to mark your thread resolved, using the Thread Tools near top of your first post.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

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



Click Here to Expand Forum to Full Width