1. 隐藏窗体
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
cs.style = WS_POPUP;//使主窗口不可见
cs.dwExStyle = WS_EX_TOOLWINDOW;//不显示任务按钮
cs.lpszClass = AfxRegisterWndClass(0);
return TRUE;
}
2. 增加托盘
CMainFrame类中增加变量
NOTIFYICONDATA m_tnid;
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)函数返回前增加如下代码
m_tnid.cbSize = sizeof(NOTIFYICONDATA);
m_tnid.hWnd = this->m_hWnd;
m_tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
m_tnid.uCallbackMessage = MYWM_NOTIFYICON;
CString szToolTip = _T("故¨º障?辅¡§助¨²");
_tcscpy(m_tnid.szTip, szToolTip);
m_tnid.uID = IDR_MAINFRAME;
HICON hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_tnid.hIcon = hIcon;
::Shell_NotifyIcon(NIM_ADD, &m_tnid);
if(hIcon)
{
::DestroyIcon(hIcon);
}
3. 消息处理
LRESULT CMainFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case MYWM_NOTIFYICON:
if(lParam == WM_LBUTTONDBLCLK)
{
AfxGetApp()->m_pMainWnd->ShowWindow(SW_SHOW);
}
else if(lParam == WM_RBUTTONDOWN)
{
CMenu menu;
menu.LoadMenu(IDR_MAINFRAME);
CMenu* pMenu=menu.GetSubMenu(0);
CPoint pos;
GetCursorPos(&pos);
pMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,pos.x,pos.y,AfxGetMainWnd());
}
break;
case WM_SYSCOMMAND:
switch(wParam)
{
case SC_MINIMIZE:
ShowWindow(SW_HIDE);
break;
case SC_CLOSE :
::Shell_NotifyIcon(NIM_DELETE, &m_tnid);
break;
}
break;
}
return CFrameWnd::WindowProc(message, wParam, lParam);
}
标签:功能,CMainFrame,pos,break,托盘,hIcon,cs,三步,tnid From: https://www.cnblogs.com/xiaoxiaoshi/p/17483699.html