最小化托盘程序

trayDlg.h:

afx_msg LRESULT onShowTask(WPARAM wParam,LPARAM lParam);
void Tdehide();
void dehide();


trayDlg.cpp:

#define WM_SHOWTASK WM_USER


BEGIN_MESSAGE_MAP(CtrayDlg, CDialogEx)
ON_MESSAGE(WM_SHOWTASK,onShowTask)
END_MESSAGE_MAP()


void CtrayDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
dehide();
ShowWindow(SW_HIDE);


}

LRESULT CtrayDlg::onShowTask(WPARAM wParam,LPARAM lParam)
{
if (wParam!=IDR_MAINFRAME)
{
return 1;
}

switch(lParam)
{
case WM_RBUTTONUP:
{
LPPOINT lpoint=new tagPOINT;
::GetCursorPos(lpoint);
CMenu menu;
menu.CreatePopupMenu();
menu.AppendMenu(MF_STRING,WM_DESTROY,_T("关闭"));
menu.TrackPopupMenu(TPM_LEFTALIGN,lpoint->x,lpoint->y,this);
HMENU hmenu=menu.Detach();
menu.DestroyMenu();
delete lpoint;
}
break;
case WM_LBUTTONDBLCLK:
{
this->ShowWindow(SW_SHOW);
Tdehide();
}
break;
}
return 0;
}

void CtrayDlg::dehide()
{
NOTIFYICONDATA tnd;
tnd.cbSize=sizeof(NOTIFYICONDATA);
tnd.hWnd=this->m_hWnd;
tnd.uID=IDR_MAINFRAME;
tnd.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
tnd.uCallbackMessage=WM_SHOWTASK;
//用户自定义的消息,即鼠标在任务栏上程序图标上动作时图标发送的消息
tnd.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
strcpy(tnd.szTip,"测试程序");//图标提示为"测试程序"
Shell_NotifyIcon(NIM_ADD,&tnd);//向任务栏添图标
}

void CtrayDlg::Tdehide()
{
NOTIFYICONDATA tnd;
tnd.cbSize=sizeof(NOTIFYICONDATA);
tnd.hWnd=this->m_hWnd;
tnd.uID=IDR_MAINFRAME;
tnd.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
tnd.uCallbackMessage=WM_SHOWTASK;
//用户自定义的消息,即鼠标在任务栏上程序图标上动作时图标发送的消息
tnd.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
strcpy(tnd.szTip,"测试程序");//图标提示为"测试程序"
Shell_NotifyIcon(NIM_DELETE,&tnd);//向任务栏删图标
}



你可能感兴趣的:(程序)