隐藏 MFC 对话框

2022-01-12 00:00:00 visual-studio winapi visual-c++ c++ mfc

好的,所以我正在使用此代码来隐藏基于对话框的 MFC 应用程序 (VC++) 的任务栏图标.每当我单击十字或关闭按钮时,任务栏图标和对话框都会隐藏.但我不能把这件事做对.每当我从标题栏点击关闭或十字按钮时,对话框首先闪烁并显示一种中间对话框,然后隐藏.这很烦人.经过两天徒劳的努力,我在这里发布了我的代码.所以请大家帮帮我.提前致谢.

Ok so I am using this code to hide the taskbar icon of a dialog based MFC application(VC++). The taskbar icon and the dialog box hide whenever I click on the cross or the close buttons. But I can’t get this one thing right. Whenever I hit the close or the cross button from title bar, the dialog box first flickers and shows a sort of intermediate dialog box and then hides. This is very annoying. I am posting my code here after two days of vain effort. So guys please help me. Thanks in advance.

void CMyAppDlg::OnBnClickedCancel()
{
  // TODO: Add your control notification handler code here
  CWnd* pWnd;
  pWnd = AfxGetMainWnd();

  RemoveTaskbarIcon(pWnd);
  pWnd->ModifyStyle(WS_VISIBLE, 0);
  mVisible = FALSE;
}

BOOL CMyAppDlg::RemoveTaskbarIcon(CWnd* pWnd)
{
  LPCTSTR pstrOwnerClass = AfxRegisterWndClass(0);

  // Create static invisible window
  if (!::IsWindow(mWndInvisible.m_hWnd))
   {
    if (!mWndInvisible.CreateEx(0, pstrOwnerClass, _T(""),
             WS_POPUP,
             CW_USEDEFAULT,
             CW_USEDEFAULT, 
             CW_USEDEFAULT, 
            CW_USEDEFAULT,
             NULL, 0))
      return FALSE;
   }

   pWnd->SetParent(&mWndInvisible);

  return TRUE;
}

这是对话框的屏幕截图.当我按下关闭或交叉按钮时,首先看起来像 this 的对话框会转动进入 this 不到半秒,然后消失(隐藏).

Here are the screen shots of dialog box. When I press the close or cross button, the dialog box which looks like this in the first place turns into this for like less than half a second and then disappears (hides).

推荐答案

如果您使用 CDialog::DoModal() 显示对话框,框架将确保显示您的对话框.只有一种方法可以防止显示模式对话框:

If you show your dialog using CDialog::DoModal() the framework will make sure your dialog is shown. There is only one way to prevent a modal dialog from being shown:

BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
    ON_WM_WINDOWPOSCHANGING()
END_MESSAGE_MAP()

BOOL CHiddenDialog::OnInitDialog()
{
    CDialog::OnInitDialog();
    m_visible = FALSE;

    return TRUE;
}

void CHiddenDialog::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos) 
{
    if (!m_visible)
        lpwndpos->flags &= ~SWP_SHOWWINDOW;

    CDialog::OnWindowPosChanging(lpwndpos);
}

相关文章