Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. AfxGetMainWnd while creating CMainFrame

AfxGetMainWnd while creating CMainFrame

Scheduled Pinned Locked Moved C / C++ / MFC
c++databasehelptutorial
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Miguel Lopes
    wrote on last edited by
    #1

    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

    PJ ArendsP 1 Reply Last reply
    0
    • M Miguel Lopes

      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

      PJ ArendsP Offline
      PJ ArendsP Offline
      PJ Arends
      wrote on last edited by
      #2

      use the hwndParent parameter of the supplied CREATESTRUCT 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!

      Within you lies the power for good; Use it!

      M 1 Reply Last reply
      0
      • PJ ArendsP PJ Arends

        use the hwndParent parameter of the supplied CREATESTRUCT 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!

        M Offline
        M Offline
        Miguel Lopes
        wrote on last edited by
        #3

        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?

        PJ ArendsP 1 Reply Last reply
        0
        • M Miguel Lopes

          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?

          PJ ArendsP Offline
          PJ ArendsP Offline
          PJ Arends
          wrote on last edited by
          #4

          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!

          Within you lies the power for good; Use it!

          M 1 Reply Last reply
          0
          • PJ ArendsP PJ Arends

            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!

            M Offline
            M Offline
            Miguel Lopes
            wrote on last edited by
            #5

            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?

            PJ ArendsP 1 Reply Last reply
            0
            • M Miguel Lopes

              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?

              PJ ArendsP Offline
              PJ ArendsP Offline
              PJ Arends
              wrote on last edited by
              #6

              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!

              Within you lies the power for good; Use it!

              M 1 Reply Last reply
              0
              • PJ ArendsP PJ Arends

                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!

                M Offline
                M Offline
                Miguel Lopes
                wrote on last edited by
                #7

                Thanks!! :-D

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups