📄 traynotifyicon.cpp
字号:
// TrayNotifyIcon.cpp: implementation of the CTrayNotifyIcon class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "LeoBlock2004.h"
#include "TrayNotifyIcon.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNAMIC(CTrayNotifyIcon,CObject)
CTrayNotifyIcon::CTrayNotifyIcon()
{
memset(&m_NotifyIconData,0,sizeof(m_NotifyIconData));
m_bCreated=FALSE;
m_bHidden=FALSE;
m_pNotificationWnd=NULL;
m_bIsWindowNormal=TRUE;
}
CTrayNotifyIcon::~CTrayNotifyIcon()
{
RemoveIcon();
}
BOOL CTrayNotifyIcon::Create(CWnd *pNotifyWnd,UINT uID,LPCTSTR pszTooltipText,HICON hIcon,UINT nNotifyMessage)
{
//确保pNotifyWnd指向的窗体有效
ASSERT(pNotifyWnd && ::IsWindow(pNotifyWnd->GetSafeHwnd()));
m_pNotificationWnd=pNotifyWnd;
//nNotifyMessage包含的Windows消息是否超过了Windows定义的用户消息
ASSERT(nNotifyMessage>=WM_USER);
//托盘中的工具提示信息最多包含64个字符
ASSERT(_tcslen(pszTooltipText)<=64);
//设置必要的NOTIFYICONDATA数据
//该数据结构对于托盘的生成很重要
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;
}
HICON CTrayNotifyIcon::GetIcon() const
{
HICON hIcon=NULL;
if(m_bCreated)
hIcon=m_NotifyIconData.hIcon;
return hIcon;
}
void CTrayNotifyIcon::RemoveIcon()
{
if(m_bCreated)
{
m_NotifyIconData.uFlags=0;
Shell_NotifyIcon(NIM_DELETE,&m_NotifyIconData);
m_bCreated=FALSE;
}
}
BOOL CTrayNotifyIcon::SetIcon(HICON hIcon)
{
if(!m_bCreated)
return 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=LoadIcon(NULL,MAKEINTRESOURCE(nIDResource));
return SetIcon(hIcon);
}
LRESULT CTrayNotifyIcon::OnTrayNotification(WPARAM wID,LPARAM lEvent)
{
//如果wID不是已经定义好的菜单,迅速返回
if(wID!=m_NotifyIconData.uID)
return 0L;
//加载菜单
CMenu menu;
if(!menu.LoadMenu(m_NotifyIconData.uID))
return 0;
CMenu *pSubMenu=menu.GetSubMenu(0);
if(!pSubMenu)
return 0;
//右键单击图标,弹出菜单
//左键双击图标,窗口激活或隐藏
if(lEvent==WM_RBUTTONUP)
{
//使菜单的第一项变粗体
//::SetMenuDefaultItem(pSubMenu->m_hMenu,0,TRUE);
//显示并追踪鼠标信息
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)
{
if(!m_bIsWindowNormal)
::SendMessage(m_NotifyIconData.hWnd,WM_SHOWWINDOWL,0,0);
else
::SendMessage(m_NotifyIconData.hWnd,WM_HIDDEWINDOW,0,0);
}
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -