MFC 托盘图标 NOTIFYICONDATA 封装class

说明: 我只是把原本的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 once

class 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();
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值