
Again: minimizing dialog-based app on startup....
Quote:
> >Hi there,
> >Maybe I'm a little impatient, but for those who didn't see my earlier
> >posting and have a solution, please reply...
> >- Start earlier message
> >I've developed a MFC application that is dialog based (so it's not SDI or
> >MDI!): It's main window is a dialog. Because I've attached a tray icon to
> >this application and a Hide button it has the feature of hiding itself and
> >when double clicking on the tray icon it displays the window. When clicking
> >on the Hide button I just call ShowWindow(SW_HIDE). However I also want
> this
> >application to hide itself when started up. So I guessed I put the same
> call
> >into the OnInitDialog function, however that does not work.
> >Does anybody have a good idea to make this work. I tried SendMessage,
> >PostMessage (all from the OnInitDialog function), but they either don't
> work
> >or make the application end-up in an endless loop killing itself.
> >Regards,
> >Rob
> >- End earlier message
I faced the same problem a few months ago, and here's the solution I
found.
The code below can be added to a dialog-based app as generated by App
Wizard
(or any other dialog-based app for that matter) to give it the behavior
you're looking for. The code should go into the CDialog-derived class
for your main
window.
- Add a visible flag. In the dialog's constructor initialize it to
FALSE
(i.e. hidden).
BOOL m_bVisible;
- Add a handler for OnWindowPosChanging. You'll have to add it by hand
as
it's not directly supported by ClassWizard (at least not in VC 4.1, I
don't
know about 5.0).
void CTestDlg::OnWindowPosChanging( WINDOWPOS* lpwndpos )
{
if ( !m_bVisible )
lpwndpos->flags &= ~SWP_SHOWWINDOW ;
CDialog::OnWindowPosChanging(lpwndpos);
}
- Add a function to show/hide the app.
void CTestDlg::DisplayWindow( BOOL bShow )
{
if ( bShow )
{
m_bVisible = TRUE;
ShowWindow( SW_SHOWNORMAL );
}
else
{
m_bVisible = FALSE;
ShowWindow( SW_HIDE );
}
}
- Add calls to the display function.
DisplayWindow( TRUE ) // in response to a context-menu selection
DisplayWindow( FALSE ) // when the user closes the dialog
HTH,
Eliot Rogers