How do you programmatically set the "visible" attribute in CDialog?

How do you programmatically set the "visible" attribute in CDialog? I need to change this attribute in the middle of the program - so a 1 time setting of this in the resource is not good enough. - Any suggestions?
[213 byte] By [ken pat] at [2007-11-18 17:41:19]
# 1 Re: How do you programmatically set the "visible" attribute in CDialog?
ShowWindow
Sam Hobbs at 2007-11-11 1:26:03 >
# 2 Re: How do you programmatically set the "visible" attribute in CDialog?
This is not sufficient for me and I will explain to you why.

I have an application that while it is booting up - I create a dialog with a third party graph in it. Right after I create it - I then destroy it. The reason for this is that the first time this third party graph is brought up - it takes a long time for it to be displayed - so I create this graph during the bringing up of the application - so that later on - it will be displayed instantly. Now when I use ShowWindow(SW_HIDE) - the dialog is still shown for about a second on the screen and this looks very ugly. On the other hand - setting in the resource the visible flag as being off - causes not even a split second displaying of the dialog. I assume that the reason for this is that ShowWindow() requires that the dialog's m_hWnd exists and this means that for the initial first second of creation of the dialog we will see the window before ShowWindow will do its thing.

So is there some type of way to set the dialog as being not visible before it turns into a m_hWnd?

P.S. Note that I could make the Visible attribute in the resource be turned off and for later on in the program - I could do ShowWindow(SW_SHOW) - but then this leads to other problems that if a lot of CPU is being used - then it takes a long time before the window is shown for later on in the program - or at times the window appears behind the application - the reason for this is the same problem - that ShowWindow() requires that the dialog's m_hWnd exists.
ken pat at 2007-11-11 1:27:04 >
# 3 Re: How do you programmatically set the "visible" attribute in CDialog?
GetDlgItem( IDC_BUTTON ) -> ShowWindow( true );
GetDlgItem( IDC_BUTTON ) -> EnableWindow( true );

IDC_BUTTON can be the name of a static text, window or button.
true will show and false will hide. same as enable.

Hope this helps
darian at 2007-11-11 1:28:05 >
# 4 Re: How do you programmatically set the "visible" attribute in CDialog?
Originally posted by darian
GetDlgItem( IDC_BUTTON ) -> ShowWindow( true );
GetDlgItem( IDC_BUTTON ) -> EnableWindow( true );

IDC_BUTTON can be the name of a static text, window or button.
true will show and false will hide. same as enable.

Note that you should avoid using the C++ boolean types while dealing with MFC...since it uses the Microsoft types instead - and these are different (-> integers)...
Andreas Masur at 2007-11-11 1:29:10 >
# 5 Re: How do you programmatically set the "visible" attribute in CDialog?
I know of no reason not to use the C++ Boolean Type (bool) in a MFC or Windows program as long as it is not used as if it is a Windows BOOL type. However I sure agree that the constants for the bool type (true and false) should not be used instead of the TRUE and FALSE values used for the BOOL type.
Sam Hobbs at 2007-11-11 1:30:09 >
# 6 Re: How do you programmatically set the "visible" attribute in CDialog?
So is there some type of way to set the dialog as being not visible before it turns into a m_hWnd?

Override PreCreateWindow in your dialog class, this allows you to modify the style before the window is created.

You should be able to remove the WS_VISIBLE style at this point.
wayside at 2007-11-11 1:31:08 >
# 7 Re: How do you programmatically set the "visible" attribute in CDialog?
I tried the various solutions given and still none of them give me the same effect as if I set in the resources the visible checked off.

Though overriding PreCreateWindow and setting cs.style to be cs.style &= ~WS_VISIBLE was the best of the solutions but still there was a split second showing on the screen.

- Ken
ken pat at 2007-11-11 1:32:12 >
# 8 Re: How do you programmatically set the "visible" attribute in CDialog?
That's very strange; PreCreateWindows runs before the window is created so I don't understand why the style change isn't applied until after the window is created.

Try this:

In your PreCreateWindow function, move the window off-screen:

cs.x = ::GetSystemMetrics(SM_CXSCREEN) +10;
cs.y = ::GetSystemMetrics(SM_CYSCREEN) +10;

Hopefully this will do the trick.

Another suggestion would be to duplicate the resource (giving it a different ID) and set one of them to be invisible. Change the dialog constructor to take the resource as an argument. The first time you can use the resource that has been set to not be visible, after that use the resource that is visible.
wayside at 2007-11-11 1:33:18 >
# 9 Re: How do you programmatically set the "visible" attribute in CDialog?
Now it is even better - but still not 100%.
Thanks - Ken
ken pat at 2007-11-11 1:34:18 >
# 10 Re: How do you programmatically set the "visible" attribute in CDialog?
Did you try the duplicate resource trick?

That should work 100%.
wayside at 2007-11-11 1:35:19 >