隐藏MFC对话框DoModal出来的窗口

前言:
环境:win7sp1 x64 vs2008 mfc
目的:对话框程序 想在初始化时隐藏界面
实施:
- 在重载函数OnInitDialog()中添加ShowWindow(SW_HIDE); 发现不起作用 所以有了度娘谷哥之路
过程:
- WM_NCPAINT
void CxDlg::OnNcPaint()
{
static int i = 2;
if (i > 0)
{
i--;
ShowWindow(SW_HIDE);
}
else
{
CDialogEx::OnNcPaint();
}
}
效果一级棒 但是我有两个工程 一个i=2才行 一个i=3才行 网上的说法i=2是有道理的  但我查不出第三个消息在哪发的  所以暂时保留着这个方案
- 定时器 没想过 因为一看都不专业

- 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);
}


- WINDOWPLACEMENT   wp;  
wp.length=sizeof(WINDOWPLACEMENT);
wp.flags=WPF_RESTORETOMAXIMIZED;  
wp.showCmd=SW_HIDE;  
SetWindowPlacement(&wp);
 
ModifyStyleEx(WS_EX_APPWINDOW, WS_EX_TOOLWINDOW);
- 上面3种情况有好有坏 网上评价不一 我都没有试过 偶然间我看到了最后一种情况:

重载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;
}


总结:
- 消息和重载都能很好的实现想要的效果 但很少有方案不受限制 计时器第一个被否定 后面几种在无界面时 效果很好 最符合的还是WM_NCPAINT和DoModal重载
- 千万不要把业务逻辑放在界面消息 就算要放也要非常慎重

参考资料:

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

你可能感兴趣的:(visual,C++)