Solved

Taskbar

Posted on 1998-06-01
7
674 Views
Last Modified: 2012-06-21
How do I make a program so that its icon is displayed in the taskbar  (Like volume control) in MS Visual
C++ 5.0?
0
Comment
Question by:onka34
  • 3
  • 2
  • 2
7 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 100 total points
Comment Utility
Use the Shell_NotifyIcon() API.
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
An example:

#define STRICT
#include <windows.h>
#include <shellapi.h>

#define MYWM_NOTIFYICON     (WM_USER + 1)   // Tray notification message


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
{
    NOTIFYICONDATA  nid     = { sizeof(NOTIFYICONDATA) };
    WNDCLASS        wc      = { 0 };
    HWND            hwnd;
    MSG             msg;

    wc.style         = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc   = (WNDPROC)WindowProc;
    wc.hInstance     = hInstance;
    wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MENU);
    wc.lpszClassName = "Class Name";
    RegisterClass(&wc);

    hwnd = CreateWindow(...);

    // Link with tray icon
    nid.hWnd = hwnd;
    nid.uID = IDI_TRAYICON;
    nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
    nid.uCallbackMessage = MYWM_NOTIFYICON;
    nid.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TRAYICON));
    lstrcpy(nid.szTip, AGHOOK_APP_NAME);

    Shell_NotifyIcon(NIM_ADD, &nid);

    // Acquire and dispatch messages until a WM_QUIT message is received.
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);  // Translates virtual key codes
        DispatchMessage(&msg);   // Dispatches message to window
    }

    Shell_NotifyIcon(NIM_DELETE, &nid);
    return 1;
}


// Main window message processing procedure
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
        // Tray icon notification message
        case MYWM_NOTIFYICON:
            // Button event is single click?
            if (lParam == WM_LBUTTONDOWN || lParam == WM_RBUTTONDOWN)
            {
                POINT   pos;

                GetCursorPos(&pos);
                SetForegroundWindow(hwnd);
                TrackPopupMenuEx(GetSubMenu(GetMenu(hwnd), 0),
                                 TPM_HORIZONTAL, pos.x, pos.y, hwnd, NULL);
                PostMessage(hwnd, WM_NULL, 0, 0);
            }

            break;

        // Menu commands
        case WM_COMMAND:
            switch (LOWORD(wParam))
            {
                case IDM_EXIT:
                    PostQuitMessage(0);
                    break;
            }
            break;

        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }

    return 0;
}

0
 

Author Comment

by:onka34
Comment Utility
Thankyou alexo.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 11

Expert Comment

by:alexo
Comment Utility
yourwelcome onka34 :-)
0
 

Expert Comment

by:zicokiller
Comment Utility
Hi..., If your are MFC programmer this is CTrayIcon class
for implement taskbar icons.

/////////////////////////////////////////////////////////////////
// TrayIcon.h : header file
//

#ifndef _INCLUDED_TRAYICON_H_
#define _INCLUDED_TRAYICON_H_

/////////////////////////////////////////////////////////////////
// CTrayIcon window

class CTrayIcon : public CObject
{
// Construction/destruction
public:
      CTrayIcon();
      CTrayIcon(CWnd* pWnd, UINT uCallbackMessage, LPCTSTR szTip, HICON icon, UINT uID);
      virtual ~CTrayIcon();

// Operations
public:
      BOOL Enabled() { return m_bEnabled; }
      BOOL Visible() { return !m_bHidden; }

      //Create the tray icon
      Create(CWnd* pWnd, UINT uCallbackMessage, LPCTSTR szTip, HICON icon, UINT uID);

      //Change or retrieve the Tooltip text
      BOOL    SetTooltipText(LPCTSTR pszTooltipText);
      BOOL    SetTooltipText(UINT nID);
      CString GetTooltipText() const;

      //Change or retrieve the icon displayed
      BOOL  SetIcon(HICON hIcon);
      BOOL  SetIcon(LPCTSTR lpIconName);
      BOOL  SetIcon(UINT nIDResource);
      BOOL  SetStandardIcon(LPCTSTR lpIconName);
      BOOL  SetStandardIcon(UINT nIDResource);
      HICON GetIcon() const;
      void  HideIcon();
      void  ShowIcon();
      void  RemoveIcon();
      void  MoveToRight();

//Change or retrieve the window to send notification messages to
      BOOL  SetNotificationWnd(CWnd* pNotifyWnd);
      CWnd* GetNotificationWnd() const;

      //Default handler for tray notification message
      virtual LRESULT OnTrayNotification(WPARAM uID, LPARAM lEvent);

// Overrides
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CTrayIcon)
      //}}AFX_VIRTUAL

// Implementation
protected:
      BOOL      m_bEnabled;      // does O/S support tray icon?
      BOOL      m_bHidden;      // Has the icon been hidden?
      NOTIFYICONDATA      m_tnd;

      DECLARE_DYNAMIC(CTrayIcon)
};


#endif

/////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////
// TrayIcon.cpp : implementation file
//
// This is a conglomeration of ideas from the MSJ "Webster"
// application,sniffing round the online docs, and from other
// implementations such as PJ Naughter's "CTrayIconifyIcon"
// (http://indigo.ie/~pjn/ntray.html) especially the
// "CTrayIcon::OnTrayNotification" member function.
//
// This class is a light wrapper around the windows system
// tray stuff. It adds an icon to the system tray with
// the specified ToolTip text and callback notification value,
// which is sent back to the Parent window.
//
// The tray icon can be instantiated using either the
// constructor or by declaring the object and
// creating (and displaying) it later on in the
// program. eg.
//
//      CTrayIcon m_TrayIcon;      // Member variable of some class
//            
//            ...
//            // in some member function maybe...
//            m_TrayIcon.Create(pParentWnd, WM_MY_NOTIFY, //                       "Click here", hIcon, nTrayIconID);
//
//  Written by Chris Maunder (Chris.Maunder@cbr.clw.csiro.au)
//
//
/////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "TrayIconEx.h"

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

IMPLEMENT_DYNAMIC(CTrayIcon, CObject)

/////////////////////////////////////////////////////////////////
// CTrayIcon construction/creation/destruction

CTrayIcon::CTrayIcon()
{
      memset(&m_tnd, 0, sizeof(m_tnd));
      m_bEnabled = FALSE;
      m_bHidden  = FALSE;
}

CTrayIcon::CTrayIcon(CWnd* pWnd, UINT uCallbackMessage, LPCTSTR szToolTip, HICON icon, UINT uID)
{
      Create(pWnd, uCallbackMessage, szToolTip, icon, uID);
      m_bHidden = FALSE;
}

BOOL CTrayIcon::Create(CWnd* pWnd, UINT uCallbackMessage, LPCTSTR szToolTip, HICON icon, UINT uID)
{
      // this is only for Windows 95 (or higher)
      VERIFY(m_bEnabled = ( GetVersion() & 0xff ) >= 4);
      if (!m_bEnabled) return FALSE;

      //Make sure Notification window is valid
      VERIFY(m_bEnabled = (pWnd && ::IsWindow(pWnd->GetSafeHwnd())));
      if (!m_bEnabled) return FALSE;
      
      //Make sure we avoid conflict with other messages
      ASSERT(uCallbackMessage >= WM_USER);

      //Tray only supports tooltip text up to 64 characters
      ASSERT(_tcslen(szToolTip) <= 64);

      // load up the NOTIFYICONDATA structure
      m_tnd.cbSize = sizeof(NOTIFYICONDATA);
      m_tnd.hWnd       = pWnd->GetSafeHwnd();
      m_tnd.uID       = uID;
      m_tnd.hIcon  = icon;
      m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
      m_tnd.uCallbackMessage = uCallbackMessage;
      strcpy (m_tnd.szTip, szToolTip);

      // Set the tray icon
      VERIFY(m_bEnabled = Shell_NotifyIcon(NIM_ADD, &m_tnd));
      return m_bEnabled;
}

CTrayIcon::~CTrayIcon()
{
      RemoveIcon();
}


/////////////////////////////////////////////////////////////////
// CTrayIcon icon manipulation

void CTrayIcon::MoveToRight()
{
      HideIcon();
      ShowIcon();
}

void CTrayIcon::RemoveIcon()
{
      if (!m_bEnabled) return;

      m_tnd.uFlags = 0;
    Shell_NotifyIcon(NIM_DELETE, &m_tnd);
    m_bEnabled = FALSE;
}

void CTrayIcon::HideIcon()
{
      if (m_bEnabled && !m_bHidden) {
            m_tnd.uFlags = NIF_ICON;
            Shell_NotifyIcon (NIM_DELETE, &m_tnd);
            m_bHidden = TRUE;
      }
}

void CTrayIcon::ShowIcon()
{
      if (m_bEnabled && m_bHidden) {
            m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
            Shell_NotifyIcon(NIM_ADD, &m_tnd);
            m_bHidden = FALSE;
      }
}

BOOL CTrayIcon::SetIcon(HICON hIcon)
{
      if (!m_bEnabled) return FALSE;

      m_tnd.uFlags = NIF_ICON;
      m_tnd.hIcon = hIcon;

      return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
}

BOOL CTrayIcon::SetIcon(LPCTSTR lpszIconName)
{
      HICON hIcon = AfxGetApp()->LoadIcon(lpszIconName);

      return SetIcon(hIcon);
}

BOOL CTrayIcon::SetIcon(UINT nIDResource)
{
      HICON hIcon = AfxGetApp()->LoadIcon(nIDResource);

      return SetIcon(hIcon);
}

BOOL CTrayIcon::SetStandardIcon(LPCTSTR lpIconName)
{
      HICON hIcon = LoadIcon(NULL, lpIconName);

      return SetIcon(hIcon);
}

BOOL CTrayIcon::SetStandardIcon(UINT nIDResource)
{
      HICON hIcon = LoadIcon(NULL, MAKEINTRESOURCE(nIDResource));

      return SetIcon(hIcon);
}
 
HICON CTrayIcon::GetIcon() const
{
      HICON hIcon = NULL;
      if (m_bEnabled)
            hIcon = m_tnd.hIcon;

      return hIcon;
}

/////////////////////////////////////////////////////////////////
// CTrayIcon tooltip text manipulation

BOOL CTrayIcon::SetTooltipText(LPCTSTR pszTip)
{
      if (!m_bEnabled) return FALSE;

      m_tnd.uFlags = NIF_TIP;
      _tcscpy(m_tnd.szTip, pszTip);

      return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
}

BOOL CTrayIcon::SetTooltipText(UINT nID)
{
      CString strText;
      VERIFY(strText.LoadString(nID));

      return SetTooltipText(strText);
}

CString CTrayIcon::GetTooltipText() const
{
      CString strText;
      if (m_bEnabled)
            strText = m_tnd.szTip;

      return strText;
}

/////////////////////////////////////////////////////////////////
// CTrayIcon notification window stuff

BOOL CTrayIcon::SetNotificationWnd(CWnd* pWnd)
{
      if (!m_bEnabled) return FALSE;

      //Make sure Notification window is valid
      ASSERT(pWnd && ::IsWindow(pWnd->GetSafeHwnd()));

      m_tnd.hWnd = pWnd->GetSafeHwnd();
      m_tnd.uFlags = 0;

      return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
}

CWnd* CTrayIcon::GetNotificationWnd() const
{
      return CWnd::FromHandle(m_tnd.hWnd);
}

/////////////////////////////////////////////////////////////////
// CTrayIcon implentation of OnTrayNotification

LRESULT CTrayIcon::OnTrayNotification(UINT wParam, LONG lParam)
{
      //Return quickly if its not for this tray icon
      if (wParam != m_tnd.uID)
            return 0L;

      CMenu menu, *pSubMenu;

      // Clicking with right button brings up a context menu
      if (LOWORD(lParam) == WM_RBUTTONUP)
      {      
            if (!menu.LoadMenu(m_tnd.uID)) return 0;
            if (!(pSubMenu = menu.GetSubMenu(0))) return 0;

            // Make first menu item the default (bold font)
            ::SetMenuDefaultItem(pSubMenu->m_hMenu, 0, TRUE);

            //Display and track the popup menu
            CPoint pos;
            GetCursorPos(&pos);

            ::SetForegroundWindow(m_tnd.hWnd);  
            ::TrackPopupMenu(pSubMenu->m_hMenu, 0, pos.x, pos.y, 0, m_tnd.hWnd, NULL);

            // BUGFIX: See "PRB: Menus for Notification Icons Don't Work Correctly"
            ::PostMessage(m_tnd.hWnd, WM_USER, 0, 0);

            menu.DestroyMenu();
      }
      else if (LOWORD(lParam) == WM_LBUTTONDBLCLK)
      {
            if (!menu.LoadMenu(m_tnd.uID)) return 0;
            if (!(pSubMenu = menu.GetSubMenu(0))) return 0;

            // double click received, the default action is to execute first menu item
            ::SetForegroundWindow(m_tnd.hWnd);
            ::SendMessage(m_tnd.hWnd, WM_COMMAND, pSubMenu->GetMenuItemID(0), 0);

            menu.DestroyMenu();
      }

      return 1;
}
0
 

Author Comment

by:onka34
Comment Utility
thankyou zicokiller
0
 

Expert Comment

by:zicokiller
Comment Utility
No problem
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now