65.9K
CodeProject is changing. Read more.
Home

24 Bit Color Icon in System Tray

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.08/5 (10 votes)

Mar 16, 2004

2 min read

viewsIcon

113351

downloadIcon

1218

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.