Link to home
Start Free TrialLog in
Avatar of toddmoore
toddmoore

asked on

Making a Modal Dialog invisble prior to display?

Okay, whats the trick to create a modal CDialog box
to be invisible at first?  ShowWindow(SW_HIDE) does not
work from OnInitDialog().  I can't remember the trick,
have to override either one of the virtuals or handle
a system message and modify the style.  Please help!
Avatar of ghimireniraj
ghimireniraj

What do you mean by at first?
When do you wan ti to show?
Avatar of toddmoore

ASKER

I want the dialog application to not display itself
initially.  It registers a sys tray icon which you
can double click to unhide it.
Try turning off the visible flag in the dialog styles in the resource editor.
Avatar of DanRollins
I believe that just before going modal, the framework forces the dialog to be visible (clearing the Visible checkbox in the dialog editor is no good).  So you need to let the window creation complete, then hide it.

One simple way is add this to the bottom of your ClassWizard-defined OnPaint handler:

  static BOOL gfNeedsToHide= TRUE;
  if ( gfNeedsToHide ) {
    if ( IsWindowVisible()  )  ShowWindow( SW_HIDE );
    else gfNeedsToHide= FALSE;
  }

A better way:  You probably have a button that the user can use to hide the window.  At the end of OnInitDialog, place this:

  PostMessage( WM_COMMAND, IDC_TheButtonThatHidesMe, 0 );

If you don't have such a button, you can pass a custom message.  Do you need information on how to do this?

I think you are stuck with the quick flash as the dialog goes visible then hides itself.   You could move it off screen first, but that seems kludgy and the taskbar button appears briefly anyway.   I'll be interested to see if somebody can come up with a clean solution that avoids that flash.

-- Dan
ON_WM_NCPAINT()

void CHidedlgDlg::OnNcPaint()
{
    if (m_bHide)
    {
        this->ShowWindow(SW_HIDE);
    }
     
    CDialog::OnNcPaint();
}
Rejected the answer because that actually removes the menu from the dialog box.  My program has the logic to hide itself when you try and close it.  It hides itself and you use the system tray icon to unhide it.  works great.  

I would like to figure out how to properly do solve this... sure i could use a on paint handler or a window timer to hide it, but that causes it to flash in both the task manager and the window briefly puts itself on the screen...  seems like there should be a way to do it without the flashing...

I guess you have to create a Dialog from your owm Template in Memory, initially constructing a dialog with no dialog template parameter(enpty constructor).Then using InitModalIndirect(..) before actuallt calling DoModal().I tried it but i couldn't do it.

There is also a CDialogTemplate class that is undocumented for which you need to include afxpriv.h, even tried that but didn't help.I don't know why i can't change the initial style of the dialog no matter what i do.It always seems to ignore it or get its value form somewhere at some particular time..

Let's hope we know some solution.


--Niraj

The best I have so far is a combination of the above comments...  When a Modal box first opens it will fire off a WM_PAINT and WM_NCPAINT messages.  If I put the following sniplet in, it sorta work (but it flashes)

        static onetime=true;
     if (onetime)
     {
          ShowWindow(SW_HIDE);
          onetime=false;
          return;
     }    

This will ignore the first messages... i'm starting to think this could be done better with a override of WindowProc...
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Brilliant Dan!

--Niraj
>because that actually removes the menu from the dialog box.

Where is the menu? How can you see it since you hide the entire dialog box?
Thanks Dan, just the answer I was looking for!  Seems like a lot of work for just a little thing, but after diving into the depths of DoModal, I don't see any other way.  Cheers!

--Todd
ghimireniraj>>Brilliant Dan!
Thanks! You are too kind.  

toddmoore:
Thanks for accepting and thanks for an interesting challenge.

-- Dan
Thats the spirit!