说明: 我只是把原本的NOTIFYICONDATA结构体创建方式进行一次封装,还有大量可扩展空间 诸位自行研究 我这里只是一个简易的框架
CMainFrame头文件中需要添加的对象
//托盘功能
#define WM_SYSTEMTRAY WM_USER + 0x0010
#include "NotifyIcon.h"
class CMainFrame
{
//...CNotifyIcon NotifyIcon;afx_msg LRESULT OnSystemTray(WPARAM wParam, LPARAM lParam);
//...}
创建方式
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{//.....//设置系统托盘HICON hicon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);NotifyIcon.InitNotify(this, hicon, WM_SYSTEMTRAY);return 0;
}
当然了还有要添加消息
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWndEx)
//....ON_MESSAGE(WM_SYSTEMTRAY, &CMainFrame::OnSystemTray)
//.....
END_MESSAGE_MAP()afx_msg LRESULT CMainFrame::OnSystemTray(WPARAM wParam, LPARAM lParam)
{//wParam接收的是图标的ID,而lParam接收的是鼠标的行为 NotifyIcon.OnSystemTray((UINT)wParam, lParam);return 0;
}
头文件定义
#pragma onceclass CNotifyIcon :public NOTIFYICONDATA
{
public:CNotifyIcon();virtual ~CNotifyIcon();
protected:CWnd* pWndParent;
public:void InitNotify(CWnd* pWnd,HICON hicon, UINT MsgID);LRESULT OnSystemTray(UINT wParam, LPARAM lParam);
protected:// 右键单击消息virtual void OnRButtonDown();// 左键单击virtual void OnLButtonDown();// 左键双击virtual void OnLButtonDblClk();
};
源码文件定义
#include "pch.h"
#include <shellapi.h>
#include "NotifyIcon.h"
#include "resource.h"
CNotifyIcon::CNotifyIcon()
{cbSize = sizeof(NOTIFYICONDATA);hIcon = NULL;hWnd = NULL;uID = 0;szTip[0] = 0;uCallbackMessage = NULL;uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_INFO;dwState = NIS_SHAREDICON;szInfo[0] = 0;uTimeout = 1000;uVersion = NOTIFYICON_VERSION_4;szInfoTitle[0] = 0;dwInfoFlags = NIIF_INFO;}CNotifyIcon::~CNotifyIcon()
{Shell_NotifyIcon(NIM_DELETE, this);//消除托盘图标
}void CNotifyIcon::InitNotify(CWnd* pWnd, HICON hicon, UINT MsgID)
{pWndParent = pWnd;cbSize = sizeof(NOTIFYICONDATA);hIcon = hicon;hWnd = pWndParent->GetSafeHwnd();uID = 0;lstrcpy(szTip, _T("Demo软件"));uCallbackMessage = MsgID;uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_INFO;dwState = NIS_SHAREDICON;CTime tm = CTime::GetCurrentTime();CString timestr = tm.Format("%Y-%m-%d %H:%M:%S");lstrcpy(szInfo, timestr);uTimeout = 1000;uVersion = NOTIFYICON_VERSION_4;lstrcpy(szInfoTitle, L"软件已启动");dwInfoFlags = NIIF_INFO;Shell_NotifyIcon(NIM_ADD, this); //添加系统托盘
}
LRESULT CNotifyIcon::OnSystemTray(UINT ID, LPARAM WM_MOUSE)
{//wParam接收的是图标的ID,而lParam接收的是鼠标的行为 switch (WM_MOUSE){case WM_RBUTTONDOWN://右键起来时弹出快捷菜单OnRButtonDown();break;case WM_LBUTTONDOWN://左键单击的处理 OnLButtonDown();break;case WM_LBUTTONDBLCLK:OnLButtonDblClk();break;}return 0;
}// 右键单击消息
void CNotifyIcon::OnRButtonDown()
{CMenu menuexit;menuexit.LoadMenu(IDR_Notify);//这里的IDR_Notify 是菜单栏 用户自定义的CMenu *pPopup;pPopup = menuexit.GetSubMenu(0);CPoint point;GetCursorPos(&point);pWndParent->SetForegroundWindow();//显示右键菜单,由视类窗口拥有。pPopup->TrackPopupMenu(TPM_LEFTALIGN, point.x, point.y, pWndParent);
}// 左键单击
void CNotifyIcon::OnLButtonDown()
{if (pWndParent->IsWindowVisible()){//pWndParent->ModifyStyleEx(0, WS_EX_TOPMOST); //可以改变窗口的显示风格pWndParent->BringWindowToTop();}
}// 左键双击
void CNotifyIcon::OnLButtonDblClk()
{if (pWndParent->IsWindowVisible()){pWndParent->ShowWindow(SW_HIDE);}else{pWndParent->ShowWindow(SW_RESTORE);pWndParent->BringWindowToTop();}
}