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   
QuestionSetting Icon Externally at Runtime ??memberManni Singh8 Jun '06 - 2:14 
Hey this is gr8 , butt i cant Set Iconss on the Fly (on the Run) i mean to say i want to Change the Icon at Runtime from my Harddrive SetIcon"C:\\test.ico") ... like tht.. plzz tell mee how s that possibel !!! plzzzzzzzzzzzz
GeneralA very useful piece of codememberdreadgod31 Mar '06 - 9:22 
Direct and explicit, very handy. :->
 
Sanity is the greatest peril for genius
GeneralIt doesn't work under w2kmemberki11er28 Feb '05 - 6:43 
I've made tons of tests but with no success though.
The code shows 4bit (16 colors) tray icon only. Frown | :(
 
Any ideas?
 
Thanks a lot.
GeneralRe: It doesn't work under w2kmemberFrendie7 Nov '05 - 14:55 
yeah.. I'm having the same problem as well.
 
Anyone can help?Sigh | :sigh:
 

GeneralRe: It doesn't work under w2kmemberThisIsANameOK14 Sep '06 - 13:56 
yeah, i tried everything.. i think the win2k system tray is one of those retardations that got carried over from previous windows versions.
 
The winxp+ system tray is all new programming since you can have alpha-transparent icons in it... so 24bit images are allowed.
General8-bit and 24-bit not identicalmembermerlin9x916 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.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
GeneralRe: why. - It seems to be redundantmembersemmel7116 Mar '04 - 7:54 
"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)"

Do not forget: The 8-bit bitmap comes with a color palette which contains...

...right: 256 entries, each "one of 16.7 million possible colors".
And because accidently the 8-bit icon has as many pixels as color palette entries (16x16=256), EACH pixel can have a different color "of 16.7 million possible colors". So there is really no use for 24-bit tray icons!Smile | :)
 
Whats more frustrating is the 16-color VGA palette Mad | :mad: used for tray icons in Win 2000 and before!!
GeneralRe: why. - It seems to be redundantmemberl a u r e n16 Mar '04 - 7:56 
uhhhhhhhh and the system colors?
that means u lose at least 20 palette entries
so no u dont have enuff to show the thing as it would be in non-256 color mode

 

"there is no spoon"
biz stuff   about me
GeneralRe: why. - It seems to be redundantmembersemmel7116 Mar '04 - 8:09 
I can only tell from practice:
WinXP does not complain if you load a palette icon in the tray which does not have the 20 system colors included, nor does XP replace my palette colors with the system colors. It looks just as in the icon editor Smile | :)

If you dynamically create the icon within your application you use a color DDB to draw on. It has a color organisation depending on the current graphics mode, which will be usually your 32 or 24-bit true color resolution. (In Win 2000 this DDB gets converted onto the 16-color VGA palette Frown | :( in WinXP not.) So here there is even no other choice then to use a true-color bitmap! Thats why I do not get the point of the article.

Generalyou just don't get it?membergaamoa16 Mar '04 - 9:55 
what you say is complete bull because:
 
you lose 20 palette entries if AND ONLY IF you're using a paletted 8-bit display mode, in which case there is no way of displaying a 24-bit icon anyway. and don't tell me anybody serious still uses these display modes.
 
in all other (real-color) display modes, there is no such thing as system palette entries because there is no palette. all colors are represented as their RGB values in video memory, as are the pixels of your icon.
GeneralRe: you just don't get it?memberl a u r e n16 Mar '04 - 9:58 
yes
thats what i was talking about

 

"there is no spoon"
biz stuff   about me
GeneralRe: why. - It seems to be redundantmemberMuhammad Asif Khan16 Mar '04 - 19:07 
Each bitmap pixel contains color ranging from 0 to 2^NoOfBits. i.e an 24 bit bitmap pixels contain color range from 0 to 2^24 but what to say about icons. Do u anticipate the same behaviour from icons having more than 256 colors?
GeneralRe: why. - It seems to be redundantsussAnonymous28 Jan '05 - 0:25 
semmel71 wrote:
Whats more frustrating is the 16-color VGA palette used for tray icons in Win 2000 and before!!
 
That is not true.
Under windows 98, you can switch the option "Display icons using all possible colors" in Control panel/desktop properties/Effects.
 
But directories icons are less beautiful with 256 colors, i think. Laugh | :laugh:
 

And with that, icons can be 24 bits !
I programmed a viewer for .jpg files that display a 32x32 24 bits icon, and counted the colors entries for an icon, after printing screen, and found 589 different colors !
 
I think that for Windows 95, you need microsoft plus to do that.
 
It is fun to see that a 16x16 24 bits icon is smaller in memory than a 16x16 256 colors (with optimized palette).
GeneralRe: why.memberMuhammad Asif Khan16 Mar '04 - 18:55 
You are right in saying that each pixel can have one of 24/32 bit range colors, but if u get into MSDN, u will see that VC donot allows normal icon loading that contains more than 256 color support.
GeneralRe: why.membernoshbar18 Mar '04 - 3:49 
Hey, I just thought I'd point out that what he meant was that 16x16 pixels = 256 pixels, so you could have a palette of 256 unique colours and still be covered having a new colour per pixel... thing...
 


Crayons don't kill people, death does.

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.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