⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tray.cpp

📁 这是书上的代码
💻 CPP
字号:
// Tray.cpp: implementation of the CTray class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "TimeClose.h"
#include "Tray.h"
#include <afxpriv.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

//初始化 NOTIFYICONDATA
CTray::CTray(UINT uID)
{
	memset(&m_nid, 0 , sizeof(m_nid));
	m_nid.cbSize = sizeof(m_nid);
	m_nid.uID = uID;	// 图标的ID号
	AfxLoadString(uID, m_nid.szTip, sizeof(m_nid.szTip));

}

CTray::~CTray()
{
	SetIcon(0); // 从托盘中移走图标
}

BOOL CTray::SetIcon(UINT uID)
{
	HICON hicon=NULL;
	if (uID) {
		//设置鼠标移动信息
		AfxLoadString(uID, m_nid.szTip, sizeof(m_nid.szTip));
		hicon = AfxGetApp()->LoadIcon(uID);
	}
	return SetIcon(hicon, NULL);

}
//主要函数
BOOL CTray::SetIcon(HICON hicon, LPCSTR lpTip)
{
	UINT msg;
	m_nid.uFlags = 0;

	// 设置图标
	if (hicon) {
		// 增加或替换图标
		msg = m_nid.hIcon ? NIM_MODIFY : NIM_ADD;
		m_nid.hIcon = hicon;
		m_nid.uFlags |= NIF_ICON;
	} else { // 移去图标
		if (m_nid.hIcon==NULL)
			return TRUE;		
		msg = NIM_DELETE;
	}

	if (lpTip)
		strncpy(m_nid.szTip, lpTip, sizeof(m_nid.szTip));
	if (m_nid.szTip[0])
		m_nid.uFlags |= NIF_TIP;

	if (m_nid.uCallbackMessage && m_nid.hWnd)
		m_nid.uFlags |= NIF_MESSAGE;

	// 用于在托盘上增加、删除或修改函数
	BOOL bRet = Shell_NotifyIcon(msg, &m_nid);
	if (msg==NIM_DELETE || !bRet)
		m_nid.hIcon = NULL;	
	return bRet;

}
//设置接收托盘图标通知消息的窗口句柄
void CTray::SetNotificationWnd(CWnd *pNotifyWnd, UINT uCbMsg)
{
	ASSERT(pNotifyWnd==NULL || ::IsWindow(pNotifyWnd->GetSafeHwnd()));
	m_nid.hWnd = pNotifyWnd->GetSafeHwnd();

	ASSERT(uCbMsg==0 || uCbMsg>=WM_USER);
	m_nid.uCallbackMessage = uCbMsg;

}


LRESULT CTray::OnTrayNotification(WPARAM wID, LPARAM lEvent)
{
	if (wID!=m_nid.uID ||
		(lEvent!=WM_RBUTTONUP && lEvent!=WM_LBUTTONDBLCLK))
		return 0;
	CMenu menu;
	if (!menu.LoadMenu(m_nid.uID))
		return 0;
	CMenu* pSubMenu = menu.GetSubMenu(0);
	if (!pSubMenu) 
		return 0;

	if (lEvent==WM_RBUTTONUP) {//响应鼠标右键

		::SetMenuDefaultItem(pSubMenu->m_hMenu, 0, TRUE);

		CPoint mouse;
		GetCursorPos(&mouse);
		::SetForegroundWindow(m_nid.hWnd);	
		::TrackPopupMenu(pSubMenu->m_hMenu, 0, mouse.x, mouse.y, 0,
			m_nid.hWnd, NULL);
	} 
	return 1; 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -