Hiding the MFC Dialog

Ok, so I use this code to hide the dialog-based MFC application taskbar icon (VC ++). The taskbar icon and dialog box are hidden whenever I click the cross or close buttons. But I can’t get it right. Whenever I click the close or cross button in the title bar, the dialog box first flickers and shows a kind of intermediate dialog box, and then it hides. This is very annoying. I post my code here after two days of futile 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; } 

Here are the screenshots of the dialog box. When I click the close or cross button, a dialog box that looks like this first turns to this in less than half a second, and then disappears (hides).

+4
source share
3 answers

If you show your dialog box using CDialog::DoModal() , the framework will show your dialog box. There is only one way to prevent the display of a modal dialog:

 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); } 
+16
source

Maybe the obvious thing, but what happens when you make this skin before you open the dialogue? Also, if you do not directly change the window style, but use ShowWindow (SW_HIDE)?

Finally, you tried to switch the dialog box style to WS_CHILD before calling SetParent () and / or perhaps pushing it out of the client area so that the window no longer appears (MoveWindow (-1000, -1000) or something like that).

0
source

I think Paul Dilashia recommended the following. This is for modal dialogs only.

You can put the following code in OnInitDialog to move the dialog off-screen. If necessary, you will need to implement a method for moving it to the screen.

 CRect DialogRect; GetWindowRect(&DialogRect); int DialogWidth = DialogRect.Width(); int DialogHeight = DialogRect.Height(); MoveWindow(0-DialogWidth, 0-DialogHeight, DialogWidth, DialogHeight); 

The answer from l33t looks good and probably better, but it is an alternative.

0
source

Source: https://habr.com/ru/post/1382856/


All Articles