Click here to Skip to main content
Click here to Skip to main content
Go to top

24 Bit Color Icon in System Tray

, 15 Mar 2004
Rate this:
Please Sign up or sign in to vote.
An article on displaying icons with more than 256 colors in system tray

Introduction

I have found a lot of articles on CodeProject on displaying a system tray icon, but none of them allowed me to display an icon with more than 256 colors (at least I did not find any one). I was in need to display icons having more than 256 colors. A little bit of effort with MSDN put me on the right deck. Since a lot has been done for system tray icons, I did not go for re-inventing the wheel, and used the Chris Maunder class of CSystemTray with very little modification (not relating to icon display, instead enabling menu update handlers to work out. You can insert the code below in Chris's class to provide an overloaded function for this). All the credits for that class goes to Chris Maunder.

By default Visual studio does not load an icon in its editor, if it contains more than 256 colors. If you import an icon with more than 256 colors, it loads it as bitmap resource. What you need to do is to extract icon from that bitmap resource. See the section "Using the code" to see detail, how it has been done.

Background

I had benefited a lot from CodeProject. I wished, I could have a contribution too. A very small opportunity came in my way, I cashed it on. I will try to put some more useful articles as soon as its possible for me.

Using the code

There is nothing tricky in this article. Create two member variable of type CBitmap and HICON in your class (here CSysTray24Dlg) along with an object of CSystemTray below:

CSystemTray m_TrayIcon;
HICON m_hTrayIcon;
CBitmap m_24bitBMP;

Now in OnInitDialog function extract the icon from the bitmap resource as follows:

if(m_24bitBMP.LoadBitmap(IDB_BITMAP_24BIT))
{
    ICONINFO icInfo;
    icInfo.fIcon = TRUE;
    icInfo.hbmMask = (HBITMAP) m_24bitBMP;
    icInfo.xHotspot = 0;
    icInfo.yHotspot = 0;
    icInfo.hbmColor = (HBITMAP) m_24bitBMP;
    m_hTrayIcon = CreateIconIndirect(&icInfo);
}

Now display the icon as below:

if (!m_TrayIcon.Create(NULL,// Parent window
    WM_ICON_NOTIFY, // Icon notify message to use
    "24 bit Icon", // tooltip
    m_hTrayIcon, // Icon to use
    IDR_POPUP_MENU)) // ID of tray icon
    return FALSE;

Remember to destroy the icon handle in destructor or when you don't need it as:

DestroyIcon(m_hTrayIcon);

That's it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Muhammad Asif Khan
Web Developer
Pakistan Pakistan
Masters of Computer Sciences from International Islamic University Islamabad, Pakistan
Love programming in C++ and VC.

Comments and Discussions

 
QuestionSetting Icon Externally at Runtime ?? PinmemberManni Singh8-Jun-06 2:14 
GeneralA very useful piece of code Pinmemberdreadgod31-Mar-06 9:22 
GeneralIt doesn't work under w2k Pinmemberki11er28-Feb-05 6:43 
GeneralRe: It doesn't work under w2k PinmemberFrendie7-Nov-05 14:55 
GeneralRe: It doesn't work under w2k PinmemberThisIsANameOK14-Sep-06 13:56 
General8-bit and 24-bit not identical Pinmembermerlin9x916-Mar-04 8:12 
Generalwhy. PinmemberJames Raine16-Mar-04 4:14 
Isn't the tray icon a 16x16 icon? So you can only have 256 PIXELS - there is a limit on the number of colours from that.
GeneralRe: why. Pinmemberl a u r e n16-Mar-04 5:52 
GeneralRe: why. PinmemberMario M.16-Mar-04 6:51 
GeneralRe: why. Pinmemberl a u r e n16-Mar-04 6:58 
GeneralRe: why. Pinmemberl a u r e n16-Mar-04 7:41 
GeneralRe: why. Pinmemberl a u r e n16-Mar-04 8:02 
GeneralRe: why. PinmemberJames Raine16-Mar-04 22:17 
GeneralRe: why. Pinmemberl a u r e n16-Mar-04 8:25 
GeneralRe: why. PinmemberNeville Franks16-Mar-04 10:04 
GeneralRe: why. Pinmemberl a u r e n16-Mar-04 10:05 
GeneralRe: why. - It seems to be redundant Pinmembersemmel7116-Mar-04 7:54 
GeneralRe: why. - It seems to be redundant Pinmemberl a u r e n16-Mar-04 7:56 
GeneralRe: why. - It seems to be redundant Pinmembersemmel7116-Mar-04 8:09 
Generalyou just don't get it? Pinmembergaamoa16-Mar-04 9:55 
GeneralRe: you just don't get it? Pinmemberl a u r e n16-Mar-04 9:58 
GeneralRe: why. - It seems to be redundant PinmemberMuhammad Asif Khan16-Mar-04 19:07 
GeneralRe: why. - It seems to be redundant PinsussAnonymous28-Jan-05 0:25 
GeneralRe: why. PinmemberMuhammad Asif Khan16-Mar-04 18:55 
GeneralRe: why. Pinmembernoshbar18-Mar-04 3:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 16 Mar 2004
Article Copyright 2004 by Muhammad Asif Khan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid