AfxGetMainWnd returns CSplashWnd not CMainFrame during app startup 
Author Message
 AfxGetMainWnd returns CSplashWnd not CMainFrame during app startup

I found a trick in one of the MFC samples where we cast the value
returned from AfxGetMainWnd to be a pointer to CMainFrame.  It works
99.99999% of the time in the application I've been working with.

During OnNewDocument I want to store some text in a main frame member.
I'm saving a portion of information which CMainFrame uses to build
the title bar.  Most of the time this document member function which
stores this text can get the mainframe pointer using AfxGetMainWnd.
But I find during the very first OnNewDocument as the application is
just starting it will instead give me a point to CSplashWnd, which is,
to say the least, disastrous to cast as a pointer to CMainFrame.

I really have to question what one uses AfxGetMainWnd for, given
that you never know what your getting.

Is there a better way to obtain a reference to CMainFrame from the
document?  My thought was to save the pointer to CMainFrame
determined during construction, and use this instead of
AfxGetMainWnd.  But this seems kludgy.  Any better ideas?

BTW - Thanks to those two who recommended I obtain BoundsChecker.
I seriously doubt I could have found this without it.

Best regards - Lee Gillie
--
+--------------------------------------------------------------------+

[ Remove "NoSpam" from the reply E-Mail address.                     ]
+--------------------------------------------------------------------+



Wed, 02 Aug 2000 03:00:00 GMT  
 AfxGetMainWnd returns CSplashWnd not CMainFrame during app startup

Quote:
> I found a trick in one of the MFC samples where we cast the value
> returned from AfxGetMainWnd to be a pointer to CMainFrame.  It works
> 99.99999% of the time in the application I've been working with.

> During OnNewDocument I want to store some text in a main frame member.
> I'm saving a portion of information which CMainFrame uses to build
> the title bar.  Most of the time this document member function which
> stores this text can get the mainframe pointer using AfxGetMainWnd.
> But I find during the very first OnNewDocument as the application is
> just starting it will instead give me a point to CSplashWnd, which is,
> to say the least, disastrous to cast as a pointer to CMainFrame.

> I really have to question what one uses AfxGetMainWnd for, given
> that you never know what your getting.

> Is there a better way to obtain a reference to CMainFrame from the
> document?  My thought was to save the pointer to CMainFrame
> determined during construction, and use this instead of
> AfxGetMainWnd.  But this seems kludgy.  Any better ideas?

> BTW - Thanks to those two who recommended I obtain BoundsChecker.
> I seriously doubt I could have found this without it.

Lee,

I answered to your other post (?) that the document is created before the
frame. Therefore you may not have a frame in OnNewDocument().

Now reading this post :o) , I can also make a suggestion:

You should have a View floating around somewhere. You can access the
document as well as the Frame from CView::OnInitialUpdate().

You can also try and access the Document from the Frame (and ask it for the
title bar config) with (CYourDocument*)GetActiveDocument().

AfxGetMainWnd() _will_ return the CMainFrame window, provided it already
exists, and is not replaced by a splash ;)

cU
--

                        http://djuga.home.ml.org
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Retriever 1.0 is out:   http://retriever.home.ml.org

Shareware image viewer, thumbnailer & database
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



Wed, 02 Aug 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Getting pointer to CMainFrame with AfxGetMainWnd not working ?

2. Debugger hangs during startup

3. AfxGetMainWnd()->PostMessage(WM_CLOSE) is not fast enough

4. How to disable MDIChildFrame to showup during startup.

5. Access violation in heap allocation during startup

6. Tyring to Create A Window automatically during the startup of Application

7. Detecting ocx errors during application startup

8. Q: How to display a startup logo during initialization

9. Selecting from a few documents during startup

10. How2 make calling external app not return right away

11. Simple CMainFrame App

12. Call a CMainFrame menu function when app first starts

 

 
Powered by phpBB® Forum Software