[一篇VC小文章]VC++制作一個(gè)最小化最小托盤的8步驟(Minimize your app to systray in 8 easy steps)翻譯完
By Yasar Arslan. From codeproject
這篇文章內(nèi)容比較基礎(chǔ),最近看到覺得有用,順便翻譯一下
有空可以寫一個(gè)自己的TrayIcon類,化簡這些原始的操作。
Introduction
這篇文章解析了 Shell_NotifyIcon
這個(gè)函數(shù)用法--用來建立你自己的應(yīng)用程序的系統(tǒng)托盤圖標(biāo).
這篇文章給了基本的縮小到托盤的操作過程并讓你從中了解.
這篇文章提供8個(gè)簡單的步驟讓你成功的實(shí)現(xiàn)在你的程序中建立系統(tǒng)托盤圖標(biāo).
源代碼提供了一個(gè)基于對(duì)話框的演示程序.
Tray Icons
為了用托盤圖標(biāo)你需要用一個(gè)shell函數(shù) :)
BOOL Shell_NotifyIcon( DWORD dwMessage, PNOTIFYICONDATA pnid );
The dwMessage
?可選的參數(shù)包括 the NIM_ADD,NIM_DELETE and NIM_MODIFY
功能分別是添加刪除以及修改圖標(biāo)于系統(tǒng)圖標(biāo).
PNOTIFYICONDATA
結(jié)構(gòu)包括這些系統(tǒng)需要處理的任務(wù)圖標(biāo)狀態(tài)區(qū)域消息等信息.
typedef??struct _NOTIFYICONDATA {
??? DWORD cbSize;
??? HWND hWnd;
??? UINT uID;
??? UINT uFlags;
??? UINT uCallbackMessage;
??? HICON hIcon;
??? #if (_WIN32_IE < 0x0500)
??????? TCHAR szTip[64];
??? #else
??????? TCHAR szTip[128];
??? #endif??? #if (_WIN32_IE >= 0x0500)
??????? DWORD dwState;
??????? DWORD dwStateMask;
??????? TCHAR szInfo[256];
??????? union {
??????????? UINT? uTimeout;
??????????? UINT? uVersion;
??????? } DUMMYUNIONNAME;
??????? TCHAR szInfoTitle[64];
??????? DWORD dwInfoFlags;
??? #endif??? #if (_WIN32_IE >= 0x600)
??????? GUID guidItem;
??? #endif
} NOTIFYICONDATA, *PNOTIFYICONDATA;
*Note:?更完全的信息可以去參考MSDN
Creating the Application
Create a new VC++ dialog based project and call it TrayMin.
創(chuàng)建一個(gè)名叫TrayMin的基于對(duì)話框的VC++工程
Step: 1
自定義消息于 TrayMinDlg.h 頭文件.
#define WM_TRAY_MESSAGE (WM_USER + 1)
The WM_USER
常量用來幫助用戶定義自己的消息被用來建立個(gè)人的窗口類, 定義時(shí)通常用這種格式 WM_USER+X
,?這里 X 是一個(gè)整形變量.
*更詳細(xì)的看MSDN
Step: 2
現(xiàn)在在Now add the DECLARE_MESSAGE_MAP() 之前添加下面的用戶函數(shù)吧( TrayMinDlg.h file)?? afx_msg void OnTrayNotify(WPARAM wParam, LPARAM lParam);
?
當(dāng)添加一個(gè)圖標(biāo)到托盤時(shí)這有一個(gè)圖標(biāo)的回調(diào)消息,注意到 NOTIFYICONDATA
結(jié)構(gòu)中有uCallbackMessage成員是回調(diào)消息識(shí)別的關(guān)鍵,它會(huì)被傳給NIM_ADD(我們之后將會(huì)見到更詳細(xì)的)。當(dāng)添加托盤圖標(biāo)這個(gè)事件發(fā)生時(shí),系統(tǒng)發(fā)送一個(gè)回調(diào)函數(shù)到由hWnd成員對(duì)象指定的窗口過程(winproc),wParam 參數(shù)可以用來被識(shí)別究竟發(fā)生了什么操作。lParam參數(shù)存放發(fā)生事件相關(guān)的鼠標(biāo)或者鍵盤消息。舉個(gè)例子,當(dāng)一個(gè)鼠標(biāo)指針指向一個(gè)托盤圖標(biāo),lParam將包括WM_MOUSEMOVE
Step: 3
現(xiàn)在添加下面的這行在消息宏中(MessageMap)在TrayMinDlg.cpp
ON_MESSAGE(WM_TRAY_MESSAGE,OnTrayNotify)
?
現(xiàn)在應(yīng)該是這樣的.
BEGIN_MESSAGE_MAP(CTrayMinDlg, CDialog) //{{AFX_MSG_MAP(CTrayMinDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_MESSAGE(WM_TRAY_MESSAGE ,OnTrayNotify) //}}AFX_MSG_MAP END_MESSAGE_MAP()
Step: 4
現(xiàn)在在TrayMinDlg.cpp 定義OnTrayNotify函數(shù),不要忘記在函數(shù)頭部添加afx_msg。
afx_msg void CTrayMinDlg::OnTrayNotify(WPARAM wParam, LPARAM lParam) { UINT uID; UINT uMsg; uID = (UINT) wParam; uMsg = (UINT) lParam; if (uID != 1) return; CPoint pt; switch (uMsg ) { case WM_LBUTTONDOWN: GetCursorPos(&pt); ClientToScreen(&pt); OnTrayLButtonDown(pt); break; case WM_RBUTTONDOWN: case WM_CONTEXTMENU: GetCursorPos(&pt); OnTrayRButtonDown(pt); break; } return; }
Step: 5
現(xiàn)在在TrayMinDlg類添加兩個(gè)成員函數(shù)來相應(yīng)鼠標(biāo)事件。
實(shí)現(xiàn)鼠標(biāo)左鍵單擊的相應(yīng)
-
函數(shù)類型:void
? -
函數(shù)聲明:
?OnTrayLButtonDown(CPoint pt)
實(shí)現(xiàn)鼠標(biāo)右鍵單擊的相應(yīng)
- 函數(shù)類型:
void
- 函數(shù)聲明:
OnTrayRButtonDown(CPoint pt)
OnTrayLButtonDown(CPoint pt)的定義如下
.
void CTrayMinDlg::OnTrayLButtonDown(CPoint pt) { MessageBox("You have clicked Left mouse Button "); }
The Declaration of OnTrayRButtonDown(CPoint pt)
is as following.
void CTrayMinDlg::OnTrayRButtonDown(CPoint pt) { //m_menu is the member of CTrayMinDlg as CMenu m_menu; m_menu.GetSubMenu(0)->TrackPopupMenu(TPM_BOTTOMALIGN| TPM_LEFTBUTTON|TPM_RIGHTBUTTON,pt.x,pt.y,this); }
Step: 6
Add two member variable to the CTrayMinDlg
.
為CTrayMinDlg添加兩個(gè)成員變量
- Variable Type:
NOTIFYICONDATA
- Variable Name:
m_TrayData
; - Variable Type:
CMenu
- Variable Name:
m_menu
;
現(xiàn)在添加菜單資源
Step: 7
現(xiàn)在畫一個(gè)最小化的按鈕在對(duì)話框設(shè)計(jì)中并且添加這個(gè)按鈕的執(zhí)行函數(shù)
void CShellDlg::OnMinimize() { m_TrayData.cbSize = sizeof(NOTIFYICONDATA); //Size of this structure, in bytes. m_TrayData.hWnd = this->m_hWnd; //Handle to the window that receives notification //messages associated with an icon in the taskbar //status area. The Shell uses hWnd and uID to //identify which icon to operate on when //Shell_NotifyIcon is invoked. m_TrayData.uID = 1; //Application-defined identifier of the taskbar icon.//The Shell uses hWnd and uID to identify which icon //to operate on when Shell_NotifyIcon is invoked. You// can have multiple icons associated with a single //hWnd by assigning each a different uID. m_TrayData.uCallbackMessage = WM_TRAY_MESSAGE; //Application-defined message identifier. The system //uses this identifier to send notifications to the //window identified in hWnd. These notifications are //sent when a mouse event occurs in the bounding //rectangle of the icon, or when the icon is selected //or activated with the keyboard. The wParam parameter //of the message contains the identifier of the taskbar //icon in which the event occurred. The lParam parameter //holds the mouse or keyboard message associated with the// event. For example, when the pointer moves over a //taskbar icon, lParam is set to WM_MOUSEMOVE. m_TrayData.hIcon = this->m_hIcon; //Handle to the icon to be added, modified, or deleted strcpy(m_TrayData.szTip,"My Icon"); //Pointer to a null-terminated string with the text //for a standard ToolTip. It can have a maximum of 64 //characters including the terminating NULL. m_TrayData.uFlags = NIF_ICON|NIF_MESSAGE; //Flags that indicate which of the other members contain valid data. BOOL bSuccess = FALSE; BOOL BSus = FALSE; BSus = m_menu.LoadMenu(IDR_MENU1); if(!(BSus)) MessageBox("Unabled to Loa menu"); bSuccess = Shell_NotifyIcon(NIM_ADD,&m_TrayData); if(!(bSuccess)) MessageBox("Unable to Set Tary Icon"); else { this->ShowWindow(SW_MINIMIZE); this->ShowWindow(SW_HIDE); } }
Step: 8
在退出菜單的執(zhí)行函數(shù)寫下如下
Shell_NotifyIcon(NIM_DELETE,&m_TrayData);
? DestroyWindow();
現(xiàn)在可以運(yùn)行程序,并且嘗試最小化按鈕的使用(他會(huì)最小化導(dǎo)系統(tǒng)托盤)。
現(xiàn)在盡情發(fā)揮,完善這些步驟,完成自己的系統(tǒng)托盤圖標(biāo)吧!
Yasar Arslan
posted on 2007-03-06 19:06 Gohan 閱讀(1880) 評(píng)論(0) 編輯 收藏 引用 所屬分類: C++