📄 trayicon.cpp
字号:
// TrayIcon.cpp: implementation of the CTrayIcon class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "BranchManager.h"
#include "TrayIcon.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CTrayIcon::CTrayIcon()
{
memset(&m_pNotifyIconData,0,sizeof(m_pNotifyIconData));
}
CTrayIcon::CTrayIcon(CWnd* pWnd,UINT uCallbackMessage,LPCTSTR szTip,HICON icon,UINT uID)
{
Create( pWnd, uCallbackMessage, szTip, icon, uID);
}
CTrayIcon::~CTrayIcon()
{
RemoveIcon();
}
BOOL CTrayIcon::Create(CWnd *pWnd, UINT uCallbackMessage, LPCTSTR szTip, HICON icon, UINT uID)
{
ASSERT(_tcslen(szTip)<64);
ASSERT(uCallbackMessage>WM_USER);
m_pNotifyIconData.cbSize=sizeof(NOTIFYICONDATA);
m_pNotifyIconData.hWnd=pWnd->GetSafeHwnd();
strcpy(m_pNotifyIconData.szTip,szTip);
m_pNotifyIconData.hIcon=icon;
m_pNotifyIconData.uCallbackMessage=uCallbackMessage;
m_pNotifyIconData.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
if(Shell_NotifyIcon(NIM_ADD,&m_pNotifyIconData))
return TRUE;
else
return FALSE;
}
BOOL CTrayIcon::SetIcon(HICON hicon)
{
m_pNotifyIconData.hIcon=hicon;
return Shell_NotifyIcon(NIM_ADD,&m_pNotifyIconData);
}
BOOL CTrayIcon::SetIcon(LPCTSTR lpszIconName)
{
HICON hicon=AfxGetApp()->LoadIcon(lpszIconName);
return SetIcon(hicon);
}
BOOL CTrayIcon::SetIcon(UINT nIDResource)
{
HICON hicon=AfxGetApp()->LoadIcon(nIDResource);
return SetIcon(hicon);
}
BOOL CTrayIcon::SetTipText(LPCTSTR pszTip)
{
strcpy(m_pNotifyIconData.szTip,pszTip);
return Shell_NotifyIcon(NIM_ADD,&m_pNotifyIconData);
}
HICON CTrayIcon::GetIcon()
{
return m_pNotifyIconData.hIcon;
}
CString CTrayIcon::GetTipText()
{
return CString(m_pNotifyIconData.szTip);
}
BOOL CTrayIcon::RemoveIcon()
{
m_pNotifyIconData.uFlags=0;
return Shell_NotifyIcon(NIM_DELETE,&m_pNotifyIconData);
}
BOOL CTrayIcon::SetNotificationWnd(CWnd *pWnd)
{
m_pNotifyIconData.hWnd=pWnd->GetSafeHwnd();
return Shell_NotifyIcon(NIM_ADD,&m_pNotifyIconData);
}
CWnd* CTrayIcon::GetNotificationWnd() const
{
return CWnd::FromHandle(m_pNotifyIconData.hWnd);
}
LRESULT CTrayIcon::OnTrayNotification(WPARAM wParam, LPARAM lParam)
{
if(wParam!=m_pNotifyIconData.uID)
return 0L;
CMenu menu,*pSubMenu;
if(LOWORD(lParam)==WM_RBUTTONUP)
{
if(!menu.LoadMenu(IDR_MENU2)) return 0;
if(!(pSubMenu=menu.GetSubMenu(0))) return 0;
::SetMenuDefaultItem(pSubMenu->m_hMenu,0,TRUE);
CPoint pos;
GetCursorPos(&pos);
::SetForegroundWindow(m_pNotifyIconData.hWnd);
::TrackPopupMenu(pSubMenu->m_hMenu,0,pos.x,pos.y,0,
m_pNotifyIconData.hWnd,NULL);
menu.DestroyMenu();
}
else if(LOWORD(lParam==WM_LBUTTONDBLCLK))
{
if(!menu.LoadMenu(IDR_MENU2)) return 0;
if(!(pSubMenu=menu.GetSubMenu(0))) return 0;
::SetMenuDefaultItem(pSubMenu->m_hMenu,0,TRUE);
::SendMessage(m_pNotifyIconData.hWnd,WM_COMMAND,
pSubMenu->GetMenuItemID(0),0);
menu.DestroyMenu();
}
return 1;
}
BOOL CTrayIcon::ModifyIcon(HICON hIcon)
{
m_pNotifyIconData.hIcon=hIcon;
return Shell_NotifyIcon(NIM_MODIFY,&m_pNotifyIconData);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -