15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


AfxGetMainWnd returns null

stas -- stas@goldnet.net.il
Thursday, December 26, 1996

Environment: VC++ 4.2b, NT4.0

Hi All

I'd created an SDI application in which I want to activate some
functions automatically.  Those functions need to be activated only
when Main application's window is existing (AfxGetMainWnd() returns
valid pointer).  I've tried OnCreate in my view & MainFrame class but
AfxGetMainWnd() returns NULL.

Do you know the place from where I can invoke my init code.
Any advise will be appreciated.

Regards,
Stas.

_______________________________________________________________________
LOSE WEIGHT NOW ASK ME HOW
_______________________________________________________________________



Joe Willcoxson -- joew@statsoft.com
Thursday, December 26, 1996

At 09:30 AM 12/26/96 +0200, you wrote:
>Environment: VC++ 4.2b, NT4.0
>
>Hi All
>
>I'd created an SDI application in which I want to activate some
>functions automatically.  Those functions need to be activated only
>when Main application's window is existing (AfxGetMainWnd() returns
>valid pointer).  I've tried OnCreate in my view & MainFrame class but
>AfxGetMainWnd() returns NULL.
>
>Do you know the place from where I can invoke my init code.
>Any advise will be appreciated.

Try in InitInstance after m_pMainWnd = pMainFrame!

pMainFrame->LoadFrame creates the window.  The frame and the windows
WM_CREATE handlers (OnCreate) get called before LoadFrame returns.  The
wizard writers rightly decided to make sure AfxGetMainWnd would not return
a valid CWnd pointer until the window had a valid HWND.

If you set breakpoints in your OnCreate handlers you can see the order of
window creation.  This would be very instructional.

--
Gloria in excelsius Deo
Joe Willcoxson (joew@statsoft.com), Senior Software Engineer
Visit us: http://www.statsoft.com, Visit me: http://users.aol.com/chinajoe
#define STD_DISCLAIMER "I speak only for myself"
__cplusplus spoken here;




Donald C. Asonye -- donald@menudo.uh.edu
Thursday, December 26, 1996

> 
> Environment: VC++ 4.2b, NT4.0
> 
> Hi All
> 
> I'd created an SDI application in which I want to activate some
> functions automatically.  Those functions need to be activated only
> when Main application's window is existing (AfxGetMainWnd() returns
> valid pointer).  I've tried OnCreate in my view & MainFrame class but
> AfxGetMainWnd() returns NULL.
> 
> Do you know the place from where I can invoke my init code.
> Any advise will be appreciated.

Maybe you should try doing this in OnInitialUpdate of your
view.

[Moderator's note: That's certainly the right place if you have
a view, and it would have been my answer if he had said he had
a view, but...]

> 
> Regards,
> Stas.
> 
-- 
Donald
**************************************************
*  Pull me under.                                *
*  Pull me under, I am not afraid -Dream Theater *
**************************************************
Email    :mailto:donald@uh.edu 
Samples  :ftp://ftp.hpc.uh.edu/pub/donald
WWW      :http://www.uh.edu/~dasonye



Roma -- roma@neonet.lv
Friday, December 27, 1996

[Mini-digest: 2 responses]

stas wrote:
> 
> Environment: VC++ 4.2b, NT4.0
> 
> Hi All
> 
> I'd created an SDI application in which I want to activate some
> functions automatically.  Those functions need to be activated only
> when Main application's window is existing (AfxGetMainWnd() returns
> valid pointer).  I've tried OnCreate in my view & MainFrame class but
> AfxGetMainWnd() returns NULL.
> 
> Do you know the place from where I can invoke my init code.
> Any advise will be appreciated.
> 
> Regards,
> Stas.

 At the end of your InitInstance() call PostMessage(WM_COMMAND,
);
 insert message map entry ON_COMMAND(, OnSomeId) in your
 application's message map. In the OnSomeId() do your initialization 
 code.
  
 This posted wm_command message will be handled after the main window
creation.
 
-Roma
-----From: Mike Marshall 

Make sure you have something like the following code in
your InitInstance of your application class.  You need to
explicitly denote your main window as follows:

	// create main MDI Frame window
	CMainFrame* pMainFrame = new CMainFrame;
	if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
		return FALSE;
	m_pMainWnd = pMainFrame; // This line is the key!


The AppWizard should have done this for you.

There is one glaring flaw in what you tried to do.
Your tried to call AfxGetMainWnd() in your MainFrame
OnCreate, which is called as a result of the LoadFrame
call in the above code.

As you can see, the m_pMainWnd member of your Application
class is not set until after LoadFrame is called!

Therefore, you should try to do your initialization after the 
line "m_pMainWnd = pMainFrame".

The reason for this is evidenced by the following code which
traces a AfxGetMainWnd() call:

// from Afxwin1.inl
_AFXWIN_INLINE CWnd* AFXAPI AfxGetMainWnd()
	{ return AfxGetThread() != NULL ? AfxGetThread()->GetMainWnd() : NULL; }

///////////////////////////////////////////////////////////////////////////// from Thrdcore.cpp
// Access to m_pMainWnd & m_pActiveWnd

CWnd* CWinThread::GetMainWnd()
{
	if (m_pActiveWnd != NULL)
		return m_pActiveWnd;    // probably in-place active

	// when not inplace active, just return main window
	if (m_pMainWnd != NULL)
		return m_pMainWnd; // Here's the important line!

	return CWnd::GetActiveWindow();
}

As you can see, AfxGetMainWnd() eventually returns that
m_pMainWnd member variable I was talking about (if you
are not in place active.)


Hope this helps!

Mike



Paul.B.Folbrecht@JCI.Com
Friday, December 27, 1996


     If you're IN your mainframe class in ::OnCreate() you don't have to 
     call AfxGetMainWnd() to get the main window.. you're in it! 


______________________________ Reply Separator _________________________________
Subject: AfxGetMainWnd returns null
Author:  stas@goldnet.net.il at Mailhub
Date:    12/26/96 2:43 PM


Environment: VC++ 4.2b, NT4.0
     
Hi All
     
I'd created an SDI application in which I want to activate some 
functions automatically.  Those functions need to be activated only 
when Main application's window is existing (AfxGetMainWnd() returns 
valid pointer).  I've tried OnCreate in my view & MainFrame class but 
AfxGetMainWnd() returns NULL.
     
Do you know the place from where I can invoke my init code. 
Any advise will be appreciated.
     
Regards,
Stas.
     
_______________________________________________________________________ 
LOSE WEIGHT NOW ASK ME HOW
_______________________________________________________________________




| Вернуться в корень Архива |