Click here to Skip to main content
Click here to Skip to main content

24 Bit Color Icon in System Tray

By , 15 Mar 2004
 

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

About the Author

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalwhy.memberJames 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.memberl a u r e n16 Mar '04 - 5:52 
james
the size of the icon bitmap has nothing to do with the bit-depth of each pixel
Wink | ;)

 

"there is no spoon"
biz stuff   about me
GeneralRe: why.memberMario M.16 Mar '04 - 6:51 
you're wrong !, each of the 256 pixels can have a 24-bit color pixel. you won't need to display more that 256 color icons, it's completly useless WTF | :WTF:
GeneralRe: why.memberl a u r e n16 Mar '04 - 6:58 
listen very carefully and think b4 u answer:
 
each pixel in ANY bitmap can be 24bit (ie, one of 16.7 million possible colors) as opposed to 8 bit (one of 256 possible colors)
 
what this means is that u can have nicely shaded icons etc ... admittedly not much use in the big scheme of things but certainly possible
 
so no ... not wrong
go read some basic grafics books
Smile | :)
 

"there is no spoon"
biz stuff   about me
GeneralRe: why.memberl a u r e n16 Mar '04 - 7:41 
ummmmm maybe u need to go read a basic grafix book too
 
the number of bits used to represent a pixel on the screen is totally independent of the number of pixels in the image
a 256 color image uses a lookup table to define the colors it can show ... in 256 color made the system takes many of those entries so u dont have complete freedom to define ur own 256 entries
 
i was an assembly language games programmer for quite a few years so i think i know what im talking about ... unless ive gotten completely stupid lately
 
??

 

"there is no spoon"
biz stuff   about me
GeneralRe: why.memberl a u r e n16 Mar '04 - 8:02 
yes but windows takes 20 of them for its system colors
so u only get to define 236 max

 

"there is no spoon"
biz stuff   about me
GeneralRe: why.memberJames Raine16 Mar '04 - 22:17 
OK. Let's take it as given that you lose 20 colours (which in XP you don't - apparently). Is it worth increasing to 24bit for the sake of 20 extra available colours? Wouldn't it be a better idea to just tweak the image?
GeneralRe: why.memberl a u r e n16 Mar '04 - 8:25 
whatever

 

"there is no spoon"
biz stuff   about me
GeneralRe: why.memberNeville Franks16 Mar '04 - 10:04 
I assume this wasn't a discussion with yourself and our erstwhile Mario M. deleted his posts.Sigh | :sigh:
 
Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com
 

GeneralRe: why.memberl a u r e n16 Mar '04 - 10:05 
Laugh | :laugh:
no i might be many things but mad enuff to talk to myself isnt one of them
 
weird

 

"there is no spoon"
biz stuff   about me

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 16 Mar 2004
Article Copyright 2004 by Muhammad Asif Khan
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid