考在线帮助
DECLARE_DYNAMIC(CSystemIcon)
};
#endif
/////////////////////////////////////////////////////////////////////////////
b、CsystemIcon类.CPP源文件:
/////////////////////////////////////////////////////////////////////////////
// SystemIcon.cpp : implementation file
#include "stdafx.h"
#include "SystemIcon.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE = __FILE__;
#endif
IMPLEMENT_DYNAMIC(CSystemIcon, CObject)
/////////////////////////////////////////////////////////////////////////////
CSystemIcon::CSystemIcon()
memset(&m_tnd, 0, sizeof(m_tnd));
m_bEnabled = FALSE;
m_bHidden = FALSE;
CSystemIcon::CSystemIcon(CWnd* pWnd, UINT uCallbackMessage, LPCTSTR szToolTip,
HICON icon, UINT uID)
Create(pWnd, uCallbackMessage, szToolTip, icon, uID);
m_bHidden = FALSE;
BOOL CSystemIcon::Create(CWnd* pWnd, UINT uCallbackMessage, LPCTSTR szToolTip,
HICON icon, UINT uID)
//文件只能使用在WINDOW 95以上的版本中
VERIFY(m_bEnabled = ( GetVersion() & 0xff ) >= 4);
if (!m_bEnabled) return FALSE;
//确认通知窗口有效
VERIFY(m_bEnabled = (pWnd && ::IsWindow(pWnd->GetSafeHwnd())));
if (!m_bEnabled) return FALSE;
//确认自定义消息大于WM_USER
ASSERT(uCallbackMessage >= WM_USER);
//确定提示文本长度小于64
ASSERT(_tcslen(szToolTip) <= 64);
//定义NOTIFYICONDATA结构的数据项
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);
//设置图标
VERIFY(m_bEnabled = Shell_NotifyIcon(NIM_ADD, &m_tnd));
return m_bEnabled;
CSystemIcon::~CSystemIcon()
DeleteIcon();
/////////////////////////////////////////////////////////////////////////////
void CSystemIcon::DeleteIcon()
if (!m_bEnabled) return;
m_tnd.uFlags = 0;
Shell_NotifyIcon(NIM_DELETE, &m_tnd);
m_bEnabled = FALSE;
void CSystemIcon::HideIcon()
if (m_bEnabled && !m_bHidden) {
m_tnd.uFlags = NIF_ICON;
Shell_NotifyIcon (NIM_DELETE, &m_tnd);
m_bHidden = TRUE;
void CSystemIcon::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 CSystemIcon::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 CSystemIcon::SetIcon(LPCTSTR lpszIconName)
HICON hIcon = AfxGetApp()->LoadIcon(lpszIconName);
return SetIcon(hIcon);
BOOL CSystemIcon::SetIcon(UINT nIDResource)
HICON hIcon = AfxGetApp()->LoadIcon(nIDResource);
return SetIcon(hIcon);
BOOL CSystemIcon::SetStandardIcon(LPCTSTR lpIconName)
HICON hIcon = LoadIcon(NULL, lpIconName);
return SetIcon(hIcon);
BOOL CSystemIcon::SetStandardIcon(UINT nIDResource)
HICON hIcon = LoadIcon(NULL, MAKEINTRESOURCE(nIDResource));
return SetIcon(hIcon);
HICON CSystemIcon::GetIcon() const
HICON hIcon = NULL;
if (m_bEnabled)
hIcon = m_tnd.hIcon;
retur