1 在类中添加一个NOTIFYICONDATA类型的成员变量m_nid
protected:
HICON m_hIcon;
NOTIFYICONDATA m_nid;
2 在类的初始化函数OnInitDialog中对结构体NOTIFYICONDATA进行初始化赋值
ZeroMemory(&m_nid, sizeof(m_nid));
m_nid.cbSize = sizeof(m_nid);
m_nid.hWnd = this->m_hWnd;
m_nid.uID = IDR_MAINFRAME;
m_nid.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP;
m_nid.hIcon=LoadIcon(AfxGetInstanceHandle(),
MAKEINTRESOURCEA(IDR_MAINFRAME));
//WM_NOTIFYICON为点击托盘icon时产生的消息
m_nid.uCallbackMessage = WM_NOTIFYICON;
strcpy(m_nid.szTip, "hahaya");
3 定义WM_NOTIFYICON的处理过程
#define WM_NOTIFYICON WM_USER + 100
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnNotifyIcon(WPARAM wParam, LPARAM lParam);
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_MESSAGE(WM_NOTIFYICON, OnNotifyIcon)
void CMyDlg::OnNotifyIcon(WPARAM wParam, LPARAM lParam) { POINT pt; GetCursorPos(&pt); switch(lParam) { case WM_LBUTTONDBLCLK: ShowWindow(SW_SHOW); ShowWindow(SW_SHOWNORMAL); Shell_NotifyIcon(NIM_DELETE, &m_nid); break;
} }
4 捕获发生在窗口上的窗口上大小发生变化的WM_SIZE消息(可以通过类向导给对话框添加WM_SIZE消息)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnNotifyIcon(WPARAM wParam, LPARAM lParam);
afx_msg void OnSize(UINT nType, int cx, int cy);
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_MESSAGE(WM_NOTIFYICON, OnNotifyIcon)
ON_WM_SIZE()
void CMyDlg::OnSize(UINT nType, int cx, int cy) { CDialog::OnSize(nType, cx, cy); // TODO: Add your message handler code here if(nType == SIZE_MINIMIZED) { Shell_NotifyIcon(NIM_ADD, &m_nid); ShowWindow(SW_HIDE); } }