你是说右下角的那种,很简单的。给你一个小类,轻松搞定=======================================
/*
Module : NTRAY.H
Purpose: Interface for a MFC class to encapsulate Shell_NotifyIcon
Created: PJN / NOTSHELL/1 / 14-05-1997
History: NoneCopyright (c) 1997 by PJ Naughter.
All rights reserved.*/
////////////////////////////////// Macros ///////////////////////////#ifndef _NTRAY_H__
#define _NTRAY_H__/////////////////////////// Classes ///////////////////////////////////forward declaration
class CTrayNotifyIcon;
//internal class used to handle IE4 taskbar creation notification message
class CTrayRessurectionWnd : public CFrameWnd
{
public:
//Constructors / Destructors
CTrayRessurectionWnd(CTrayNotifyIcon* pTrayIcon);protected:
CTrayNotifyIcon* m_pTrayIcon; //{{AFX_MSG(CTrayRessurectionWnd)
//}}AFX_MSG
afx_msg LRESULT OnTaskbarCreated(WPARAM wParam, LPARAM lParam);
DECLARE_MESSAGE_MAP() DECLARE_DYNAMIC(CTrayRessurectionWnd)
};//internal class used to implement animated tray icons
class CTrayTimerWnd : public CFrameWnd
{
public:
//Constructors / Destructors
CTrayTimerWnd(CTrayNotifyIcon* pTrayIcon, HICON* phIcons, int nNumIcons, DWORD dwDelay);
~CTrayTimerWnd();//retreive the current icon
HICON GetCurrentIcon() const { return m_phIcons[m_nCurrentIconIndex]; };protected:
CTrayNotifyIcon* m_pTrayIcon;
HICON* m_phIcons;
int m_nNumIcons;
DWORD m_dwDelay;
UINT m_nTimerID;
int m_nCurrentIconIndex; //{{AFX_MSG(CTrayTimerWnd)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnDestroy();
afx_msg void OnTimer(UINT nIDEvent);
//}}AFX_MSG
afx_msg LRESULT OnTaskbarCreated(WPARAM wParam, LPARAM lParam);
DECLARE_MESSAGE_MAP() DECLARE_DYNAMIC(CTrayTimerWnd)
};
//the actual tray notification class wrapper
class CTrayNotifyIcon : public CObject
{
public:
//Constructors / Destructors
CTrayNotifyIcon();
~CTrayNotifyIcon(); //Create the tray icon
BOOL Create(CWnd* pNotifyWnd, UINT uID, LPCTSTR pszTooltipText, HICON hIcon, UINT nNotifyMessage);
BOOL Create(CWnd* pNotifyWnd, UINT uID, LPCTSTR pszTooltipText, HICON* phIcons, int nNumIcons, DWORD dwDelay, UINT nNotifyMessage); //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 SetIcon(HICON* phIcons, int nNumIcons, DWORD dwDelay);
BOOL SetStandardIcon(LPCTSTR lpIconName);
BOOL SetStandardIcon(UINT nIDResource);
HICON GetIcon() const;
BOOL UsingAnimatedIcon() const { return m_bAnimated; }; //Change or retrieve the window to send notification messages to
BOOL SetNotificationWnd(CWnd* pNotifyWnd);
CWnd* GetNotificationWnd() const; //Modification of the tray icons
void HideIcon();
void ShowIcon();
void RemoveIcon();
void MoveToExtremeRight(); //Default handler for tray notification message
virtual LRESULT OnTrayNotification(WPARAM uID, LPARAM lEvent); //Status information
BOOL IsShowing() const { return !IsHidden(); };
BOOL IsHidden() const { return m_bHidden; };protected:
BOOL CreateTimerWindow(HICON* phIcons, int nNumIcons, DWORD dwDelay);
void DestroyTimerWindow();
BOOL CreateRessurectionWindow();
void DestroyResurrectionWindow(); NOTIFYICONDATA m_NotifyIconData;
BOOL m_bCreated;
BOOL m_bHidden;
CWnd* m_pNotificationWnd;
CTrayTimerWnd* m_pTimerWnd;
CTrayRessurectionWnd* m_pResurrectionWnd;
BOOL m_bAnimated; DECLARE_DYNAMIC(CTrayNotifyIcon) friend class CTrayTimerWnd;
};
#endif //_NTRAY_H__
====================================
/*
Module : NTRAY.CPP
Purpose: implementation for a MFC class to encapsulate Shell_NotifyIcon
Created: PJN / NOTSHELL/1 / 14-05-1997
History: PJN / 25-11-1997 : Addition of the following
1. HideIcon(), ShowIcon() & MoveToExtremeRight
2. Support for animated tray icons
PJN / 23-06-1998 : Class now supports the new Taskbar Creation Notification
message which comes with IE 4. This allows the tray icon
to be recreated whenever the explorer restarts (Crashes!!)
PJN / 22-07-1998 : 1. Code now compiles cleanly at warning level 4
2. Code is now UNICODE enabled + build configurations are
provided
3. The documentation for the class has been updatedCopyright (c) 1997 by PJ Naughter.
All rights reserved.*////////////////////////////////// Includes //////////////////////////////////
#include "stdafx.h"
#include "resource.h"
#include "ntray.h"///////////////////////////////// Macros /////////////////////////////////////#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///////////////////////////////// Implementation //////////////////////////////const UINT wm_TaskbarCreated = RegisterWindowMessage(_T("TaskbarCreated"));IMPLEMENT_DYNAMIC(CTrayRessurectionWnd, CFrameWnd)BEGIN_MESSAGE_MAP(CTrayRessurectionWnd, CFrameWnd)
//{{AFX_MSG_MAP(CTrayRessurectionWnd)
//}}AFX_MSG_MAP
ON_REGISTERED_MESSAGE(wm_TaskbarCreated, OnTaskbarCreated)
END_MESSAGE_MAP()LRESULT CTrayRessurectionWnd::OnTaskbarCreated(WPARAM wParam, LPARAM lParam)
{
ASSERT(m_pTrayIcon); //Refresh the tray icon if necessary
if (m_pTrayIcon->IsShowing())
{
m_pTrayIcon->HideIcon();
m_pTrayIcon->ShowIcon();
} return 0L;
}CTrayRessurectionWnd::CTrayRessurectionWnd(CTrayNotifyIcon* pTrayIcon)
{
//must have at valid tray notify instance
ASSERT(pTrayIcon); //Store the values away
m_pTrayIcon = pTrayIcon;
}IMPLEMENT_DYNAMIC(CTrayTimerWnd, CFrameWnd)BEGIN_MESSAGE_MAP(CTrayTimerWnd, CFrameWnd)
//{{AFX_MSG_MAP(CTrayTimerWnd)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()CTrayTimerWnd::CTrayTimerWnd(CTrayNotifyIcon* pTrayIcon, HICON* phIcons, int nNumIcons, DWORD dwDelay)
{
m_nCurrentIconIndex = 0; //must have a valid tray notify instance
ASSERT(pTrayIcon); //must have at least 1 icon
ASSERT(nNumIcons); //array of icon handles must be valid
ASSERT(phIcons); //must be non zero timer interval
ASSERT(dwDelay); //Store the values away
m_pTrayIcon = pTrayIcon; m_phIcons = new HICON[nNumIcons];
CopyMemory(m_phIcons, phIcons, nNumIcons * sizeof(HICON));
m_nNumIcons = nNumIcons;
m_dwDelay = dwDelay;
}CTrayTimerWnd::~CTrayTimerWnd()
{
if (m_phIcons)
{
delete [] m_phIcons;
m_phIcons = NULL;
}
}int CTrayTimerWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
//create the animation timer
m_nTimerID = SetTimer(1, m_dwDelay, NULL);
return 0;
}void CTrayTimerWnd::OnDestroy()
{
//kill the animation timer
KillTimer(m_nTimerID); CFrameWnd::OnDestroy();
}void CTrayTimerWnd::OnTimer(UINT nIDEvent)
{
//increment the icon index
++m_nCurrentIconIndex;
m_nCurrentIconIndex = m_nCurrentIconIndex % m_nNumIcons; //update the tray icon
m_pTrayIcon->m_NotifyIconData.uFlags = NIF_ICON;
m_pTrayIcon->m_NotifyIconData.hIcon = m_phIcons[m_nCurrentIconIndex];
Shell_NotifyIcon(NIM_MODIFY, &m_pTrayIcon->m_NotifyIconData);
}
IMPLEMENT_DYNAMIC(CTrayNotifyIcon, CObject)CTrayNotifyIcon::CTrayNotifyIcon()
{
memset(&m_NotifyIconData, 0, sizeof(m_NotifyIconData));
m_bCreated = FALSE;
m_bHidden = FALSE;
m_pNotificationWnd = NULL;
m_pResurrectionWnd = NULL;
m_pTimerWnd = NULL;
m_bAnimated = FALSE;
}CTrayNotifyIcon::~CTrayNotifyIcon()
{
DestroyTimerWindow();
DestroyResurrectionWindow();
RemoveIcon();
}void CTrayNotifyIcon::HideIcon()
{
ASSERT(m_bCreated);
if (!m_bHidden)
{
m_NotifyIconData.uFlags = NIF_ICON;
Shell_NotifyIcon(NIM_DELETE, &m_NotifyIconData);
m_bHidden = TRUE;
}
}void CTrayNotifyIcon::ShowIcon()
{
ASSERT(m_bCreated);
if (m_bHidden)
{
m_NotifyIconData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
Shell_NotifyIcon(NIM_ADD, &m_NotifyIconData);
m_bHidden = FALSE;
}
}void CTrayNotifyIcon::RemoveIcon()
{
if (m_bCreated)
{
m_NotifyIconData.uFlags = 0;
Shell_NotifyIcon(NIM_DELETE, &m_NotifyIconData);
m_bCreated = FALSE;
}
}void CTrayNotifyIcon::MoveToExtremeRight()
{
HideIcon();
ShowIcon();
}BOOL CTrayNotifyIcon::Create(CWnd* pNotifyWnd, UINT uID, LPCTSTR pszTooltipText, HICON hIcon, UINT nNotifyMessage)
{
//Create the ressurection window
if (!CreateRessurectionWindow())
return FALSE; //Make sure Notification window is valid
ASSERT(pNotifyWnd && ::IsWindow(pNotifyWnd->GetSafeHwnd()));
m_pNotificationWnd = pNotifyWnd; //Make sure we avoid conflict with other messages
ASSERT(nNotifyMessage >= WM_USER); //Tray only supports tooltip text up to 64 characters
ASSERT(_tcslen(pszTooltipText) <= 64); m_NotifyIconData.cbSize = sizeof(m_NotifyIconData);
m_NotifyIconData.hWnd = pNotifyWnd->GetSafeHwnd();
m_NotifyIconData.uID = uID;
m_NotifyIconData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
m_NotifyIconData.uCallbackMessage = nNotifyMessage;
m_NotifyIconData.hIcon = hIcon;
_tcscpy(m_NotifyIconData.szTip, pszTooltipText); BOOL rVal = Shell_NotifyIcon(NIM_ADD, &m_NotifyIconData);
m_bCreated = rVal; return rVal;
}BOOL CTrayNotifyIcon::CreateRessurectionWindow()
{
//Create the resurrection window
ASSERT(m_pResurrectionWnd == NULL);
m_pResurrectionWnd = new CTrayRessurectionWnd(this);
if (!m_pResurrectionWnd)
return FALSE;
if (!m_pResurrectionWnd->Create(NULL, _T("CTrayNotifyIcon Resurrection Notification Window")))
return FALSE; return TRUE;
}void CTrayNotifyIcon::DestroyResurrectionWindow()
{
if (m_pResurrectionWnd)
{
m_pResurrectionWnd->SendMessage(WM_CLOSE);
m_pResurrectionWnd = NULL;
}
}BOOL CTrayNotifyIcon::CreateTimerWindow(HICON* phIcons, int nNumIcons, DWORD dwDelay)
{
//create the hidden window which will contain the timer which will do the animation
ASSERT(m_pTimerWnd == NULL);
m_pTimerWnd = new CTrayTimerWnd(this, phIcons, nNumIcons, dwDelay);
if (!m_pTimerWnd)
return FALSE;
if (!m_pTimerWnd->Create(NULL, _T("CTrayNotifyIcon Animation Notification Window")))
return FALSE; return TRUE;
}void CTrayNotifyIcon::DestroyTimerWindow()
{
if (m_pTimerWnd)
{
m_pTimerWnd->SendMessage(WM_CLOSE);
m_pTimerWnd = NULL;
}
}BOOL CTrayNotifyIcon::Create(CWnd* pNotifyWnd, UINT uID, LPCTSTR pszTooltipText, HICON* phIcons, int nNumIcons, DWORD dwDelay, UINT nNotifyMessage)
{
//must be using at least 2 icons
ASSERT(nNumIcons >= 2); if (!CreateTimerWindow(phIcons, nNumIcons, dwDelay))
return FALSE; //let the normal Create function do its stuff
BOOL bSuccess = Create(pNotifyWnd, uID, pszTooltipText, phIcons[0], nNotifyMessage);
m_bAnimated = TRUE;
return bSuccess;
}BOOL CTrayNotifyIcon::SetTooltipText(LPCTSTR pszTooltipText)
{
if (!m_bCreated)
return FALSE; m_NotifyIconData.uFlags = NIF_TIP;
_tcscpy(m_NotifyIconData.szTip, pszTooltipText); return Shell_NotifyIcon(NIM_MODIFY, &m_NotifyIconData);
}BOOL CTrayNotifyIcon::SetTooltipText(UINT nID)
{
CString sToolTipText;
VERIFY(sToolTipText.LoadString(nID)); return SetTooltipText(sToolTipText);
}BOOL CTrayNotifyIcon::SetIcon(HICON hIcon)
{
if (!m_bCreated)
return FALSE; DestroyTimerWindow();
m_bAnimated = FALSE;
m_NotifyIconData.uFlags = NIF_ICON;
m_NotifyIconData.hIcon = hIcon; return Shell_NotifyIcon(NIM_MODIFY, &m_NotifyIconData);
}BOOL CTrayNotifyIcon::SetIcon(LPCTSTR lpIconName)
{
HICON hIcon = AfxGetApp()->LoadIcon(lpIconName); return SetIcon(hIcon);
}BOOL CTrayNotifyIcon::SetIcon(UINT nIDResource)
{
HICON hIcon = AfxGetApp()->LoadIcon(nIDResource); return SetIcon(hIcon);
}BOOL CTrayNotifyIcon::SetStandardIcon(LPCTSTR lpIconName)
{
HICON hIcon = LoadIcon(NULL, lpIconName); return SetIcon(hIcon);
}BOOL CTrayNotifyIcon::SetStandardIcon(UINT nIDResource)
{
HICON hIcon = LoadIcon(NULL, MAKEINTRESOURCE(nIDResource)); return SetIcon(hIcon);
}BOOL CTrayNotifyIcon::SetIcon(HICON* phIcons, int nNumIcons, DWORD dwDelay)
{
ASSERT(nNumIcons >= 2);
ASSERT(phIcons); if (!SetIcon(phIcons[0]))
return FALSE; DestroyTimerWindow();
if (!CreateTimerWindow(phIcons, nNumIcons, dwDelay))
return FALSE;
m_bAnimated = TRUE; return TRUE;
}BOOL CTrayNotifyIcon::SetNotificationWnd(CWnd* pNotifyWnd)
{
if (!m_bCreated)
return FALSE; //Make sure Notification window is valid
ASSERT(pNotifyWnd && ::IsWindow(pNotifyWnd->GetSafeHwnd())); m_pNotificationWnd = pNotifyWnd;
m_NotifyIconData.hWnd = pNotifyWnd->GetSafeHwnd();
m_NotifyIconData.uFlags = 0; return Shell_NotifyIcon(NIM_MODIFY, &m_NotifyIconData);
}CString CTrayNotifyIcon::GetTooltipText() const
{
CString sText;
if (m_bCreated)
sText = m_NotifyIconData.szTip; return sText;
}HICON CTrayNotifyIcon::GetIcon() const
{
HICON hIcon = NULL;
if (m_bCreated)
{
if (m_bAnimated)
hIcon = m_pTimerWnd->GetCurrentIcon();
else
hIcon = m_NotifyIconData.hIcon;
} return hIcon;
}CWnd* CTrayNotifyIcon::GetNotificationWnd() const
{
return m_pNotificationWnd;
}LRESULT CTrayNotifyIcon::OnTrayNotification(WPARAM wID, LPARAM lEvent)
{
//Return quickly if its not for this tray icon
if (wID != m_NotifyIconData.uID)
return 0L; //As a default action use a menu resource with the same id
//as this was created with
CMenu menu;
if (!menu.LoadMenu(m_NotifyIconData.uID))
return 0; CMenu* pSubMenu = menu.GetSubMenu(0);
if (!pSubMenu)
return 0; if (lEvent == WM_RBUTTONUP)
{
//Clicking with right button brings up a context menu // 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_NotifyIconData.hWnd);
::TrackPopupMenu(pSubMenu->m_hMenu, 0, pos.x, pos.y, 0, m_NotifyIconData.hWnd, NULL); }
else if (lEvent == WM_LBUTTONDBLCLK)
{
// double click received, the default action is to execute first menu item
::SendMessage(m_NotifyIconData.hWnd, WM_COMMAND, pSubMenu->GetMenuItemID(0), 0);
} return 1; // handled
}
/*
Module : NTRAY.H
Purpose: Interface for a MFC class to encapsulate Shell_NotifyIcon
Created: PJN / NOTSHELL/1 / 14-05-1997
History: NoneCopyright (c) 1997 by PJ Naughter.
All rights reserved.*/
////////////////////////////////// Macros ///////////////////////////#ifndef _NTRAY_H__
#define _NTRAY_H__/////////////////////////// Classes ///////////////////////////////////forward declaration
class CTrayNotifyIcon;
//internal class used to handle IE4 taskbar creation notification message
class CTrayRessurectionWnd : public CFrameWnd
{
public:
//Constructors / Destructors
CTrayRessurectionWnd(CTrayNotifyIcon* pTrayIcon);protected:
CTrayNotifyIcon* m_pTrayIcon; //{{AFX_MSG(CTrayRessurectionWnd)
//}}AFX_MSG
afx_msg LRESULT OnTaskbarCreated(WPARAM wParam, LPARAM lParam);
DECLARE_MESSAGE_MAP() DECLARE_DYNAMIC(CTrayRessurectionWnd)
};//internal class used to implement animated tray icons
class CTrayTimerWnd : public CFrameWnd
{
public:
//Constructors / Destructors
CTrayTimerWnd(CTrayNotifyIcon* pTrayIcon, HICON* phIcons, int nNumIcons, DWORD dwDelay);
~CTrayTimerWnd();//retreive the current icon
HICON GetCurrentIcon() const { return m_phIcons[m_nCurrentIconIndex]; };protected:
CTrayNotifyIcon* m_pTrayIcon;
HICON* m_phIcons;
int m_nNumIcons;
DWORD m_dwDelay;
UINT m_nTimerID;
int m_nCurrentIconIndex; //{{AFX_MSG(CTrayTimerWnd)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnDestroy();
afx_msg void OnTimer(UINT nIDEvent);
//}}AFX_MSG
afx_msg LRESULT OnTaskbarCreated(WPARAM wParam, LPARAM lParam);
DECLARE_MESSAGE_MAP() DECLARE_DYNAMIC(CTrayTimerWnd)
};
//the actual tray notification class wrapper
class CTrayNotifyIcon : public CObject
{
public:
//Constructors / Destructors
CTrayNotifyIcon();
~CTrayNotifyIcon(); //Create the tray icon
BOOL Create(CWnd* pNotifyWnd, UINT uID, LPCTSTR pszTooltipText, HICON hIcon, UINT nNotifyMessage);
BOOL Create(CWnd* pNotifyWnd, UINT uID, LPCTSTR pszTooltipText, HICON* phIcons, int nNumIcons, DWORD dwDelay, UINT nNotifyMessage); //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 SetIcon(HICON* phIcons, int nNumIcons, DWORD dwDelay);
BOOL SetStandardIcon(LPCTSTR lpIconName);
BOOL SetStandardIcon(UINT nIDResource);
HICON GetIcon() const;
BOOL UsingAnimatedIcon() const { return m_bAnimated; }; //Change or retrieve the window to send notification messages to
BOOL SetNotificationWnd(CWnd* pNotifyWnd);
CWnd* GetNotificationWnd() const; //Modification of the tray icons
void HideIcon();
void ShowIcon();
void RemoveIcon();
void MoveToExtremeRight(); //Default handler for tray notification message
virtual LRESULT OnTrayNotification(WPARAM uID, LPARAM lEvent); //Status information
BOOL IsShowing() const { return !IsHidden(); };
BOOL IsHidden() const { return m_bHidden; };protected:
BOOL CreateTimerWindow(HICON* phIcons, int nNumIcons, DWORD dwDelay);
void DestroyTimerWindow();
BOOL CreateRessurectionWindow();
void DestroyResurrectionWindow(); NOTIFYICONDATA m_NotifyIconData;
BOOL m_bCreated;
BOOL m_bHidden;
CWnd* m_pNotificationWnd;
CTrayTimerWnd* m_pTimerWnd;
CTrayRessurectionWnd* m_pResurrectionWnd;
BOOL m_bAnimated; DECLARE_DYNAMIC(CTrayNotifyIcon) friend class CTrayTimerWnd;
};
#endif //_NTRAY_H__
====================================
/*
Module : NTRAY.CPP
Purpose: implementation for a MFC class to encapsulate Shell_NotifyIcon
Created: PJN / NOTSHELL/1 / 14-05-1997
History: PJN / 25-11-1997 : Addition of the following
1. HideIcon(), ShowIcon() & MoveToExtremeRight
2. Support for animated tray icons
PJN / 23-06-1998 : Class now supports the new Taskbar Creation Notification
message which comes with IE 4. This allows the tray icon
to be recreated whenever the explorer restarts (Crashes!!)
PJN / 22-07-1998 : 1. Code now compiles cleanly at warning level 4
2. Code is now UNICODE enabled + build configurations are
provided
3. The documentation for the class has been updatedCopyright (c) 1997 by PJ Naughter.
All rights reserved.*////////////////////////////////// Includes //////////////////////////////////
#include "stdafx.h"
#include "resource.h"
#include "ntray.h"///////////////////////////////// Macros /////////////////////////////////////#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///////////////////////////////// Implementation //////////////////////////////const UINT wm_TaskbarCreated = RegisterWindowMessage(_T("TaskbarCreated"));IMPLEMENT_DYNAMIC(CTrayRessurectionWnd, CFrameWnd)BEGIN_MESSAGE_MAP(CTrayRessurectionWnd, CFrameWnd)
//{{AFX_MSG_MAP(CTrayRessurectionWnd)
//}}AFX_MSG_MAP
ON_REGISTERED_MESSAGE(wm_TaskbarCreated, OnTaskbarCreated)
END_MESSAGE_MAP()LRESULT CTrayRessurectionWnd::OnTaskbarCreated(WPARAM wParam, LPARAM lParam)
{
ASSERT(m_pTrayIcon); //Refresh the tray icon if necessary
if (m_pTrayIcon->IsShowing())
{
m_pTrayIcon->HideIcon();
m_pTrayIcon->ShowIcon();
} return 0L;
}CTrayRessurectionWnd::CTrayRessurectionWnd(CTrayNotifyIcon* pTrayIcon)
{
//must have at valid tray notify instance
ASSERT(pTrayIcon); //Store the values away
m_pTrayIcon = pTrayIcon;
}IMPLEMENT_DYNAMIC(CTrayTimerWnd, CFrameWnd)BEGIN_MESSAGE_MAP(CTrayTimerWnd, CFrameWnd)
//{{AFX_MSG_MAP(CTrayTimerWnd)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()CTrayTimerWnd::CTrayTimerWnd(CTrayNotifyIcon* pTrayIcon, HICON* phIcons, int nNumIcons, DWORD dwDelay)
{
m_nCurrentIconIndex = 0; //must have a valid tray notify instance
ASSERT(pTrayIcon); //must have at least 1 icon
ASSERT(nNumIcons); //array of icon handles must be valid
ASSERT(phIcons); //must be non zero timer interval
ASSERT(dwDelay); //Store the values away
m_pTrayIcon = pTrayIcon; m_phIcons = new HICON[nNumIcons];
CopyMemory(m_phIcons, phIcons, nNumIcons * sizeof(HICON));
m_nNumIcons = nNumIcons;
m_dwDelay = dwDelay;
}CTrayTimerWnd::~CTrayTimerWnd()
{
if (m_phIcons)
{
delete [] m_phIcons;
m_phIcons = NULL;
}
}int CTrayTimerWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
//create the animation timer
m_nTimerID = SetTimer(1, m_dwDelay, NULL);
return 0;
}void CTrayTimerWnd::OnDestroy()
{
//kill the animation timer
KillTimer(m_nTimerID); CFrameWnd::OnDestroy();
}void CTrayTimerWnd::OnTimer(UINT nIDEvent)
{
//increment the icon index
++m_nCurrentIconIndex;
m_nCurrentIconIndex = m_nCurrentIconIndex % m_nNumIcons; //update the tray icon
m_pTrayIcon->m_NotifyIconData.uFlags = NIF_ICON;
m_pTrayIcon->m_NotifyIconData.hIcon = m_phIcons[m_nCurrentIconIndex];
Shell_NotifyIcon(NIM_MODIFY, &m_pTrayIcon->m_NotifyIconData);
}
IMPLEMENT_DYNAMIC(CTrayNotifyIcon, CObject)CTrayNotifyIcon::CTrayNotifyIcon()
{
memset(&m_NotifyIconData, 0, sizeof(m_NotifyIconData));
m_bCreated = FALSE;
m_bHidden = FALSE;
m_pNotificationWnd = NULL;
m_pResurrectionWnd = NULL;
m_pTimerWnd = NULL;
m_bAnimated = FALSE;
}CTrayNotifyIcon::~CTrayNotifyIcon()
{
DestroyTimerWindow();
DestroyResurrectionWindow();
RemoveIcon();
}void CTrayNotifyIcon::HideIcon()
{
ASSERT(m_bCreated);
if (!m_bHidden)
{
m_NotifyIconData.uFlags = NIF_ICON;
Shell_NotifyIcon(NIM_DELETE, &m_NotifyIconData);
m_bHidden = TRUE;
}
}void CTrayNotifyIcon::ShowIcon()
{
ASSERT(m_bCreated);
if (m_bHidden)
{
m_NotifyIconData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
Shell_NotifyIcon(NIM_ADD, &m_NotifyIconData);
m_bHidden = FALSE;
}
}void CTrayNotifyIcon::RemoveIcon()
{
if (m_bCreated)
{
m_NotifyIconData.uFlags = 0;
Shell_NotifyIcon(NIM_DELETE, &m_NotifyIconData);
m_bCreated = FALSE;
}
}void CTrayNotifyIcon::MoveToExtremeRight()
{
HideIcon();
ShowIcon();
}BOOL CTrayNotifyIcon::Create(CWnd* pNotifyWnd, UINT uID, LPCTSTR pszTooltipText, HICON hIcon, UINT nNotifyMessage)
{
//Create the ressurection window
if (!CreateRessurectionWindow())
return FALSE; //Make sure Notification window is valid
ASSERT(pNotifyWnd && ::IsWindow(pNotifyWnd->GetSafeHwnd()));
m_pNotificationWnd = pNotifyWnd; //Make sure we avoid conflict with other messages
ASSERT(nNotifyMessage >= WM_USER); //Tray only supports tooltip text up to 64 characters
ASSERT(_tcslen(pszTooltipText) <= 64); m_NotifyIconData.cbSize = sizeof(m_NotifyIconData);
m_NotifyIconData.hWnd = pNotifyWnd->GetSafeHwnd();
m_NotifyIconData.uID = uID;
m_NotifyIconData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
m_NotifyIconData.uCallbackMessage = nNotifyMessage;
m_NotifyIconData.hIcon = hIcon;
_tcscpy(m_NotifyIconData.szTip, pszTooltipText); BOOL rVal = Shell_NotifyIcon(NIM_ADD, &m_NotifyIconData);
m_bCreated = rVal; return rVal;
}BOOL CTrayNotifyIcon::CreateRessurectionWindow()
{
//Create the resurrection window
ASSERT(m_pResurrectionWnd == NULL);
m_pResurrectionWnd = new CTrayRessurectionWnd(this);
if (!m_pResurrectionWnd)
return FALSE;
if (!m_pResurrectionWnd->Create(NULL, _T("CTrayNotifyIcon Resurrection Notification Window")))
return FALSE; return TRUE;
}void CTrayNotifyIcon::DestroyResurrectionWindow()
{
if (m_pResurrectionWnd)
{
m_pResurrectionWnd->SendMessage(WM_CLOSE);
m_pResurrectionWnd = NULL;
}
}BOOL CTrayNotifyIcon::CreateTimerWindow(HICON* phIcons, int nNumIcons, DWORD dwDelay)
{
//create the hidden window which will contain the timer which will do the animation
ASSERT(m_pTimerWnd == NULL);
m_pTimerWnd = new CTrayTimerWnd(this, phIcons, nNumIcons, dwDelay);
if (!m_pTimerWnd)
return FALSE;
if (!m_pTimerWnd->Create(NULL, _T("CTrayNotifyIcon Animation Notification Window")))
return FALSE; return TRUE;
}void CTrayNotifyIcon::DestroyTimerWindow()
{
if (m_pTimerWnd)
{
m_pTimerWnd->SendMessage(WM_CLOSE);
m_pTimerWnd = NULL;
}
}BOOL CTrayNotifyIcon::Create(CWnd* pNotifyWnd, UINT uID, LPCTSTR pszTooltipText, HICON* phIcons, int nNumIcons, DWORD dwDelay, UINT nNotifyMessage)
{
//must be using at least 2 icons
ASSERT(nNumIcons >= 2); if (!CreateTimerWindow(phIcons, nNumIcons, dwDelay))
return FALSE; //let the normal Create function do its stuff
BOOL bSuccess = Create(pNotifyWnd, uID, pszTooltipText, phIcons[0], nNotifyMessage);
m_bAnimated = TRUE;
return bSuccess;
}BOOL CTrayNotifyIcon::SetTooltipText(LPCTSTR pszTooltipText)
{
if (!m_bCreated)
return FALSE; m_NotifyIconData.uFlags = NIF_TIP;
_tcscpy(m_NotifyIconData.szTip, pszTooltipText); return Shell_NotifyIcon(NIM_MODIFY, &m_NotifyIconData);
}BOOL CTrayNotifyIcon::SetTooltipText(UINT nID)
{
CString sToolTipText;
VERIFY(sToolTipText.LoadString(nID)); return SetTooltipText(sToolTipText);
}BOOL CTrayNotifyIcon::SetIcon(HICON hIcon)
{
if (!m_bCreated)
return FALSE; DestroyTimerWindow();
m_bAnimated = FALSE;
m_NotifyIconData.uFlags = NIF_ICON;
m_NotifyIconData.hIcon = hIcon; return Shell_NotifyIcon(NIM_MODIFY, &m_NotifyIconData);
}BOOL CTrayNotifyIcon::SetIcon(LPCTSTR lpIconName)
{
HICON hIcon = AfxGetApp()->LoadIcon(lpIconName); return SetIcon(hIcon);
}BOOL CTrayNotifyIcon::SetIcon(UINT nIDResource)
{
HICON hIcon = AfxGetApp()->LoadIcon(nIDResource); return SetIcon(hIcon);
}BOOL CTrayNotifyIcon::SetStandardIcon(LPCTSTR lpIconName)
{
HICON hIcon = LoadIcon(NULL, lpIconName); return SetIcon(hIcon);
}BOOL CTrayNotifyIcon::SetStandardIcon(UINT nIDResource)
{
HICON hIcon = LoadIcon(NULL, MAKEINTRESOURCE(nIDResource)); return SetIcon(hIcon);
}BOOL CTrayNotifyIcon::SetIcon(HICON* phIcons, int nNumIcons, DWORD dwDelay)
{
ASSERT(nNumIcons >= 2);
ASSERT(phIcons); if (!SetIcon(phIcons[0]))
return FALSE; DestroyTimerWindow();
if (!CreateTimerWindow(phIcons, nNumIcons, dwDelay))
return FALSE;
m_bAnimated = TRUE; return TRUE;
}BOOL CTrayNotifyIcon::SetNotificationWnd(CWnd* pNotifyWnd)
{
if (!m_bCreated)
return FALSE; //Make sure Notification window is valid
ASSERT(pNotifyWnd && ::IsWindow(pNotifyWnd->GetSafeHwnd())); m_pNotificationWnd = pNotifyWnd;
m_NotifyIconData.hWnd = pNotifyWnd->GetSafeHwnd();
m_NotifyIconData.uFlags = 0; return Shell_NotifyIcon(NIM_MODIFY, &m_NotifyIconData);
}CString CTrayNotifyIcon::GetTooltipText() const
{
CString sText;
if (m_bCreated)
sText = m_NotifyIconData.szTip; return sText;
}HICON CTrayNotifyIcon::GetIcon() const
{
HICON hIcon = NULL;
if (m_bCreated)
{
if (m_bAnimated)
hIcon = m_pTimerWnd->GetCurrentIcon();
else
hIcon = m_NotifyIconData.hIcon;
} return hIcon;
}CWnd* CTrayNotifyIcon::GetNotificationWnd() const
{
return m_pNotificationWnd;
}LRESULT CTrayNotifyIcon::OnTrayNotification(WPARAM wID, LPARAM lEvent)
{
//Return quickly if its not for this tray icon
if (wID != m_NotifyIconData.uID)
return 0L; //As a default action use a menu resource with the same id
//as this was created with
CMenu menu;
if (!menu.LoadMenu(m_NotifyIconData.uID))
return 0; CMenu* pSubMenu = menu.GetSubMenu(0);
if (!pSubMenu)
return 0; if (lEvent == WM_RBUTTONUP)
{
//Clicking with right button brings up a context menu // 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_NotifyIconData.hWnd);
::TrackPopupMenu(pSubMenu->m_hMenu, 0, pos.x, pos.y, 0, m_NotifyIconData.hWnd, NULL); }
else if (lEvent == WM_LBUTTONDBLCLK)
{
// double click received, the default action is to execute first menu item
::SendMessage(m_NotifyIconData.hWnd, WM_COMMAND, pSubMenu->GetMenuItemID(0), 0);
} return 1; // handled
}
解决方案 »
- CString类型转化
- 今天晚上想的一个问题?
- 如何禁止webbrowers响应滚动事件?
- VC中如何操作mysql?
- dbsqlok()
- 本人急需Excel9.OLB文件
- 如果不相信我有8000多的可用分的话,你就叫版主封了我的ID
- 记录集 movefirst后 在表格的最后一行出现全部为NULL的一行什么原因
- 请问使用CWebBrowser的对象如何得到当前显示的网页的标题(title)?在线等待中......
- mfc 实现高亮显示文本框查找字符串
- 我想结合主域服务器的组、用户等,一句话,就是能用API完成域用户管理器的工作,是不是很复杂呢?另外,我是针对NT4,并不是对于WIN2000(因为WIN2000可以用ADSI),怎么办呢?C++的高手们可以写一段程序给我学习吗?急死人了!!!
- 为什么Com口的条码枪在winme下一通电就死机!兄弟们帮帮我啊!
而且使用起来相当地简单