Solved

Make MFC app invisible on startup (Hide main dialog)

Posted on 2004-08-06
30
2,335 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:V2dB
  • 7
  • 6
  • 5
  • +5
30 Comments
 
LVL 1

Expert Comment

by:hirnsieb
ID: 11735871
Call

    ShowWindow (SW_HIDE);

in the InitInstance method of your dialog class. Good luck!
0
 
LVL 13

Expert Comment

by:SteH
ID: 11735891
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
 

Author Comment

by:V2dB
ID: 11736006
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
 
LVL 1

Expert Comment

by:reidy_boy
ID: 11736545
Call ShowWindow (SW_HIDE); from within OnInitDialog
0
 

Author Comment

by:V2dB
ID: 11736606
Tried that, the dialog still pops up..
0
 
LVL 1

Expert Comment

by:reidy_boy
ID: 11736713
Try:

Minimise();      
ShowWindow(SW_HIDE);
0
 

Author Comment

by:V2dB
ID: 11736769
Nope, didnt work
0
 
LVL 1

Expert Comment

by:reidy_boy
ID: 11737064
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
 
LVL 1

Expert Comment

by:reidy_boy
ID: 11737092
Sorry, should have told you to include KillTimer(1); after the call to ShowWindow :-)
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11737115
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
 

Author Comment

by:V2dB
ID: 11737175
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11737222
>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
 

Author Comment

by:V2dB
ID: 11737300
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11737346
Please post all your OnInitInstance function
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:V2dB
ID: 11737365
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11737462
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
 

Author Comment

by:V2dB
ID: 11737658
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11737694
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
 
LVL 16

Assisted Solution

by:kiranvj
kiranvj earned 28 total points
ID: 11742022
0
 

Assisted Solution

by:manish_cg
manish_cg earned 27 total points
ID: 11773288
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
 
LVL 16

Expert Comment

by:kiranvj
ID: 11830802
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 12014096
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
 
LVL 1

Expert Comment

by:reidy_boy
ID: 12014661
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
 
LVL 16

Expert Comment

by:kiranvj
ID: 12017118
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
 
LVL 49

Accepted Solution

by:
DanRollins earned 20 total points
ID: 12023018
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
 
LVL 16

Expert Comment

by:kiranvj
ID: 12038361
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
 
LVL 1

Expert Comment

by:reidy_boy
ID: 12042266
Isn't that the same as Current Hot MFC Solutions / Time Tested MFC Solutions?
0
 
LVL 16

Expert Comment

by:kiranvj
ID: 12043984
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 12080055
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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video discusses moving either the default database or any database to a new volume.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now