- DefWindowProc 试过 在某些情况下效果很好 但不适用大部分情况
LRESULT CXXXDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if(message==133 )
ShowWindow(SW_HIDE);
return CDialog::DefWindowProc(message, wParam, lParam);
}
- OnShowWindow
void CXXXDlg::OnShowWindow(BOOL bShow, UINT nStatus)
{
if( GetStyle() & WS_VISIBLE )
{
CDialog::OnShowWindow(bShow, nStatus);
}
else
{
long Style = ::GetWindowLong(*this, GWL_STYLE);
::SetWindowLong(*this, GWL_STYLE, Style | WS_VISIBLE);
CDialog::OnShowWindow(SW_HIDE, nStatus);
}
}
另外,隐藏任务栏图标,可修改窗口的扩展样式
隐藏任务栏图标:ModifyStyleEx(WS_EX_APPWINDOW, WS_EX_TOOLWINDOW);
显示任务栏图标:ModifyStyleEx(WS_EX_TOOLWINDOW, WS_EX_APPWINDOW);
- OnWindowPosChanging
void CXXXDlg::OnWindowPosChanging(WINDOWPOS* lpwndpos)
{
if (lpwndpos->flags & SWP_SHOWWINDOW)
{
lpwndpos->flags &= ~SWP_SHOWWINDOW;
PostMessage(WM_WINDOWPOSCHANGING, 0, (LPARAM)lpwndpos);
ShowWindow(SW_HIDE);
}
else
CDialog::OnWindowPosChanging(lpwndpos);
}
重载DoModal() 效果非常不错 满足绝大部分情况 需收藏
INT_PTR CxDlg::DoModal()
{
// can be constructed with a resource template or InitModalIndirect
ASSERT(m_lpszTemplateName != NULL || m_hDialogTemplate != NULL ||
m_lpDialogTemplate != NULL);
// load resource as necessary
LPCDLGTEMPLATE lpDialogTemplate = m_lpDialogTemplate;
HGLOBAL hDialogTemplate = m_hDialogTemplate;
HINSTANCE hInst = AfxGetResourceHandle();
if (m_lpszTemplateName != NULL)
{
hInst = AfxFindResourceHandle(m_lpszTemplateName, RT_DIALOG);
HRSRC hResource = ::FindResource(hInst, m_lpszTemplateName, RT_DIALOG);
hDialogTemplate = LoadResource(hInst, hResource);
}
if (hDialogTemplate != NULL)
lpDialogTemplate = (LPCDLGTEMPLATE)LockResource(hDialogTemplate);
// return -1 in case of failure to load the dialog template resource
if (lpDialogTemplate == NULL)
return -1;
// disable parent (before creating dialog)
HWND hWndParent = PreModal();
AfxUnhookWindowCreate();
BOOL bEnableParent = FALSE;
#ifndef _AFX_NO_OLE_SUPPORT
CWnd* pMainWnd = NULL;
BOOL bEnableMainWnd = FALSE;
#endif
if (hWndParent && hWndParent != ::GetDesktopWindow() && ::IsWindowEnabled(hWndParent))
{
::EnableWindow(hWndParent, FALSE);
bEnableParent = TRUE;
#ifndef _AFX_NO_OLE_SUPPORT
pMainWnd = AfxGetMainWnd();
if (pMainWnd && pMainWnd->IsFrameWnd() && pMainWnd->IsWindowEnabled())
{
//
// We are hosted by non-MFC container
//
pMainWnd->EnableWindow(FALSE);
bEnableMainWnd = TRUE;
}
#endif
}
TRY
{
// create modeless dialog
AfxHookWindowCreate(this);
if (CreateDlgIndirect(lpDialogTemplate,
CWnd::FromHandle(hWndParent), hInst))
{
if (m_nFlags & WF_CONTINUEMODAL)
{
// enter modal loop
DWORD dwFlags = 0; //MLF_SHOWONIDLE;
if (GetStyle() & DS_NOIDLEMSG)
dwFlags |= MLF_NOIDLEMSG;
VERIFY(RunModalLoop(dwFlags) == m_nModalResult);
}
// hide the window before enabling the parent, etc.
if (m_hWnd != NULL)
SetWindowPos(NULL, 0, 0, 0, 0, SWP_HIDEWINDOW|
SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER);
}
}
CATCH_ALL(e)
{
{ e->Delete(); } // DELETE_EXCEPTION(e);
m_nModalResult = -1;
}
END_CATCH_ALL
#ifndef _AFX_NO_OLE_SUPPORT
if (bEnableMainWnd)
pMainWnd->EnableWindow(TRUE);
#endif
if (bEnableParent)
::EnableWindow(hWndParent, TRUE);
if (hWndParent != NULL && ::GetActiveWindow() == m_hWnd)
::SetActiveWindow(hWndParent);
// destroy modal window
DestroyWindow();
PostModal();
// unlock/free resources as necessary
if (m_lpszTemplateName != NULL || m_hDialogTemplate != NULL)
UnlockResource(hDialogTemplate);
if (m_lpszTemplateName != NULL)
FreeResource(hDialogTemplate);
return m_nModalResult;
}
http://blog.csdn.net/liang_lq/article/details/6215398
http://blog.csdn.net/zgl7903/article/details/5405978 牛b中的战斗机
http://bbs.csdn.net/topics/390133752
http://topic.okbase.net/200901/2009012111/3554358.html
http://bbs.csdn.net/topics/90239741
http://tc.wangchao.net.cn/bbs/detail_23079.html