托盘图标的添加和消息响应

1.如何添加一个托盘图标

#define WM_ICON_NOTIFY        WM_USER + 2
...
BOOL CMyDlg::OnInitDialog()
{
	...
	NOTIFYICONDATA nid;

	nid.cbSize = sizeof nid;
	nid.hWnd = GetSafeHwnd();
	nid.uID = IDR_MAINFRAME;
	nid.hIcon = m_hIcon;
	nid.uCallbackMessage = WM_ICON_NOTIFY;
	nid.uFlags = NIF_MESSAGE | NIF_ICON;

	Shell_NotifyIcon(NIM_ADD, &nid);
    ...
    

2.如何给托盘图标添加消息响应

BEGIN_MESSAGE_MAP(CMyDlg, CDHtmlDialog)
...
	ON_MESSAGE(WM_ICON_NOTIFY, &CMyDlg::OnIconNotify)
END_MESSAGE_MAP()


afx_msg LRESULT CMyDlg::OnIconNotify(WPARAM wParam, LPARAM lParam)
{
	switch(lParam)
	{
	case WM_LBUTTONUP:
    case WM_RBUTTONUP:
		SendMessage(WM_SYSCOMMAND, SC_RESTORE);
		SetForegroundWindow();
        break;
	default:
		break;
	}
    return 0;
}


你可能感兴趣的:(托盘图标的添加和消息响应)