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. Creating child windows in PreSubclassWindow

Creating child windows in PreSubclassWindow

Scheduled Pinned Locked Moved C / C++ / MFC
debuggingquestion
4 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.
  • G Offline
    G Offline
    Garth Watkins
    wrote on last edited by
    #1

    Hi I have a control that can either be created dynamically (using CWnd::Create()) or is created directly by windows when placed on a dialog using the dialog editor. In both cases PreSubclassWindow is called by the framework, which I then use to initialize the control. I don't use OnCreate to do the intialization as OnCreate is not called when the control sits inside a dialog. My Initialzation function which is called from PreSubclassWindow creates other child controls used by my control. Everything works out well when the control is created as part of a dialog control, but everything seems to fall apart when creating my control dynamically using CMyControl::Create(...); Having stepped through the creation code, windows always calls CWnd::Attach twice when creating dynamically, with the second call to Attach causing the debug assertion as the controls window is already attached the first time around. Anyone got any ideas about whats going here whith a pssible solution? Thanks Garth

    G 1 Reply Last reply
    0
    • G Garth Watkins

      Hi I have a control that can either be created dynamically (using CWnd::Create()) or is created directly by windows when placed on a dialog using the dialog editor. In both cases PreSubclassWindow is called by the framework, which I then use to initialize the control. I don't use OnCreate to do the intialization as OnCreate is not called when the control sits inside a dialog. My Initialzation function which is called from PreSubclassWindow creates other child controls used by my control. Everything works out well when the control is created as part of a dialog control, but everything seems to fall apart when creating my control dynamically using CMyControl::Create(...); Having stepped through the creation code, windows always calls CWnd::Attach twice when creating dynamically, with the second call to Attach causing the debug assertion as the controls window is already attached the first time around. Anyone got any ideas about whats going here whith a pssible solution? Thanks Garth

      G Offline
      G Offline
      Garth Watkins
      wrote on last edited by
      #2

      Hi Garth The question you pose is an interesting one. In fact all the questions you've asked are quite amazing. You're either incredibly clever or supremely (?) stupid as no one on this forum has ever answered you're questions. Anyway let me have a crack at it. Create a method, call it say, Initialize() which creates all the child windows etc. Override Create a method CMyControl::Create() which clients must call when creating your control dynamically. Add a BOOL property (member variable) to the control, call it say 'm_bIsDynCreate', and set it to FALSE in constructor of the control. Now in your custom Create() function, set this variable to TRUE, call the base Create method e.g. CWnd::Create(...) and then call your intialize method. In your Overriden PreSubclass method, test 'm_bIsDynCreate'. If it is FALSE, call Initialize(..), otherwise don't, as it will have already been called when creating the control dynamically. When called as part of the creating cycle of a dialog, it will be called in PreSubclass window. Here is some source code for your perusal. BOOL CGarthsCtrl::Create(const RECT & rect,CWnd* pParentWnd,UINT nID,DWORD dwStyle) { m_bIsDynCreate=TRUE; if(!CWnd::Create("GARTHSCTRL",NULL,dwStyle|CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW,rect,pParentWnd,nID)) return FALSE; Initialize(); return TRUE; } void CGarthsCtrl::PreSubclassWindow() { CWnd::PreSubclassWindow(); if(m_bIsDynCreate==FALSE) Initialize(); } Funnily enough, Garth, I was experiencing just the same problem this afternoon. Talk about coincidences. Anyway I hope this helps you somewhat. Regards Garth

      G 1 Reply Last reply
      0
      • G Garth Watkins

        Hi Garth The question you pose is an interesting one. In fact all the questions you've asked are quite amazing. You're either incredibly clever or supremely (?) stupid as no one on this forum has ever answered you're questions. Anyway let me have a crack at it. Create a method, call it say, Initialize() which creates all the child windows etc. Override Create a method CMyControl::Create() which clients must call when creating your control dynamically. Add a BOOL property (member variable) to the control, call it say 'm_bIsDynCreate', and set it to FALSE in constructor of the control. Now in your custom Create() function, set this variable to TRUE, call the base Create method e.g. CWnd::Create(...) and then call your intialize method. In your Overriden PreSubclass method, test 'm_bIsDynCreate'. If it is FALSE, call Initialize(..), otherwise don't, as it will have already been called when creating the control dynamically. When called as part of the creating cycle of a dialog, it will be called in PreSubclass window. Here is some source code for your perusal. BOOL CGarthsCtrl::Create(const RECT & rect,CWnd* pParentWnd,UINT nID,DWORD dwStyle) { m_bIsDynCreate=TRUE; if(!CWnd::Create("GARTHSCTRL",NULL,dwStyle|CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW,rect,pParentWnd,nID)) return FALSE; Initialize(); return TRUE; } void CGarthsCtrl::PreSubclassWindow() { CWnd::PreSubclassWindow(); if(m_bIsDynCreate==FALSE) Initialize(); } Funnily enough, Garth, I was experiencing just the same problem this afternoon. Talk about coincidences. Anyway I hope this helps you somewhat. Regards Garth

        G Offline
        G Offline
        Garth Watkins
        wrote on last edited by
        #3

        Thanks Garth You're solution is, to say the least, very elegant and masterfull. Excellent. It's solved all my problems. (I'm still having a cash flow problem though :() Regards Garth

        T 1 Reply Last reply
        0
        • G Garth Watkins

          Thanks Garth You're solution is, to say the least, very elegant and masterfull. Excellent. It's solved all my problems. (I'm still having a cash flow problem though :() Regards Garth

          T Offline
          T Offline
          ThatsAlok
          wrote on last edited by
          #4

          Garth Watkins wrote: You're solution is, to say the least, very elegant and masterfull. Excellent. It's solved all my problems There is No Doubt your are Genius :)


          "I Think this Will Help" [Vote One Here,.....]

          visit me at http://www.thisisalok.tk
          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