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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章