Re: Without m_pMainWnd
"sawer" <sawer@discussions.microsoft.com> wrote in message
news:E2E6C410-1D14-44F1-AD4E-F7AEF82E8DD5@microsoft.com...
Hi
I am trying to understand the function of m_pMainWnd. But i have a
problem.
For example
BOOL CMyApp::InitInstance()
{
CMyWindow *MyWindow = new CMyWindow();//derived from CFrameWnd
m_pMainWnd = MyWindow;//If i remove here, it shows and closes window
MyWindow->Create(NULL, L"window");
// m_pMainWnd->Create(...) calls Cwnd::Create not CFrameWnd::Create like
in
m_pMainWnd = MyWindow
MyWindow->ShowWindow(m_nCmdShow);
MyWindow->UpdateWindow();
return TRUE;
};
1-) What is happening in m_pMainWnd = MyWindow assignment so window does
not
close immediately? Is this assignment call some functions in CwinThread
class? Or something else?
One of the things which MFC gives you is the "Application Framework".
Typically this involves classes for an application (derived from CWinApp)
and for a main window and a view window (both derived via intermediate
stages from CWnd).
The m_pMainWnd member of the CWinApp points to the main window of the
application. In the MFC Application Framework, the main window and the
application class work in an interrelated way. If you want to see how, the
best way I can think of is to create an MFC application with the wizard and
step through with the debugger.
But if you *don't* set m_pMainWnd to point to the main window, then the
application (the CWinApp) knows nothing about its main window and the
application is (quite literally) broken.
2-) Also this is very interesting for me and i do not understand why it is
so.
I assigned m_pMainWnd MyWindow object that derived from CFrameWnd class so
m_pMainWnd holds CFrameWnd object but m_pMainWnd->Create(...) calls
Cwnd::Create not CFrameWnd::Create ? Why is it so? How can it be?
A quick liook at the MFC source (put the caret on CWinApp and press F12)
shows that CWinApp is derived from CWinThread and CWinThread has a member
CWnd *m_pMainWnd;
and so if you call m_pMainWnd->Create(...) this is obviously CWnd::Create().
This is why you first create a window of whatever derived variety is
required and then assign the m_pMainWnd pointer to it.
Storing a pointer to the base class of some object and manipulating it via
that is a standard feature of object-oriented code, of which the advantages
are usually listed as "Encapsulation, Inheritance, and Polymorphism". This
is polymorphism.
Finally: in my experience (someone will correct me if I'm wrong!) people do
not usually delve into the inner workings of the MFC application framework
unless they really have to! If you create an application with the wizard,
then you get the framework for free. Believe that its structure is the way
it is for a reason (it's hard, I know) and do not tinker with the basics.
Building on it is fine; attacking the foundations usually ends in tears.
Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm
Can you please help me to understand this assignment.
I am looking for your answers...