Click here to Skip to main content
15,860,972 members
Articles / Programming Languages / C++
Article

24 Bit Color Icon in System Tray

Rate me:
Please Sign up or sign in to vote.
2.08/5 (11 votes)
15 Mar 20042 min read 109.6K   1.2K   26   25
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


Written By
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 ?? Pin
MacGadger8-Jun-06 2:14
MacGadger8-Jun-06 2:14 
GeneralA very useful piece of code Pin
dreadgod31-Mar-06 9:22
dreadgod31-Mar-06 9:22 
GeneralIt doesn't work under w2k Pin
ki11er28-Feb-05 6:43
ki11er28-Feb-05 6:43 
GeneralRe: It doesn't work under w2k Pin
Frendie7-Nov-05 14:55
Frendie7-Nov-05 14:55 
GeneralRe: It doesn't work under w2k Pin
SonicMouse14-Sep-06 13:56
SonicMouse14-Sep-06 13:56 
General8-bit and 24-bit not identical Pin
merlin9x916-Mar-04 8:12
merlin9x916-Mar-04 8:12 
If I recall correctly, versions of Windows previous to XP display notification icons in something like 16 colors no matter what you try to do.

Even if that's not correct, there are still more issues here. Theoretically, yes, an 8x8 image can have 256 unique colors at most. But, yes, up to 20 are possibly stolen from you by the system (you may be using some of those by coincidence or design). And, you lose 1 for transparency (that may be 1 of the 20...I don't quite remember off the top of my head). So, even right there, there's a difference between a 8x8 icon at 24-bit and 8-bit. Now, the technique for displaying 24-bit icons is identical to displaying 32-bit icons. As we all know, Windows XP finally allows 32-bit icons. So, if we plug in a high-quality, antialiased icon, we'll get nice smooth edges rather than the raggedy, ugly ones we'd get with an 8-bit icon.

I believe that the article's title and content should be updated to show the use of 32-bit icons.
Generalwhy. Pin
James Raine16-Mar-04 4:14
James Raine16-Mar-04 4:14 
GeneralRe: why. Pin
l a u r e n16-Mar-04 5:52
l a u r e n16-Mar-04 5:52 
GeneralRe: why. Pin
Mario M.16-Mar-04 6:51
Mario M.16-Mar-04 6:51 
GeneralRe: why. Pin
l a u r e n16-Mar-04 6:58
l a u r e n16-Mar-04 6:58 
GeneralRe: why. Pin
l a u r e n16-Mar-04 7:41
l a u r e n16-Mar-04 7:41 
GeneralRe: why. Pin
l a u r e n16-Mar-04 8:02
l a u r e n16-Mar-04 8:02 
GeneralRe: why. Pin
James Raine16-Mar-04 22:17
James Raine16-Mar-04 22:17 
GeneralRe: why. Pin
l a u r e n16-Mar-04 8:25
l a u r e n16-Mar-04 8:25 
GeneralRe: why. Pin
Neville Franks16-Mar-04 10:04
Neville Franks16-Mar-04 10:04 
GeneralRe: why. Pin
l a u r e n16-Mar-04 10:05
l a u r e n16-Mar-04 10:05 
GeneralRe: why. - It seems to be redundant Pin
semmel7116-Mar-04 7:54
semmel7116-Mar-04 7:54 
GeneralRe: why. - It seems to be redundant Pin
l a u r e n16-Mar-04 7:56
l a u r e n16-Mar-04 7:56 
GeneralRe: why. - It seems to be redundant Pin
semmel7116-Mar-04 8:09
semmel7116-Mar-04 8:09 
Generalyou just don't get it? Pin
gaamoa16-Mar-04 9:55
gaamoa16-Mar-04 9:55 
GeneralRe: you just don't get it? Pin
l a u r e n16-Mar-04 9:58
l a u r e n16-Mar-04 9:58 
GeneralRe: why. - It seems to be redundant Pin
Muhammad Asif Khan16-Mar-04 19:07
Muhammad Asif Khan16-Mar-04 19:07 
GeneralRe: why. - It seems to be redundant Pin
Anonymous28-Jan-05 0:25
Anonymous28-Jan-05 0:25 
GeneralRe: why. Pin
Muhammad Asif Khan16-Mar-04 18:55
Muhammad Asif Khan16-Mar-04 18:55 
GeneralRe: why. Pin
noshbar18-Mar-04 3:49
noshbar18-Mar-04 3:49 

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

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