1、对话框 STYLE 中加入 WS_MINIMIZEBOX
STYLE DS_SETFONT | DS_MODALFRAME | WS_MINIMIZEBOX | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
2、定义最小化和托盘双击事件
#define WM_SHOWTASK (WM_USER + 120)
BEGIN_MESSAGE_MAP(CDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_MESSAGE(WM_SHOWTASK, &CDlg::OnShowTask)
END_MESSAGE_MAP()
3、最小化点击
void CDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if(nID==SC_MINIMIZE)
{
ToTray(); //最小化到托盘的函数
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
void CDlg::ToTray()
{
CString strTip;
strTip.Format(_T(“XXXX”));
NOTIFYICONDATA nid;
nid.cbSize=(DWORD)sizeof(NOTIFYICONDATA);
nid.hWnd=this->m_hWnd;
nid.uID=IDR_MAINFRAME;
nid.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP ;
nid.uCallbackMessage=WM_SHOWTASK;
//自定义的消息名称
nid.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
wcscpy_s (nid.szTip,_countof(nid.szTip),strTip);
//信息提示条
Shell_NotifyIcon(NIM_ADD,&nid);
//在托盘区添加图标
ShowWindow(SW_HIDE); //隐藏主窗口
}
4、托盘显示和双击复原
LRESULT CDlg::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();//声明一个弹出式菜单
//增加菜单项“关闭”,点击则发送消息WM_DESTROY给主窗口,将程序结束。
menu.AppendMenu(MF_STRING,WM_DESTROY,_T(“Close”));
//确定弹出式菜单的位置
menu.TrackPopupMenu(TPM_LEFTALIGN,lpoint->x,lpoint->y,this);
//资源回收
menu.Detach();
menu.DestroyMenu();
delete lpoint;
}
break;
case WM_LBUTTONDBLCLK://双击左键的处理
{
this->ShowWindow(SW_SHOW);
//简单的显示主窗口完事儿
DeleteTray();
}
break;
default:
break;
}
return 0;
}
void CDlg::DeleteTray()
{
NOTIFYICONDATA nid;
nid.cbSize=(DWORD)sizeof(NOTIFYICONDATA);
nid.hWnd=this->m_hWnd;
nid.uID=IDR_MAINFRAME;
nid.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP ;
nid.uCallbackMessage=WM_SHOWTASK;
//自定义的消息名称
nid.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
//信息提示条为“计划任务提醒”
Shell_NotifyIcon(NIM_DELETE,&nid);
//在托盘区删除图标
}