sujitha sujitha - 2 months ago 21
C++ Question

Show modelless dialog initially hidden

I have modelless child dialog. In resource properties Visible flag is set as true.(As per my requirement in resource properties visible flag should be true).

I want to programmatically hide the dialog while initially displaying.

I overrided the presubclasswindow and removed the WS_VISIBLE flag using below code but the dialog is not getting hidden.

void CAddressChildDlg::PreSubclassWindow()
{
CWnd::PreSubclassWindow();
if (::IsWindow(m_hWnd))
{
LONG lStyle = GetWindowLong(m_hWnd, GWL_STYLE);
lStyle &= ~WS_VISIBLE;
SetWindowLong(m_hWnd, GWL_STYLE, lStyle);
}
}


Please anyone help me to achieve my requirement

Answer

You can also override ON_WM_WINDOWPOSCHANGING

class CMyDialog : public CDialog
{
public:
    bool m_override_showwindow;
    //initialize somewhere ... 

    void OnWindowPosChanging(WINDOWPOS* wpos)
    {
        if (m_override_showwindow)
            wpos->flags &= ~SWP_SHOWWINDOW;
        CDialog::OnWindowPosChanging(wpos);
    }
    DECLARE_MESSAGE_MAP()
    ...    
};

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

Enable this override only when you don't want it to show the dialog. Make sure to disable the override otherwise dialog is never shown.

dlg.m_override_showwindow = true;
dlg.Create(...);
dlg.m_override_showwindow = false;

MessageBox(L"Test...");
dlg.ShowWindow(SW_SHOW);
Comments