?? trayicon.cpp
字號:
// TrayIcon.cpp : implementation file
//
#include "stdafx.h"
#include "TrayIcon.h"
#include "GfxPopupMenu.h"
#include "resource.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTrayIcon
IMPLEMENT_DYNCREATE(CTrayIcon, CCmdTarget)
CTrayIcon::CTrayIcon()
{
//初始化NOTIFYICONDATA結構變量
memset(&m_nid, 0 , sizeof(m_nid));
m_nid.cbSize = sizeof(m_nid);
}
CTrayIcon::~CTrayIcon()
{
SetIcon(0,NULL); // 從系統托盤中刪除圖標
}
// 設定通知窗口,該窗口必須已被創建
void CTrayIcon::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;
}
BOOL CTrayIcon::SetIcon(UINT uID,LPCSTR lpTip)
{
HICON hicon=NULL;
if (uID)
{
if(lpTip!=NULL)
strcpy(m_nid.szTip,lpTip);
hicon = AfxGetApp()->LoadIcon(uID);
}
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|NIF_MESSAGE;
if(lpTip!=NULL)
m_nid.uFlags|=NIF_TIP;
}
else
{ // 刪除圖標
if (m_nid.hIcon==NULL)
return TRUE; //已被刪除
msg = NIM_DELETE;
}
BOOL bRet = Shell_NotifyIcon(msg, &m_nid);
if (msg==NIM_DELETE || !bRet)
m_nid.hIcon = NULL;
return bRet;
}
// 缺省事件處理程序,該程序處理鼠標右擊及雙擊事件。
LRESULT CTrayIcon::OnTrayNotification(WPARAM wID,LPARAM lEvent)
{
if (wID!=m_nid.uID ||(lEvent!=WM_RBUTTONUP && lEvent!=WM_LBUTTONDBLCLK && lEvent!=WM_LBUTTONDOWN))
return 0;
CMenu menu;
CGfxPopupMenu cMenu;
if(!menu.LoadMenu(IDR_MAINFRAME))
return 0;
if (lEvent==WM_RBUTTONUP) {
cMenu.Attach(menu.GetSubMenu(0)->GetSafeHmenu());
cMenu.LoadToolBarResource(IDR_TOOLBAR1);
cMenu.RemapMenu(&cMenu);
// 在鼠標的當前位置彈出菜單。
CPoint mouse;
GetCursorPos(&mouse);
::SetForegroundWindow(m_nid.hWnd);
::TrackPopupMenu(cMenu.m_hMenu,
0,
mouse.x,
mouse.y,
0,
m_nid.hWnd,
NULL);
}
else if(lEvent==WM_LBUTTONDOWN)
{
cMenu.Attach(menu.GetSubMenu(1)->GetSafeHmenu());
cMenu.LoadToolBarResource(IDR_TOOLBAR2);
cMenu.RemapMenu(&cMenu);
CPoint mouse;
GetCursorPos(&mouse);
::SetForegroundWindow(m_nid.hWnd);
::TrackPopupMenu(cMenu.m_hMenu,
0,
mouse.x,
mouse.y,
0,
m_nid.hWnd,
NULL);
}
else
{
::SendMessage(m_nid.hWnd, WM_HOTKEY,1001, 0);
}
return 1; // 表示事件已被處理
}
/////////////////////////////////////////////////////////////////////////////
// CTrayIcon message handlers
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -