Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

24 Bit Color Icon in System Tray

, 15 Mar 2004
An article on displaying icons with more than 256 colors in system tray
systray24.zip
SysTray24
Debug
res
24 bit.bmp
SysTray24.ico
SysTray24.clw
SysTray24.dsp
SysTray24.dsw
// SysTray24Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "SysTray24.h"
#include "SysTray24Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
#define	WM_ICON_NOTIFY			WM_USER+1

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSysTray24Dlg dialog

CSysTray24Dlg::CSysTray24Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSysTray24Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSysTray24Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_hTrayIcon = NULL;
	m_bBitmapIcon = TRUE;

}

CSysTray24Dlg::~CSysTray24Dlg()
{
	// You need to destroy the icon created with 
	// CreateIconIndirect() function call but not with LoadIcon() call

	if(m_bBitmapIcon && m_hTrayIcon != NULL)
		DestroyIcon(m_hTrayIcon);

}


void CSysTray24Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSysTray24Dlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSysTray24Dlg, CDialog)
	//{{AFX_MSG_MAP(CSysTray24Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(ID_MENU_OPEN, OnMenuOpen)
	ON_UPDATE_COMMAND_UI(ID_MENU_OPEN, OnUpdateMenuOpen)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSysTray24Dlg message handlers

BOOL CSysTray24Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	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);
	}

	// if failed to create the icon from bitmap, load default icon
	if(m_hTrayIcon == NULL)
	{
		AfxGetApp()->LoadIcon(IDR_MAINFRAME);
		m_bBitmapIcon = FALSE;
	}

	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;

    m_TrayIcon.SetMenuDefaultItem(0, TRUE);
	m_TrayIcon.HideIcon();

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSysTray24Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else if((nID & 0xFFF0) == SC_MINIMIZE)
	{
		m_TrayIcon.ShowIcon();
		this->ShowWindow(SW_HIDE);
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CSysTray24Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSysTray24Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSysTray24Dlg::OnMenuOpen() 
{
	// TODO: Add your command handler code here
	this->ShowWindow(SW_SHOW);
	m_TrayIcon.HideIcon();
	
}

void CSysTray24Dlg::OnUpdateMenuOpen(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(TRUE);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for 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.

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