System TRAY ICONS

xiaoxiao2021-04-08  394

Download Source Files - 5.88 KB Download Demo Project - 46.7 KB

Introduction

Note: - This Code Will Work Only Under Windows 2000 and Above, When Compiled with VC .NET OR VC 6.0 (with the Latest Platform SDK).

The system tray is a familiar place for every reasonably experienced Windows OS user. Many applications use this area to place small icons there, which give information about their status. They give the user additional flexibility to directly interact with the application. Almost all applications provide a context menu over the system tray icons for interaction with the application. with the advent of Windows 2000 / XP, popup balloons can also be shown over the system tray icons, to notify the user of any relevant event. This article explains the process of .

USING THE CODE

Include the files SYSTRAYICON.H and SYSTRAYICON.CPP in your project. Create an object of class CSysTrayIcon. In case you wish to process mouse messages over the icons derive a class from CSysTrayIcon and override any or all of the mouse messages mentioned in section " Overridable functions "Create the object of class CSysTrayIcon or a class derived from CSysTrayIcon (preferably in the MainFrame class). Create and manipulate icons in the system tray using the functions given in section" Functions for creating and manipulating icons in the system tray ". Mouse messages are trapped and overridable functions are provided for custom processing. See section "Overridable functions" for the list of overridable functions which are triggered by mouse messages.Functions for creating and manipulating icons in the system tray

Bool Createicon (Hicon Hicon, Uint Niconid, PCSTR SZTIP) Input Parameters

HICON hIcon -> Handle to an icon to be shown in the system tray UINT nIconID -.> A user defined icon ID It can be any unsigned integer PCSTR szTip -..> A tooltip that is shown when the user moves the mouse over the Icon. Return Value True if a new TRAY ICON with THE NICONID COULD BE CREATED, ELSE FALSE BOOL Showicon (Uint Niconid) Input Parameters

Uint niconid -> The icon with this id to be shown. Return Value True If The icon with this id exists and could be shown, else false bool hideicon (uint niconid) - HIDES The icon. It does not delete the icon from memory. Input Parameters

UINT nIconID -.> The icon with this ID to be hidden It is not deleted from memory Return value TRUE if the icon with this ID exists and could be hidden, else FALSE BOOL DeleteIcon (UINT nIconID) -. Hides and deletes the icon from the memory Input parametersUINT nIconID -..> The icon with this ID to be hidden and deleted from memory Return value TRUE if the icon with the ID exists and could be removed and deleted from the system tray and memory, else FALSE BOOL ChangeIconTip (UINT NICONID, CSTRING STRTIP - this change the Tool Tip Associated with the system tray icon. Input Parameters

UINT nIconID -> The icon with this ID whose tool tip has to be changed CString strTip -.> The new tool tip to be associated with this icon Return value TRUE if the icon with this ID exists and its tool tip cold be changed,. Else False Bool Showballoon (Uint Niconid, PTStr Szballoontitle, PTStr Szballoonmsg, DWORD DWICON = Niif_none, Uint Ntimeout = 10) - Shows A Popup Balloon over The icon with the ID, Niconid. Input Parameters

UINT nIconID -> The icon ID over which the balloon will be shown PTSTR szBalloonTitle -.> A string for the heading of the balloon message PTSTR szBalloonMsg -.> A string for the balloon message DWORD dwIcon -> An additional symbol in the left corner .. of the balloon that will emphasize the meaning of the balloon There are four possibilities, NIIF_NONE, NIIF_ERROR, NIIF_INFO and NIIF_WARNING The default is NIIF_NONE These are standard constants defined in Windows Platform SDK in shellapi.h UINT nTimeOut -.> Time in seconds for the balloon to be visible. In Windows 2000 / XP, the minimum is 10 seconds. Even if you specify a time less than 10 seconds, it will be visible for a minimum of 10 seconds. This will probably change with the future versions of Windows. Return Value True If The Balloon with Given Id Exists and The Requested Balloon Could Be Shown, Else False.Overridable functions

There Are Ten Overridable Virtual Functions, All of Which DEAL with MOUSE Messages.

virtual void OnLButtonDown (UINT nIconID, CPoint ptMouse) Gets called when the left mouse button has been pressed virtual void OnRButtonDown (UINT nIconID, CPoint ptMouse) Gets called when the right mouse button has been pressed virtual void OnMButtonDown (UINT nIconID, CPoint ptMouse) gets called when the middle mouse button has been pressed virtual void OnLButtonUp (UINT nIconID, CPoint ptMouse) gets called when the left mouse button is released virtual void OnRButtonUp (UINT nIconID, CPoint ptMouse) gets called when the right mouse button is released virtual void OnMButtonUp (UINT nIconID, CPoint ptMouse) Gets called when the middle mouse button is released virtual void OnLButtonDblClk (UINT nIconID, CPoint ptMouse) Gets called when the left mouse button is double clicked virtual void OnRButtonDblClk (UINT nIconID, CPoint ptMouse) Gets called when The Right Mouse Button Is Double Clicked Virtual Void OnMbuttondblclk (uint niconid, cpoint ptmouse) Gets Called When The Middle Mouse Button IS Double Clicked Virtual Void OnMousemove (Uint Niconid, cpoint ptmouse) Gets Called When the Mouse Moves over the iconinput parameters for All Overridables:

UINT NICONID -> The icon id over which the mouse message has been generated cpoint ptmouse -> the mouse position in screen co-order

Helper Function for Handling a Context Menu

BOOL OnContextMenu (CMenu * pContextMenu, CPoint ptMouse, CWnd * pWndMessageHandler) This function should be called by the programmer, as it will help him to do the requisite processing to show a context menu. This sets the window mentioned in the third parameter in the foreground, so that commands can be routed to it for processing This function should generally be called in response to a right button down message, typically in the overridden method OnRButtonDown of the class derived from CSysTrayIcon Input parameters: CMenu * pContextMenu -> Pointer.. to the menu to be shown, it is mostly on a right button press CPoint ptMouse -.> Mouse position in screen co-ordinates CWnd * pWndMessageHandler -.> A pointer to the window that will process the command handlers of the menu pContextMenu This. Function Set So That IT ITLERS. THIS Function WAS WRITTEN SPECIFICLY for BRINGINGINGED oreground. In case you do not bring the window that will receive the menu commands in the foreground, the commands will not be routed properly. This window is generally the MainFrame window in a typical Windows application.

The Implementation of The Class Csystrayicon

The CSysTrayIcon class is a wrapper around the Windows API, Shell_NotifyIcon and the Windows Platform SDK structure NOTIFYICONDATA, which is declared in the header file shellapi.h. Details about the above mentioned API and structure are well documented in the MSDN documentation.

The class CSysTrayIcon contains a linked list of NOTIFYICONDATA structures (CSystrayIcon :: NOTIFYICONDATAList), which maintains information about each icon in the system tray related to that application. Every application can have more than one icon in the system tray. There is no restriction on an application to have only one system tray icon. It is only restricted by the maximum value of an unsigned integer! Since this class CSysTrayIcon can not directly receive mouse messages, there has to be a way to route the messages to this class. This is done by creating a hidden window as a private member variable m_wndTrayMsgReceiver (of type CSysTrayWnd) in the class CSysTrayIcon, which is registered to receive the WM_SYSTRAYMSG. When a message WM_SYSTRAYMSG is received by the window object CSysTrayIcon :: m_wndTrayMsgReceiver, it is mapped to CsystrayWnd :: OnSystraymsg (WPARAM WPARAM, LPARAM LPARAM). The lparam gives the Type of Mouse Message and the wparam gives the icon ID on which the mouse message age was generated. If you look at the implementation of CSysTrayIcon :: CreateIcon (), the NOTIFYICONDATA structure members hWnd and uCallBackMessage are initialized as shown below. This is where the operating system comes to know which window is to be sent the WM_SYSTRAYMSG.

PnidiconInfo-> hWnd = m_wndtraymsgreceiver.getsafehwnd ();

PnidiconInfo-> UcallbackMessage = WM_SYSTRAYMSG;

An object of CSysTrayWnd window is created in the constructor of CSysTrayIcon itself. The CSystTrayWnd also has a pointer to the CSysTrayIcon class which helps in calling the virtual overridable functions as mentioned in "Overridable functions".

There are few other functions which are used internally in the class to get the icons from the linked list, if given the icon ID as the parameter These are summarized below.GetNotifyIconDataFromID () -> Retrieves the NOTIFYCOINDATA structure from the linked list CSysTrayIcon.: :. NOTIFYICONDATAList for the given icon ID CheckIfIconIDExists () -> This is called to check if the icon with the given ID already exists, when creating a new system tray icon DeleteNotifyIconDataFromID () -> This is called from the DeleteIcon () function. And the destructor of the csysttrayicon class for deleting the icon and profment removs the icon by removing and deletring it from the linked list csystrayicon :: NotifyicondatAlist.

Demo Project

A class is derived from CSysTrayIcon with the name CMyAppTrayIcon. This class overrides only two virtual functions, OnLButtonDownDBlClk () to show a message box and OnRButtonDown to show a context menu. These two functions get triggered for the Windows messages WM_LBUTTONDBLCLK and WM_RBUTTONDOWN respectively, by The Routing Through a csystraywnd Object.

An object of CMyAppTrayIcon is kept as a member variable of CMainFrame class. Four icons are created in the CMainFrame :: OnCreate (). The CMainFrame class handles all the command handlers for the context menu for the system tray icons.

For the context menu processing, the overridden OnRButtonDown method passes the main window's pointer to the CSysTrayIcon :: OnContextMenu as the third parameter. The main window has the command handlers for the required processing.

Void CMYApptrayicon :: OnRbuttondown (uint niconid, cpoint ptmouse)

{

CMenu T_Menu;

T_Menu.LoadMenu (idR_mainframe);

OnContextMenu (T_Menu.getsubmenu (0), PTMOUSE, :: AFXGETMAINWND ());

Use the icon menu to manipulate the system TRAY ICONS AS Shown in The Top Figure.

Prabeek Kaul

转载请注明原文地址:https://www.9cbs.com/read-132772.html

New Post(0)