📄 traynotifyicon.cpp
字号:
// TrayNotifyIcon.cpp: implementation of the CTrayNotifyIcon class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.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()
{
//设置m_NotifyIconData中的数据为0
memset(&m_NotifyIconData,0,sizeof(m_NotifyIconData));
}
CTrayNotifyIcon::~CTrayNotifyIcon()
{
m_NotifyIconData.uFlags = 0;
Shell_NotifyIcon(NIM_DELETE,&m_NotifyIconData);
}
BOOL CTrayNotifyIcon::Create(CWnd *pNotifyWnd, UINT uID, LPCTSTR pszTooltipText, HICON hIcon, UINT nNotifyMessage)
{
//确保pNotifyWnd指向的窗体有效
ASSERT(pNotifyWnd && ::IsWindow(pNotifyWnd->GetSafeHwnd()));
//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);
//调用Shell_NotifyIcon()函数
BOOL rVal = Shell_NotifyIcon(NIM_ADD,&m_NotifyIconData);
return rVal;
}
LRESULT CTrayNotifyIcon::OnTrayNotification(WPARAM wID, LPARAM lEvent)
{
//当数据不是返回给该元素时,迅速返回
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;
//测试消息
//可以在此添加其他的消息响应
//该类仅仅封装了WM_RBUTTONUP和WM_LBUTTONDBLCLK消息
if(lEvent == WM_RBUTTONUP)
{
//当右键单击Icon图标时,弹出菜单并使其第一个选项变为粗体
::SetMenuDefaultItem(pSubMenu->m_hMenu,3,true);
pSubMenu->EnableMenuItem(pSubMenu->GetMenuItemID(0),m_bIsWindowNormal);
pSubMenu->EnableMenuItem(pSubMenu->GetMenuItemID(1),!m_bIsWindowNormal);
//显示并追踪鼠标信息
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_COMMAND,pSubMenu->GetMenuItemID(0),0);
else
//否则,将窗体隐藏
::SendMessage(m_NotifyIconData.hWnd,WM_COMMAND,pSubMenu->GetMenuItemID(1),0);
}
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -