Tutorial - Write Icon in the Taskbar Window | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Nov 2, 2001
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.