//设置系统托盘
1.定义成员变量:
NOTIFYICONDATA NotifyIcon;//系统托盘类
2.创建系统托盘
NotifyIcon.cbSize = sizeof(NOTIFYICONDATA);
NotifyIcon.hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
NotifyIcon.hWnd = m_hWnd;
NotifyIcon.uID = IDR_MAINFRAME;
lstrcpy(NotifyIcon.szTip, _T("大屏幕控制器软件"));
NotifyIcon.uCallbackMessage = WM_SYSTEMTRAY;
NotifyIcon.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
Shell_NotifyIcon(NIM_ADD, &NotifyIcon); //添加系统托盘
3.定义全局自定义消息,用于弹出右键菜单
#define WM_SYSTEMTRAY WM_USER+100 //自定义消息,用于弹出系统托盘
4.创建消息函数
ON_MESSAGE(WM_SYSTEMTRAY, &CViewControlDlg::OnSystemTray)
5.菜单退出
//右键弹出菜单
afx_msg LRESULT CViewControlDlg::OnSystemTray(WPARAM wParam, LPARAM lParam)
{
switch(lParam)
{
case WM_RBUTTONDOWN:
{
//右击弹出托盘菜单
CMenu menu;
CPoint pos(GetMessagePos());
menu.CreatePopupMenu();
menu.AppendMenu(MF_STRING,10001,_T("退出"));
menu.TrackPopupMenu(TPM_LEFTALIGN| TPM_BOTTOMALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON,pos.x,pos.y,this);
}
}
return 0;
}
//响应点击菜单中退出的消息
BOOL CViewControlDlg::OnCommand(WPARAM wParam, LPARAM lParam)
{
switch (wParam)//判断是选择的哪一项
{
case 10001://添加
{
::SendMessage(this->m_hWnd, WM_CLOSE, 0, 0);
}
}
return CDialogEx::OnCommand(wParam, lParam);
}
6.//消除托盘
Shell_NotifyIcon(NIM_DELETE, &NotifyIcon);//消除托盘图标