#define WM_TRAYICON_MSG (WM_USER+100)
public:
afx_msg void OnBnClickedButTuo();
BOOL TrayMyIcon(BOOL bAdd=TRUE);
LRESULT OnTrayCallBackMsg(WPARAM wparam, LPARAM lparam);
添加消息映射
ON_MESSAGE(WM_TRAYICON_MSG, OnTrayCallBackMsg)
//按钮事件,实现托盘
void CWuMaDlg::OnBnClickedButTuo()
{
// TODO: 在此添加控件通知处理程序代码
TrayMyIcon();
}
//托盘实现
BOOL CWuMaDlg::TrayMyIcon(BOOL bAdd)
{
BOOL bRet = FALSE;
NOTIFYICONDATA tnd;
tnd.cbSize = sizeof(NOTIFYICONDATA);
tnd.hWnd = m_hWnd;
tnd.uID = IDR_MAINFRAME;
if (bAdd == TRUE) {
tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
tnd.uCallbackMessage = WM_TRAYICON_MSG;
tnd.hIcon = LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME));
_tcscpy_s(tnd.szTip, sizeof(tnd.szTip), _T("無聊辅助v1.0"));
ShowWindow(SW_MINIMIZE);
ShowWindow(SW_HIDE);
bRet = Shell_NotifyIcon(NIM_ADD, &tnd);
}
else{
ShowWindow(SW_SHOWNA);
SetForegroundWindow();
bRet = Shell_NotifyIcon(NIM_DELETE, &tnd);
}
return bRet;
}
//消息响应
LRESULT CWuMaDlg::OnTrayCallBackMsg(WPARAM wparam, LPARAM lparam)
{
switch (lparam)
{
case WM_RBUTTONUP:
{
CMenu mMenu, *pMenu = NULL;
CPoint pt;
mMenu.LoadMenu(IDR_MENU1);
pMenu = mMenu.GetSubMenu(0);
GetCursorPos(&pt);
SetForegroundWindow();
pMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt.x, pt.y, this);
break;
}
case WM_LBUTTONDBLCLK:
ShowWindow(SW_RESTORE);
SetForegroundWindow();
TrayMyIcon(FALSE);
break;
default:break;
}
return NULL;
}
//右键显示界面
void CWuMaDlg::OnMenuShow()
{
ShowWindow(SW_RESTORE);
TrayMyIcon(FALSE);
}
//退出
void CWuMaDlg::OnMenuExit()
{
TrayMyIcon(FALSE);
OnCancel();
}