📄 systemtray.cpp
字号:
// SystemTray.cpp : implementation file
//
#include "stdafx.h"
#include "SystemTray.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSystemTray
CSystemTray::CSystemTray()
{
m_NotifyIconData.cbSize = sizeof(NOTIFYICONDATA);//托盘数据结构
ZeroMemory( m_NotifyIconData.szTip, sizeof(m_NotifyIconData.szTip) );
m_uMenuIDResource = 0;//菜单资源
m_nSubMenu = 0;//子菜单项
m_hMenu = 0;//托盘默认弹出的菜单
m_hMenuWnd = 0;//默认菜单关联的窗口
AddMenuMessage( WM_LBUTTONDOWN );//鼠标右键激活默认菜单
AddMenuMessage( WM_RBUTTONDOWN );//鼠标左键激活默认菜单
m_uTimerID = 0;//定时器ID;
m_uInterval = 1000;//定时器时间间隔
m_pIconList = NULL;//按顺序显示的图标(动画图标序列)
}
/////////////////////////////////
CSystemTray::CSystemTray( CWnd* pOwner, UINT uCallbackMessage, HICON hIcon, UINT uID, char* pszTips )
{
m_NotifyIconData.cbSize = sizeof(NOTIFYICONDATA);//托盘数据结构
ZeroMemory( m_NotifyIconData.szTip, sizeof(m_NotifyIconData.szTip) );
m_uMenuIDResource = 0;//菜单资源
m_nSubMenu = 0;//子菜单项
m_hMenu = 0;//托盘默认弹出的菜单
m_hMenuWnd = 0;//默认菜单关联的窗口
AddMenuMessage( WM_LBUTTONDOWN );//鼠标右键激活默认菜单
AddMenuMessage( WM_RBUTTONDOWN );//鼠标左键激活默认菜单
m_uTimerID = 0;//定时器ID;
m_uInterval = 1000;//定时器时间间隔
m_pIconList = NULL;//按顺序显示的图标(动画图标序列)
//
CreateTray( pOwner, uCallbackMessage, hIcon, uID, pszTips );
}
/////////////////////////////////
CSystemTray::~CSystemTray()
{
StopAnimation();
//
if( m_pIconList != NULL )
{
m_pIconList->DeleteImageList();
delete m_pIconList;
m_pIconList = NULL;
}
//
RemoveIcon();
//
DestroyWindow();
//
}
/////////////////////////////////////////////////////////////////////////////
BEGIN_MESSAGE_MAP(CSystemTray, CWnd)
//{{AFX_MSG_MAP(CSystemTray)
// NOTE - the ClassWizard will add and remove mapping macros here.
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSystemTray message handlers
void CSystemTray::AddIcon()
{
//将托盘图标加入到系统托盘区
Shell_NotifyIcon( NIM_ADD, &m_NotifyIconData );
}
void CSystemTray::RemoveIcon()
{
//将托盘图标移出系统托盘区
Shell_NotifyIcon( NIM_DELETE, &m_NotifyIconData );
}
void CSystemTray::ModifyIcon()
{
//更新托盘
Shell_NotifyIcon( NIM_MODIFY, &m_NotifyIconData );
}
void CSystemTray::SetOwnerWnd( HWND hWnd )
{
//设置处理托盘消息的窗口句柄
m_NotifyIconData.hWnd = hWnd;
}
HWND CSystemTray::GetOwnerWnd()const
{
//读取处理托盘消息的窗口句柄
return m_NotifyIconData.hWnd;
}
void CSystemTray::SetTips( char* pszTips )
{
//设置提示字符串,有效长度为64字节
strcpy( m_NotifyIconData.szTip, pszTips );
}
CString CSystemTray::GetTips()const
{
//读取提示字符串
return CString( m_NotifyIconData.szTip );
}
void CSystemTray::SetTrayIcon( HICON hIcon )
{
//设置托盘图标
m_NotifyIconData.hIcon = hIcon;
}
HICON CSystemTray::GetTrayIcon()const
{
//读取托盘图标
return m_NotifyIconData.hIcon;
}
void CSystemTray::SetCallbackMessage( UINT uCallbackMessage )
{
//设置托盘向拥有托盘的窗口发送的消息(自定义的消息)
m_NotifyIconData.uCallbackMessage = uCallbackMessage;
}
UINT CSystemTray::GetCallbackMessage()const
{
//返回托盘向拥有托盘的窗口发送的消息
return m_NotifyIconData.uCallbackMessage;
}
void CSystemTray::SetDefaultMenu( UINT nIDResource, int nSubMenu )
{
//根据资源创建菜单
m_uMenuIDResource = nIDResource;//菜单资源
m_nSubMenu = nSubMenu;//子菜单项
}
void CSystemTray::SetSubMenu( int nSubMenu )
{
//资源子菜单
m_nSubMenu = nSubMenu;//子菜单项
}
void CSystemTray::SetDefaultMenu( HMENU hMenu )
{
//设置默认菜单
m_hMenu = hMenu;
}
HMENU CSystemTray::GetDefaultMenu()const
{
//读取默认菜单
return m_hMenu;
}
void CSystemTray::SetMenuWnd( HWND hWnd )
{
//设置处理默认菜单消息的窗口(默认菜单关联的窗口)
m_hMenuWnd = hWnd;
}
HWND CSystemTray::GetMenuWnd()const
{
//读取处理默认菜单消息的窗口(默认菜单关联的窗口)
return m_hMenuWnd;
}
void CSystemTray::AddMenuMessage( WORD message, UINT uMenuID, int nSubMenu, HMENU hMenu, HWND hMenuWnd )
{
//添加激活默认菜单的消息
MENUMESSAGE menuMsg = { message, uMenuID, nSubMenu, hMenu, hMenuWnd };
//
int sizes = m_arrMenuMessages.GetSize();
int index = 0;
for( ;index < sizes; index++ )
{
if( m_arrMenuMessages[index].m_wMessage == message )
{
m_arrMenuMessages[index] = menuMsg;
return;
}
}
//
m_arrMenuMessages.Add( menuMsg );
}
void CSystemTray::RemoveMenuMessage( WORD message )
{
//移出激活默认菜单的消息
int sizes = m_arrMenuMessages.GetSize();
int index = 0;
for( ;index < sizes; index++ )
{
if( m_arrMenuMessages[index].m_wMessage == message )
{
m_arrMenuMessages.RemoveAt( index );
return;
}
}
}
MENUMESSAGE* CSystemTray::GetMenuMessage( WORD message )
{
//检测激活默认菜单的消息
int sizes = m_arrMenuMessages.GetSize();
int index = 0;
for( ; index < sizes; index++ )
{
if( m_arrMenuMessages[index].m_wMessage == message )
{
return &(m_arrMenuMessages[index]);
}
}
return NULL;
}
void CSystemTray::SetInterval( UINT uInterval)
{
//设置定时器时间间隔
m_uInterval = uInterval;
}
UINT CSystemTray::GetInterval()const
{
//读取定时器时间间隔
return m_uInterval;
}
BOOL CSystemTray::PlayAnimation()
{
//开始动画显示图标
if( m_uTimerID > 0 )
{
KillTimer( m_uTimerID );
}
m_uTimerID = SetTimer( 1, GetInterval(), NULL );
return (m_uTimerID > 0);
}
BOOL CSystemTray::StopAnimation()
{
//停止动画显示图标
if( m_uTimerID > 0 )
{
BOOL result = KillTimer( m_uTimerID );
m_uTimerID = 0;
return result;
}
return FALSE;
}
BOOL CSystemTray::CreateIconList( CImageList* pIconList )
{
//创建动画显示的图标序列
if( m_pIconList == NULL )
{
m_pIconList = new CImageList;
}
else
{
m_pIconList->DeleteImageList();
}
return m_pIconList->Create( pIconList );
}
/////////////////////////////////////////////////////////////////////////////////
BOOL CSystemTray::CreateTray( CWnd* pOwner, UINT uCallbackMessage, HICON hIcon, UINT uID, char* pszTips )
{
ASSERT( uCallbackMessage >= WM_USER );
//
CWnd::CreateEx( 0, AfxRegisterWndClass(0), _T(""), 0, 0, 0, 0, 0, 0, 0 );
//
m_NotifyIconData.cbSize = sizeof(NOTIFYICONDATA);
m_NotifyIconData.hWnd = pOwner != NULL ? pOwner->GetSafeHwnd() : m_hWnd;
m_NotifyIconData.uID = uID;
m_NotifyIconData.hIcon = hIcon;
m_NotifyIconData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
m_NotifyIconData.uCallbackMessage = uCallbackMessage;
SetTips( pszTips );
return TRUE;
}
//////////////////////////////////////////////////////
LRESULT CSystemTray::OnTrayNotification( WPARAM wParam, LPARAM lParam )
{
if( wParam != m_NotifyIconData.uID )
{
return 0;
}
//
MENUMESSAGE* pMenuMsg = GetMenuMessage( LOWORD(lParam) );
if( pMenuMsg == NULL )//是否激活菜单的消息?
{
return 0;
}
//
HWND hMenuWnd = pMenuMsg->m_hMenuWnd ? pMenuMsg->m_hMenuWnd : GetMenuWnd();
//
if( !hMenuWnd )//是否有窗口处理菜单命令
{
return 0;
}
//
//优先根据ID资源创建菜单,
//如果失败则根据句柄资源读取菜单
//前一种方式创建的菜单需要销毁,
//最后一种方式读取的菜单不能销毁
HMENU hMenu = 0;
CMenu menu;
BOOL loadedMenu = FALSE;
//
if( pMenuMsg->m_uMenuID > 0 )
{
loadedMenu = menu.LoadMenu( pMenuMsg->m_uMenuID );
}
if( loadedMenu )
{
if( pMenuMsg->m_nSubMenu >= 0 )
{
CMenu* pSubMenu = menu.GetSubMenu( pMenuMsg->m_nSubMenu );
if( pSubMenu )
{
hMenu = pSubMenu->GetSafeHmenu();
}
}
else
{
hMenu = menu.GetSafeHmenu();
}
}
//
if( !hMenu )
{
hMenu = pMenuMsg->m_hMenu;
}
//
if( !hMenu && ( m_uMenuIDResource > 0 ) )
{
loadedMenu = menu.LoadMenu( m_uMenuIDResource );
if( loadedMenu )
{
if( m_nSubMenu >= 0 )
{
CMenu* pSubMenu = menu.GetSubMenu( m_nSubMenu );
if( pSubMenu )
{
hMenu = pSubMenu->GetSafeHmenu();
}
}
else
{
hMenu = menu.GetSafeHmenu();
}
}
}
//
if( !hMenu )
{
hMenu = GetDefaultMenu();
}
//
if( !hMenu )
{
return 0;
}
//
CWnd::FromHandle( hMenuWnd )->SetForegroundWindow();
CPoint pos;
GetCursorPos( &pos );
::TrackPopupMenu( hMenu, 0, pos.x, pos.y, 0, hMenuWnd, NULL );
if( loadedMenu )
{
menu.DestroyMenu();
}
return 1;
}
/////////////////////////////////////////////////////////////////////////////
void CSystemTray::OnTimer(UINT nIDEvent)
{
if( nIDEvent == m_uTimerID )
{
if( m_pIconList != NULL )
{
if( m_pIconList->GetImageCount() > 0 )
{
//动态改变托盘区图标
static int nImage = 0;
SetTrayIcon( m_pIconList->ExtractIcon( nImage % m_pIconList->GetImageCount() ) );
ModifyIcon();
nImage++;
}
}
}
CWnd::OnTimer(nIDEvent);
}
LRESULT CSystemTray::WindowProc( UINT message, WPARAM wParam, LPARAM lParam )
{
if( message == GetCallbackMessage() )
{
return OnTrayNotification( wParam, lParam );
}
return CWnd::WindowProc( message, wParam, lParam );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -