user3883198
user3883198

Reputation:

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

Upvotes: 2

Views: 1473

Answers (2)

Barmak Shemirani
Barmak Shemirani

Reputation: 31599

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

Upvotes: 3

Joseph Willcoxson
Joseph Willcoxson

Reputation: 6040

You are not clear in what you want. Your title says you want to initially have the dialog hidden. then text in the question says you want it initially visible and then hidden. Which is it/

What do you mean by your requirement says that the dialog style has to be WS_VISIBLE. if you want to make it initially not visible, then don't include the flag.

For a modeless dialog, generally you create them on the heap whereas modal dialogs are typically created on the stack.

CYourDialog* pDlg = new CYourDialog(... and whatever arguments);
pDlg->Create(CYourDialog::IDD); // or whatever the ID is...
pDlg->ShowWindow(SW_NORMAL); // shows window if it was invisible...
pDlg->ShowWindow(SW_HIDE); // hides window if it was visible...

Upvotes: 1

Related Questions