MFC以任务栏图标启动需要在任务栏添加图标,隐藏主窗口,退出时关闭图表
一、在任务栏添加图标
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_MYSTART;
tnd.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
strcpy_s(tnd.szTip, "提示 "
Shell_NotifyIcon(NIM_ADD,&tnd);
二、处理图标消息
LRESULT CspriteDlg::OnStart(WPARAM wParam, LPARAM lParam)
{
UINT uID;
UINT uMouseMsg;
POINT pt;
uID=(UINT)wParam;
uMouseMsg=(UINT)lParam;
if(uMouseMsg==WM_RBUTTONDOWN)
{
switch(uID)
{
case IDR_MAINFRAME:
GetCursorPos(&pt);
int id = menuTray.GetSubMenu(0)->TrackPopupMenu(TPM_RETURNCMD | TPM_LEFTALIGN|TPM_RIGHTBUTTON, pt.x, pt.y, this);
switch(id)
{
case(ID_SYSTEM_OPENMSG):
OnSystemOpenmsg();
break;
case(ID_SYSTEM_EXIT):
OnSystemExit();
break;
}
break;
}
}
return 0;
}
三、关闭图标
NOTIFYICONDATA tnid;
tnid.cbSize=sizeof(NOTIFYICONDATA);
tnid.hWnd=this->m_hWnd;
tnid.uID=IDR_MAINFRAME;
Shell_NotifyIcon(NIM_DELETE,&tnid);
PostQuitMessage(WM_QUIT);
四、隐藏主窗口
在应用程序启动时用如下代替默认窗口
CXXDlg* dlg = new CXXDlg;
m_pMainWnd = dlg;
dlg->Create(IDD_SPRITE_DIALOG);
dlg->ShowWindow(SW_HIDE);