Make MFC app invisible on startup (Hide main dialog)

I've already tried setting the dialogs Visible property to False in the dialog editor with no luck.. how else can I make the dialog invisible on startup? i.e. when the program starts, no dialog shows up any where.

The dialog needs to be able to become visible later as well.

Thanks.
V2dBAsked:
Who is Participating?
 
DanRollinsCommented:
This question comes up occasionally (and experts always give several wrong answers... :-).  The solution took a lot of time to find, but the reason for the problem and the steps to solve it are described clearly here:

     http:/MFC/Q_20059930.html#189474

I also think that the idea at kiranvj's link http:#11742022 might work, and that manish_cg  has a good idea, but I did not try either.  If the Asker is not listening to accept this (my own) answer, I will recommend a split of points.

-- Dan
0
 
hirnsiebCommented:
Call

    ShowWindow (SW_HIDE);

in the InitInstance method of your dialog class. Good luck!
0
 
SteHCommented:
In the InitInstance of the CWinApp derived class of your project you have the following lines

      CYourDlg dlg;
      m_pMainWnd = &dlg;
      int nResponse = dlg.DoModal(); // don't execute this line until the the dlg should be shown. But what code is running else?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
V2dBAuthor Commented:
Thanks guys, so far both answers have helped, but what im trying to do is, to show the main app dialog or not show it depending on user settings, the app has a tray icon, so depending on the setting I want the app to start hidden and only show when the user wants to view it.

If I remove the DoModal the app wont start at all as mentioned by SteH.

Thanks.
0
 
reidy_boyCommented:
Call ShowWindow (SW_HIDE); from within OnInitDialog
0
 
V2dBAuthor Commented:
Tried that, the dialog still pops up..
0
 
reidy_boyCommented:
Try:

Minimise();      
ShowWindow(SW_HIDE);
0
 
V2dBAuthor Commented:
Nope, didnt work
0
 
reidy_boyCommented:
Go to View->ClassWizard and create a function to trap the WM_TIMER message.

Now in your OnInitDialog method, insert the following line:
SetTimer (1, 100, NULL);

Next, go to your OnTimer function and insert the following:
ShowWindow(SW_HIDE);
0
 
reidy_boyCommented:
Sorry, should have told you to include KillTimer(1); after the call to ShowWindow :-)
0
 
Jaime OlivaresSoftware ArchitectCommented:
Hi V2dB,

I think you must manage the WS_VISIBLE style in your dialog resource.
Go to the dialog editor, clear the "Visible" attribute
Now your app will lauch but your dialog will not appear
To make it appear conditionaly, add a boolean member to your dialog object, let's say bool m_IsVisible, then inside OnInitDialog, evaluate your condition and set or clear the boolean flag.
Create an OnInitDialog event handler for your dialog and put this code
if (m_IsVisible) {
     ShowWindow(SW_SHOW);
     CenterWindow();
}


Good luck,
Jaime.
0
 
V2dBAuthor Commented:
reidy_boy:
Even setting the timer to 1 millisecond causes the window to flash by for a second

Jaime:
I've tried setting the dialogs Visible property to False in the dialog editor, but it still shows up (this is the main app dialog using MFC7)

Thanks.
0
 
Jaime OlivaresSoftware ArchitectCommented:
>I've tried setting the dialogs Visible property to False in the dialog editor, but it still shows up
I have tried too and don't show, maybe you have a ShowWindow() or UpdateWindow() call in another code zone.
0
 
V2dBAuthor Commented:
This is very strange and annoying, I just started a new dialog based MFC application, opened up the dialog in the editor, set the Visible property to False and the damn thing still pops up when I start the app!
0
 
Jaime OlivaresSoftware ArchitectCommented:
Please post all your OnInitInstance function
0
 
V2dBAuthor Commented:
Just the standard MFC generated code:


BOOL CVisibleApp::InitInstance()
{
      // InitCommonControls() is required on Windows XP if an application
      // manifest specifies use of ComCtl32.dll version 6 or later to enable
      // visual styles.  Otherwise, any window creation will fail.
      InitCommonControls();

      CWinApp::InitInstance();

      AfxEnableControlContainer();

      // Standard initialization
      // If you are not using these features and wish to reduce the size
      // of your final executable, you should remove from the following
      // the specific initialization routines you do not need
      // Change the registry key under which our settings are stored
      // TODO: You should modify this string to be something appropriate
      // such as the name of your company or organization
      SetRegistryKey(_T("Local AppWizard-Generated Applications"));

      CVisibleDlg dlg;
      m_pMainWnd = &dlg;
      INT_PTR nResponse = dlg.DoModal();
      if (nResponse == IDOK)
      {
            // TODO: Place code here to handle when the dialog is
            //  dismissed with OK
      }
      else if (nResponse == IDCANCEL)
      {
            // TODO: Place code here to handle when the dialog is
            //  dismissed with Cancel
      }

      // Since the dialog has been closed, return FALSE so that we exit the
      //  application, rather than start the application's message pump.
      return FALSE;
}
0
 
Jaime OlivaresSoftware ArchitectCommented:
I dont know why is showing if you say you have turned off visible attribute. Maybe anything in your OnInitDialog, please verify.
At the last resource, here is a dirty solution: Move your dialog to the "hyperspace"

On the OnInitDialog() or anywhere, instead of using ShowWindow(SW_HIDE):
SetWindowPos(0,-1000,-1000,0,0, SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE);
To make it appear again just use
CenterWindow();
0
 
V2dBAuthor Commented:
Jaime,

Are you using MFC7 (visual studio .NET) ?

using setwindowpos works well, but the same way I Cant get the main dialog to stay invisible the property for the taskbar icon doesnt seem to be working so its taskbar button is visible.

Do you think you could upload your sample app ?

Thanks.
0
 
Jaime OlivaresSoftware ArchitectCommented:
Sorry, I don't have VC7 in the machine where I am writting from right now. But my trick will not make the task button disappear since dialog is still visible (in theory).
0
 
Kiran Paul VJComputer EngineerCommented:
0
 
manish_cgCommented:
hi,

for modal dialog ShowWindow(SW_HIDE) in their OnInitDialog isnt sufficient to hide the dialog initially.the reason for this is - when CDialog::OnInitDialog() finishes it calls ShowWindow(SW_SHOW). Thus your dialog box is again made visisble.

To solve ur problem here is the trick-

Add a boolean member in your dialog class and set it to false.

Now you need to override WM_WINDOWPOSCHANGING.You have to change your message filtering options to have this message show up in the Class Wizard.

The code will look like this-

void CTestDlg::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos)
{
    if(!visible) //boolean data member
        lpwndpos->flags &= ~SWP_SHOWWINDOW;

    CDialog::OnWindowPosChanging(lpwndpos);
}
That's it. Now your modal dialog actually starts up in a hidden state. And when you want to make it visible this is what you need to do -

set boolean variale to true and call ShowWindow(SW_SHOW);

Manish Sinha

www.otssolutions.net
0
 
Kiran Paul VJComputer EngineerCommented:
hi manish..

ur code is working well with little changes. Why did u put the tilde (~) in front of SWP_SHOWWINDOW

with tilde its not working, i removed it and its its working fine.

great piece of code, i was looking for this for some time.

kiranvj
0
 
DanRollinsCommented:
V2dB,
It is time for you to finalize this old question -- accept an answer, split points, or get a refund.  If you do not take action, NEGATIVE FEEDBACK could be added to your profile.  Click here to see your options-> http:help.jsp#hs5 
EXPERTS,
Please post your closing recommendations within 4 days.  If you do not respond, I will assume that you think that no correct answer was provided.
0
 
reidy_boyCommented:
I suggest manish_cg should receive the points, seeing as kiranvj acknowledged that the code is working well, even though he did not post the original question.
0
 
Kiran Paul VJComputer EngineerCommented:
hi...

i also think manish_cg  deserves the points...
his code is working fine for me, but i dont know whether its working for V2dB

kiranvj
0
 
Kiran Paul VJComputer EngineerCommented:
hi Dan,

good info on your link.
Dan i have a sugestion ... i think it will be a good idea to start a frequently asked questions to each section.

thank u
kiranvj
0
 
reidy_boyCommented:
Isn't that the same as Current Hot MFC Solutions / Time Tested MFC Solutions?
0
 
Kiran Paul VJComputer EngineerCommented:
hi...

is that same as current hot mfc solutions, but the questions in current hot mfc solutions section is asked only once ( i think ), my suggestion is to include the most no:of times asked questions...

thank u
kiranvj
0
 
DanRollinsCommented:
The "current hot" and "time-tested" measure the number of times people reach a page via an search ENgine search (I think).  The answer might not be particularly good, but it would appear to be "Frequently Asked".

There is some plan in the works to give Page Editors a place to create special pages, including (I think) FAQs and "interesting questions" and maybe even a page named "In My Humble Opinion" :)

-- Dan

P.S.  If I fail to respond to this Q for a while it is because when doing MFC cleanup, I route all MFC notifs into a folder and never look at them until I work my way back to the Q on the second cleanup pass.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.