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