AfxGetMainWnd while creating CMainFrame
-
Hi. I have a MFC application that creates a lot of child windows. I want to get a pointer to CMainFrame from my child windows during their OnCreate handlers. Im doing it like this; CMainFrame * pMain=(CMainFrame *)AfxGetMainWnd(); The problem is that it only works AFTER all initialization is done, for example when i trigger a handler for a window click. I REALLY need to get a pointer to CMainFrame during the creating procedure but i cant find a place in CMainframe to insert my child windows "Create" function. Ive tried it on "OnCreateClient" and in "OnCreate", even after the CFrameWnd::OnCreate(...). Thank you in advance
-
Hi. I have a MFC application that creates a lot of child windows. I want to get a pointer to CMainFrame from my child windows during their OnCreate handlers. Im doing it like this; CMainFrame * pMain=(CMainFrame *)AfxGetMainWnd(); The problem is that it only works AFTER all initialization is done, for example when i trigger a handler for a window click. I REALLY need to get a pointer to CMainFrame during the creating procedure but i cant find a place in CMainframe to insert my child windows "Create" function. Ive tried it on "OnCreateClient" and in "OnCreate", even after the CFrameWnd::OnCreate(...). Thank you in advance
use the
hwndParent
parameter of the suppliedCREATESTRUCT
structure.
CPUA 0x5041 Sonork 100.11743 Chicken Little "So it can now be written in stone as a testament to humanities achievments "PJ did Pi at CP"." Colin Davies Within you lies the power for good - Use it!
-
use the
hwndParent
parameter of the suppliedCREATESTRUCT
structure.
CPUA 0x5041 Sonork 100.11743 Chicken Little "So it can now be written in stone as a testament to humanities achievments "PJ did Pi at CP"." Colin Davies Within you lies the power for good - Use it!
The hwndParent parameter isnt pointing to CMainFrame. This is a child CWnd fomr a child window from a CMainFrame's child window. So, its CMainFrame great grandaugther. Also, is there a way i can declare a global variable pointing to CMainFrame?
-
The hwndParent parameter isnt pointing to CMainFrame. This is a child CWnd fomr a child window from a CMainFrame's child window. So, its CMainFrame great grandaugther. Also, is there a way i can declare a global variable pointing to CMainFrame?
You should be abl to set a global variable to a valid CMainFrame in your CMainFrame::OnCreate() member function as long as it is after the call to the base class's OnCreate() function. I have not tried this, but here goes:
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;g\_MyGlobalMainFrame = this;
...
CPUA 0x5041 Sonork 100.11743 Chicken Little "So it can now be written in stone as a testament to humanities achievments "PJ did Pi at CP"." Colin Davies Within you lies the power for good - Use it!
-
You should be abl to set a global variable to a valid CMainFrame in your CMainFrame::OnCreate() member function as long as it is after the call to the base class's OnCreate() function. I have not tried this, but here goes:
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;g\_MyGlobalMainFrame = this;
...
CPUA 0x5041 Sonork 100.11743 Chicken Little "So it can now be written in stone as a testament to humanities achievments "PJ did Pi at CP"." Colin Davies Within you lies the power for good - Use it!
Im having no problem in creating a global variable. The problem is that it must be declared in a file that every other header can include it. If my child window is declared as an include in CMainframe, and the global is also in CMainframe, i cant include CMainframe in the child, cause it gives me a Linking error cause im going recursive in includes. So, can i just create a separate file (or use StdAfx?), create the global there, initialize it within CMainframe and include it in each class i want?
-
Im having no problem in creating a global variable. The problem is that it must be declared in a file that every other header can include it. If my child window is declared as an include in CMainframe, and the global is also in CMainframe, i cant include CMainframe in the child, cause it gives me a Linking error cause im going recursive in includes. So, can i just create a separate file (or use StdAfx?), create the global there, initialize it within CMainframe and include it in each class i want?
Do not declare global variables in any header files. Declare them in one, and only one cpp file, and any cpp file that needs to use them imports it using the extern keyword
// In MainFrame.cpp
...
#include "MainFrm.h"CMainFrame *g_MyGlobalMainFrame = NULL;
...int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;g\_MyGlobalMainFrame = this;
...
// In any other cpp file that needs access to g_MyGlobalMainFrame
#include "MainFrm.h"
extern CMainFrame *g_MyGlobalMainFrame...
CPUA 0x5041 Sonork 100.11743 Chicken Little "So it can now be written in stone as a testament to humanities achievments "PJ did Pi at CP"." Colin Davies Within you lies the power for good - Use it!
-
Do not declare global variables in any header files. Declare them in one, and only one cpp file, and any cpp file that needs to use them imports it using the extern keyword
// In MainFrame.cpp
...
#include "MainFrm.h"CMainFrame *g_MyGlobalMainFrame = NULL;
...int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;g\_MyGlobalMainFrame = this;
...
// In any other cpp file that needs access to g_MyGlobalMainFrame
#include "MainFrm.h"
extern CMainFrame *g_MyGlobalMainFrame...
CPUA 0x5041 Sonork 100.11743 Chicken Little "So it can now be written in stone as a testament to humanities achievments "PJ did Pi at CP"." Colin Davies Within you lies the power for good - Use it!
Thanks!! :-D