這裡主要來實現這樣的功能,就是當點擊最小化時,把對話框隱藏掉,並且讓它在系統托盤小時小圖標,如圖:最左邊的應用程序圖標,就像QQ一樣,當右鍵這小圖標時,會彈出一個菜單,如圖:,點擊它即可以退出程序。 只是簡單實現這個功能,沒有考慮太多。
-,先來介紹NOTIFYICONDATA結構
typedef struct _NOTIFYICONDATA ...{
DWord cbSize; //以字節為單位這個結構的大小
HWND hWnd; //接收托盤圖表通知消息的窗口句柄
UINT uID; //應用程序定義的該圖表的ID號
UINT uFlags; // 設置該圖表的屬性
UINT uCallbackMessage ; // 應用程序定義的消息ID號,此消息傳遞給hWnd
HICON hIcon; // 圖表句柄
char szTip[64]; // 鼠標停留在該圖標上提示信息
} NOTIFYICONDATA, *PNOTIFYICONDATA;
二:Shell_NotifyIcon介紹
WINSHELLAPI BOOL WINAPI Shell_NotifyIcon( DWord dwMessage, PNOTIFYICONDATA pnid);
Sends a message to the system to add, modify, or delete an icon from the taskbar status area.
Returns nonzero if successful, or zero otherwise.
dwMessage
Message value to send. This parameter can be one of these values: NIM_ADD Adds an icon to the status area.
NIM_DELETE Deletes an icon from the status area.
NIM_MODIFY ModifIEs an icon in the status area.
pnid
Address of a NOTIFYICONDATA structure. The content of the structure depends on the value of dwMessage.
三:實現例子代碼:
首先自己要定義一個消息ID
#define WM_SYSTEMTRAY WM_USER+1;
在頭文件中定義一個向應該消息的函數如:
afx_msg LRESULT OnSystemTray(WPARAM wParam, LPARAM lParam);
添加消息響應宏:ON_MESSAGE(WM_SYSTEMTRAY, OnSystemTray)
響應該消息的函數可以如下:
LRESULT CSystemTrayDlg::OnSystemTray(WPARAM wParam, LPARAM lParam)
...{
// wParam = the nid.uID defined above
// (useful if you have more then one icon in tray)
// lParam = mouse message
if ( wParam = IDR_MAINFRAME )
...{
switch( lParam )
...{
case WM_LBUTTONDOWN:
this->ShowWindow(SW_NORMAL);
break;
case WM_RBUTTONDOWN:
CMenu menu;
menu.LoadMenu(IDR_MENU);
POINT pt;
::GetCursorPos(&pt);
menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTALIGN, pt.x, pt.y, AfxGetMainWnd());
break;
}
}
return 1;
}
假如建立的工程是居於DLG的
可以在對話框初始化函數中添加下面代碼
NOTIFYICONDATA nid;
nid.cbSize = sizeof( NOTIFYICONDATA );
nid.hWnd = m_hWnd; // handle of window that will receive
// messages from icon
nid.uID = IDR_MAINFRAME; // id for this icon
nid.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
// the next three parameters are valid
nid.uCallbackMessage = WM_SYSTEMTRAY;
// message that icon sends when clicked
nid.hIcon = AfxGetApp()->LoadIcon( IDR_MAINFRAME );
// icon
//strcpy( nid.szTip, "System Tray Tip" );
wcscpy(nid.szTip, L"System Tray Tip");
//StrCpyW(&nid.szTip,"System Tray Tip");
// bubble help message for icon
::Shell_NotifyIcon( NIM_ADD,&nid );
響應最大最小消息,可以通過WM_SYSCOMMAND
void CSystemTrayDlg::OnSysCommand(UINT nID, LPARAM lParam)
...{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
...{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else if(nID == SC_MAXIMIZE) //最大化
...{
this->ShowWindow(SW_SHOWMAXIMIZED);
}
else if(nID == SC_MINIMIZE) //最小化,把他隱藏起來
...{
this->ShowWindow(FALSE);
}
else
...{
CDialog::OnSysCommand(nID, lParam);
}
}
在資源欄添加一個菜單資源,假如資源號為IDR_MENU
並且添加響應此菜單的函數
afx_msg void OnExit();
ON_COMMAND(ID_EXIT32774, OnExit)
void CSystemTrayDlg::OnExit()
...{
CDialog::OnCancel();
}
因為在退出程序的時候需要把小圖標刪除掉,所以在添加如下代碼:
void CSystemTrayDlg::OnDestroy()
...{
NOTIFYICONDATA nid;
nid.cbSize = sizeof( NOTIFYICONDATA );
nid.hWnd = m_hWnd;
nid.uID = IDR_MAINFRAME;
nid.uFlags = 0;
::Shell_NotifyIcon( NIM_DELETE,&nid );
CDialog::OnDestroy();
}