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

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;
}


阅读更多
文章标签: user
个人分类: mfc Windows编程
上一篇如何让窗口重画——Invalidate()、SendMessage(WM_PAINT)和UpdateWindow()
下一篇托盘图标的删除和托盘程序的优化
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭