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. Only if WTL had AfxGetMainWnd

Only if WTL had AfxGetMainWnd

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
2 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
    Mike NET
    wrote on last edited by
    #1

    In MFC there is an easy to get access to CMainFrame and call its methods from anywhere in the application. Just by simply doing this... CMainFrame pFrame = (CMainFrame*)AfxGetMainWnd(); pFrame->MyFunction(...); Although in WTL there is method GetTopLevelWindow() that can be used to get access to Top Application Window, but there seems to be no way to cast the return CWindow type to CMainFrame. Take for instance the following... CMainFrame frm = (CMainFrame)GetTopLevelWindow(); frm.MyFunction(...); ... will not work, as there seems to be no way to cast ATL::CWindow to CMainFrame. THE QUESTION!! How would one go about calling CMainFrame method from anywhere in the WTL application (say from distant dialog box)? Without using global variable to store pointer to CMainFrame? Is there such way? Or am I just dreaming :) Thanks in advance Mike M

    P 1 Reply Last reply
    0
    • M Mike NET

      In MFC there is an easy to get access to CMainFrame and call its methods from anywhere in the application. Just by simply doing this... CMainFrame pFrame = (CMainFrame*)AfxGetMainWnd(); pFrame->MyFunction(...); Although in WTL there is method GetTopLevelWindow() that can be used to get access to Top Application Window, but there seems to be no way to cast the return CWindow type to CMainFrame. Take for instance the following... CMainFrame frm = (CMainFrame)GetTopLevelWindow(); frm.MyFunction(...); ... will not work, as there seems to be no way to cast ATL::CWindow to CMainFrame. THE QUESTION!! How would one go about calling CMainFrame method from anywhere in the WTL application (say from distant dialog box)? Without using global variable to store pointer to CMainFrame? Is there such way? Or am I just dreaming :) Thanks in advance Mike M

      P Offline
      P Offline
      Paul M Watt
      wrote on last edited by
      #2

      Mike.NET wrote: Is there such way? Or am I just dreaming But there are things that you can do to get around this. When you call this in MFC: CMainFrame *pFrame = (CMainFrame*)AfxGetMainWnd(); it is returning a pointer to the window that is stored as a member variable inside of the app object. You could either mimick this behaviour by creating your own app module that derives from CAppModule, and stores this member variable. Since GetTopLevelWindow only returns a HWND there is no way to cast this handle to a CWindow object without creating a completely new instance of the object. The next best thing that you could do is store a pointer to the CWindow, (or CMainFrame) object in the user data field of the window. That way you could do this to get the pointer that you are interested in:

      HWND hWnd = GetTopLevelWindow();
      CMainFrame *mainFrm = dynamic_cast(::GetWindowLong(hWnd, GWL_USERDATA);
      if (NULL == mainFrm)
      {
      // This window is not of type CMainFrame
      // Do error handling, or dynamic_cast to a CWindow if you are interested.
      }
      // This would now be a valid call through your pointer.
      mainFrm->MyFunction(...);

      Good Luck!


      Build a man a fire, and he will be warm for a day
      Light a man on fire, and he will be warm for the rest of his life!

      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