这里主要来实现这样的功能,就是当点击最小化时,把对话框隐藏掉,并且让它在系统托盘小时小图标,如图:最左边的应用程序图标,就像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
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)#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();
}