Tutorial - Write Icon in the Taskbar Window

Environment: VC6 SP4, W9x, NT 4

This simple program code show how to write or remove an icon from the window taskbar and add a tool tip text. It also show how you can define a user message for trap events generate with mouse action (click on the icon). The sample application is an MFC exe dialog based program.

The routine for write and remove icon:

void CWriteTaskBarDlg::OnWrite() 
{
  // handle to icon
     HICON hIcon;
  // text for tool tip
     char lpszTip[] = "Mouse is on the Icon !!";

     HINSTANCE hInst =
         AfxFindResourceHandle(MAKEINTRESOURCE(IDI_ICON1),
                               RT_GROUP_ICON);
     hIcon = (HICON)LoadImage( hInst,
                               MAKEINTRESOURCE(IDI_ICON1),
                               IMAGE_ICON,
                               16,
                               16,
                               LR_DEFAULTCOLOR);
    
  // set NOTIFYCONDATA structure

     NOTIFYICONDATA tnid;

     tnid.cbSize = sizeof(NOTIFYICONDATA);
     tnid.hWnd = m_hWnd;
     tnid.uID = IDI_ICON1;
     tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
     tnid.uCallbackMessage = WM_TASKBAR; // my user message
     tnid.hIcon = hIcon;

     if (lpszTip)
        lstrcpyn(tnid.szTip, lpszTip, sizeof(tnid.szTip));
     else
        tnid.szTip[0] = '\0';

  // call to Shell_NotifyIcon with NIM_ADD parameter

     Shell_NotifyIcon(NIM_ADD, &tnid);
 
  // free icon 

     if (hIcon) 
     DestroyIcon(hIcon); 
}

For remove icon:

void CWriteTaskBarDlg::OnRemove() 
{

  // for remove, only provide cbSize, hWnd and uID!

    NOTIFYICONDATA tnid;

    tnid.cbSize = sizeof(NOTIFYICONDATA); 
    tnid.hWnd = m_hWnd; 
    tnid.uID = IDI_ICON1; 

  // call to Shell_NotifyIcon with NIM_DEL parameter

    Shell_NotifyIcon(NIM_DELETE, &tnid); 
}

For Mouse Message:

LRESULT  CWriteTaskBarDlg::OnTaskbar(WPARAM wParam, 
                                     LPARAM lParam)
{
    UINT uMouseMsg = (UINT) lParam;

    switch (uMouseMsg)

  {
     case WM_LBUTTONDOWN: 
               AfxMessageBox("Mouse click on the Icon !");
               break;

     default:  break;
    
  }

  return 0;
}

Downloads

Download source - 34 Kb

IT Offers

Comments

  • Actually...

    Posted by grahamr (work) on 10/03/2006 04:59am

    ...it's not taskbar or traybar, but SystemTray as most of the literature I've read calls it.

    Reply
  • Generating a mouse click

    Posted by Legacy on 11/02/2003 12:00am

    Originally posted by: Seth D.

    Hi,

    I'm trying to find a simple code(visual C++)
    that will generate a mouse click in a specific area(like clicking on the Start menu).

    Thanx,

    Seth.

    Reply
  • Nope, it's taskbar and not traybar.

    Posted by Legacy on 05/26/2002 12:00am

    Originally posted by: Zack

    Search for Shell_NotifyIcon in the MSDN docs and check out the following in the remarks sections.

    The taskbar notification area is sometimes erroneously called the "tray."

    I couldn't find a "Tray bar" options in Windows. However, when I hide the taskbar in windows, the so called "tray bar" disappears along with it. I guess that would mean that this "tray bar" is apart of the taskbar?

    However, it seems that "Traybar" is a popular term for the task bar notification area. The term was probably coined by a marketing department at some company that is now out of business.

    Calling someone an idiot for using the proper terminology is well... Do your research first.

    Reply
  • Dammit it's not the taskbar it's the tray bar!!

    Posted by Legacy on 02/08/2002 12:00am

    Originally posted by: DaaZ

    No wonder I can't find anything about hiding a window from the taskbar! There are countless morons who write stuff about adding/removing an icon from the taskbar but who, like our idiot here,are actually talking about the FUCKING TRAY BAR!!!

    Dammit it's not that complicated!

    Reply
  • Good One, But..

    Posted by Legacy on 11/06/2001 12:00am

    Originally posted by: Praveen Dandu

    It's a good one..but the exactly same example has been provided in the MSDN itself..

    Reply
  • reply

    Posted by Legacy on 11/05/2001 12:00am

    Originally posted by: Erwan

    Resourceful..

    Reply
  • Someone had wrote a class for this: CTrayIcon

    Posted by Legacy on 11/03/2001 12:00am

    Originally posted by: DaFu Chen

    Someone had wrote a class for this situation, the class is named CTrayIcon.

    Reply
  • Better tutorial @ http://www.maxcode.com/

    Posted by Legacy on 11/02/2001 12:00am

    Originally posted by: clemens

    check the BETTER tutorial @MAXcode

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Whitepapers and More

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds