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. Document Class and Dialog Windows :: MFC

Document Class and Dialog Windows :: MFC

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestionannouncementlounge
3 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.
  • V Offline
    V Offline
    valikac
    wrote on last edited by
    #1

    Hi. I am working on a project that contains a formview class with multiple dialog windows visible. As the user make changes to each dialog box, I want to update the document class. I implemente a message solution. The dialog boxes update send messages to the view class. The view class update the document class. In general, the solution above is adequate. However, in this particular project, the dialog windows are visible in the formview. I would like to know is there other more elegant solutions? For example, is it possible to implement a GetDocument() function that returns a pointer to the document class inside the dialog boxes? As another example, how about passing the dialog boxes pointers to the document class? I do not want to implement these solution unless messages fail. Thus these solutions should be last resorts. Thanks, Kuphryn

    J 1 Reply Last reply
    0
    • V valikac

      Hi. I am working on a project that contains a formview class with multiple dialog windows visible. As the user make changes to each dialog box, I want to update the document class. I implemente a message solution. The dialog boxes update send messages to the view class. The view class update the document class. In general, the solution above is adequate. However, in this particular project, the dialog windows are visible in the formview. I would like to know is there other more elegant solutions? For example, is it possible to implement a GetDocument() function that returns a pointer to the document class inside the dialog boxes? As another example, how about passing the dialog boxes pointers to the document class? I do not want to implement these solution unless messages fail. Thus these solutions should be last resorts. Thanks, Kuphryn

      J Offline
      J Offline
      Jonathan Craig
      wrote on last edited by
      #2

      I'm sure there are a million answers to your question. But I would stick to the message solution. It is the best way for windows to communicate. I would not pass pointers to class objects around to be used. That could be potentially dangerous. If the WPARAM and LPARAM parameters seem to limiting remember you can allocate memory (or objects; classes, structs, etc.) and pass the pointer to another window through a message.

      if(IsWindow(hwnd))
      {
      CThing *pThing = new CThing;
      PostMessage(hwnd, WM_NEW_THING, 0, (LPARAM)pThing);
      }

      Just remember the receiver of the message is responible for deleting the memory.

      long CMyWnd::OnNewThing(WPARAM wParam, LPARAM lParam)
      {
      ASSERT(lParam);
      CThing *pThing = (CThing *)lParam;
      ...
      delete pThing;
      }

      One more thing, in the OnDestory method of the CWnd class make sure you have no more WM_NEW_THING messages in the queue. Their memory should be deleted if they are.

      MSG msg;
      while(::PeekMessage(&msg, (HWND)NULL, WM_NEW_THING, WM_NEW_THING, PM_REMOVE))
      {
      CThing *pThing = (CThing *)msg.lParam;
      delete pThing;
      }

      Hope this helps. Jonathan Craig www.mcw-tech.com

      V 1 Reply Last reply
      0
      • J Jonathan Craig

        I'm sure there are a million answers to your question. But I would stick to the message solution. It is the best way for windows to communicate. I would not pass pointers to class objects around to be used. That could be potentially dangerous. If the WPARAM and LPARAM parameters seem to limiting remember you can allocate memory (or objects; classes, structs, etc.) and pass the pointer to another window through a message.

        if(IsWindow(hwnd))
        {
        CThing *pThing = new CThing;
        PostMessage(hwnd, WM_NEW_THING, 0, (LPARAM)pThing);
        }

        Just remember the receiver of the message is responible for deleting the memory.

        long CMyWnd::OnNewThing(WPARAM wParam, LPARAM lParam)
        {
        ASSERT(lParam);
        CThing *pThing = (CThing *)lParam;
        ...
        delete pThing;
        }

        One more thing, in the OnDestory method of the CWnd class make sure you have no more WM_NEW_THING messages in the queue. Their memory should be deleted if they are.

        MSG msg;
        while(::PeekMessage(&msg, (HWND)NULL, WM_NEW_THING, WM_NEW_THING, PM_REMOVE))
        {
        CThing *pThing = (CThing *)msg.lParam;
        delete pThing;
        }

        Hope this helps. Jonathan Craig www.mcw-tech.com

        V Offline
        V Offline
        valikac
        wrote on last edited by
        #3

        Recommendation noted. Thanks, Kuphryn

        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