/*
Module : NTRAY.CPP
Purpose: implementation for a MFC class to encapsulate Shell_NotifyIcon
Created: PJN / 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 updated
PJN / 27-01-1999 : Code first tries to load a 16*16 icon before loading the 32*32
version. This removes the blurryness which was previously occuring
PJN / 28-01-1999 : Fixed a number of level 4 warnings which were occurring.
PJN / 09-05-1999 : Fixed a problem as documented in KB article "PRB: Menus for
Notification Icons Do Not Work Correctly", Article ID: Q135788
PJN / 15-05-1999 : 1. Now uses the author's hookwnd class. This prevents the need to
create the two hidden windows namely CTrayRessurectionWnd and
CTrayTimerWnd
2. Code now compiles cleanly at warning level 4
3. General code tidy up and rearrangement
4. Added numerous ASSERT's to improve the code's robustness
5. Added functions to allow context menu to be customized
PJN / 01-01-2001 : 1. Now includes copyright message in the source code and documentation.
2. Fixed problem where the window does not get focus after double clicking
on the tray icon
3. Now fully supports the Windows 2000 balloon style tooltips
4. Fixed a off by one problem in some of the ASSERT's
5. Fixed problems with Unicode build configurations
6. Provided Win2k specific build configurations
Copyright (c) 1996 - 2001 by PJ Naughter. (Web: www.naughter.com, Email: pjna@naughter.com)
All rights reserved.
Copyright / Usage Details:
You are allowed to include the source code in any product (commercial, shareware, freeware or otherwise)
when your product is released in binary form. You are allowed to modify the source code in any way you want
except you cannot modify the copyright details at the top of each module. If you want to distribute source
code with your application, then you are only allowed to distribute versions released by the author. This is
to maintain a single distribution point for the source code.
*/
///////////////////////////////// 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"));
const UINT g_nTrayTimer = 101;
CTrayIconHooker::CTrayIconHooker()
{
//Set all of our variables to defaults
m_pTrayIcon = NULL;
m_phIcons = NULL;
m_nNumIcons = 0;
m_nTimerID = 0;
m_nCurrentIconIndex = 0;
}
CTrayIconHooker::~CTrayIconHooker()
{
StopUsingAnimation();
}
void CTrayIconHooker::Init(CTrayNotifyIcon* pTrayIcon, CWnd* pNotifyWnd)
{
//Validate our parameters
ASSERT(!IsHooked());
ASSERT(pTrayIcon); //must have a valid tray notify instance
ASSERT(pNotifyWnd && ::IsWindow(pNotifyWnd->GetSafeHwnd()));
//Hook the top level frame of the notify window in preference
//to the notify window itself. This will ensure that we get
//the taskbar created message
CWnd* pTopLevelWnd = pNotifyWnd->GetTopLevelFrame();
if (pTopLevelWnd)
Hook(pTopLevelWnd);
else
Hook(pNotifyWnd);
m_pTrayIcon = pTrayIcon;
}
void CTrayIconHooker::StartUsingAnimation(HICON* phIcons, int nNumIcons, DWORD dwDelay)
{
//Validate our parameters
ASSERT(nNumIcons >= 2); //must be using at least 2 icons if you are using animation
ASSERT(phIcons); //array of icon handles must be valid
ASSERT(dwDelay); //must be non zero timer interval
//Stop the animation if already started
StopUsingAnimation();
//Hive away all the values locally
m_phIcons = new HICON[nNumIcons];
for (int i=0; i<nNumIcons; i++)
m_phIcons[i] = phIcons[i];
m_nNumIcons = nNumIcons;
//Start up the timer
m_nTimerID = m_pOriginalWnd->SetTimer(g_nTrayTimer, dwDelay, NULL);
}
void CTrayIconHooker::StopUsingAnimation()
{
//Kill the timer
if (m_nTimerID)
{
if (m_pOriginalWnd && ::IsWindow(m_pOriginalWnd->GetSafeHwnd()))
m_pOriginalWnd->KillTimer(m_nTimerID);
m_nTimerID = 0;
}
//Free up the memory
if (m_phIcons)
{
delete [] m_phIcons;
m_phIcons = NULL;
}
//Reset the other animation related variables
m_nCurrentIconIndex = 0;
m_nNumIcons = 0;
}
BOOL CTrayIconHooker::UsingAnimatedIcon() const
{
return (m_nNumIcons != 0);
}
HICON CTrayIconHooker::GetCurrentIcon() const
{
ASSERT(UsingAnimatedIcon());
ASSERT(m_phIcons);
return m_phIcons[m_nCurrentIconIndex];
}
LRESULT CTrayIconHooker::WindowProc(UINT nMsg, WPARAM wParam, LPARAM lParam)
{
LRESULT lResult = 0;
if (nMsg == wm_TaskbarCreated)
lResult = OnTaskbarCreated(wParam, lParam);
else
{
switch (nMsg)
{
case WM_TIMER:
{
if (wParam == g_nTrayTimer) //It's our timer
OnTimer(wParam);
else
lResult = Default();
break;
}
default:
{
lResult = Default();
break;
}
}
}
return lResult;
}
#ifdef _DEBUG
void CTrayIconHooker::OnTimer(UINT nIDEvent)
#else
void CTrayIconHooker::OnTimer(UINT /*nIDEvent*/) //Just to avoid a compiler warning
#endif //when being built for release
{
ASSERT(nIDEvent == m_nTimerID);
//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, (PNOTIFYICONDATA) &m_pTrayIcon->m_NotifyIconData);
}
LRESULT CTrayIconHooker::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;
}
IMPLEMENT_DYNAMIC(CTrayNotifyIcon, CObject)
CTrayNotifyIcon::CTrayNotifyIcon()
{
ZeroMemory(&m_NotifyIconData, sizeof(NOTIFYICONDATA_2));
m_bCreated = FALSE;
m_bHidden = FALSE;
m_pNotificationWnd = NULL;
}
CTrayNotifyIcon::~CTrayNotifyIcon()
{
RemoveIcon();
}
void CTrayNotifyIcon::HideIcon()
{
ASSERT(m_bCreated);
if (!m_bHidden)
{
m_NotifyIconData.uFlags = NIF_ICON;
Shell_NotifyIcon(NIM_DELETE, (PNOTIFYICONDATA) &m_NotifyIconData);
m_bHidden = TRUE;
}
}
void CTrayNotifyIcon::ShowIcon()
{
ASSERT(m_bCreated);
if (m_bHidden)
{
m_NotifyIconData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
Shell_Not
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ntray.zip (16个子文件)
StdAfx.h 424B
RES
happy.ico 1KB
Nottest.rc2 399B
sad.ico 1KB
MainFrm.cpp 3KB
NTRAY.HTM 32KB
MainFrm.h 697B
Traytest.dsw 541B
Ntray.cpp 19KB
Traytest.rc 4KB
resource.h 779B
Ntray.h 5KB
TRAYTEST.H 432B
Traytest.dsp 12KB
StdAfx.cpp 205B
TRAYTEST.CPP 919B
共 16 条
- 1
资源评论
普通网友
- 粉丝: 882
- 资源: 2万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功