dcsimg

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


This article was originally published on November 2nd, 2001

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date