Subclassing Dialogs
-
Hey all, I've got to create 5 similar dialogs for a new app I'm writing. All 5 dialogs have the same 3 controls on them (a couple of CStatics and a CEdit control) in addition to a few controls that will be unique for each of the 5 dialogs. Is there any way to accomplish the following? - Create a base dialog (CBaseDialog), derive it from CDialog, add those 3 common controls to it. - Use that new CBaseDialog class as the base class for my 5 similar dialogs so that I don't have to copy/paste the 3 controls onto each of the 5 dialogs? I'm thinking that since the controls are created from the resource file, I might run into a problem with something like that... Thanks. -Mike -Mike Zinni "Don't take life too seriously, you'll never make it out alive" - Van Wilder
-
Hey all, I've got to create 5 similar dialogs for a new app I'm writing. All 5 dialogs have the same 3 controls on them (a couple of CStatics and a CEdit control) in addition to a few controls that will be unique for each of the 5 dialogs. Is there any way to accomplish the following? - Create a base dialog (CBaseDialog), derive it from CDialog, add those 3 common controls to it. - Use that new CBaseDialog class as the base class for my 5 similar dialogs so that I don't have to copy/paste the 3 controls onto each of the 5 dialogs? I'm thinking that since the controls are created from the resource file, I might run into a problem with something like that... Thanks. -Mike -Mike Zinni "Don't take life too seriously, you'll never make it out alive" - Van Wilder
Why not just host a modeless dialog box inside your main CBaseDialog?
CBaseDialog _base ; CCustomDialog _custom ; _base.SetChild( &_custom ) ; _base.DoModal() ;
...BOOL CBaseDialog::OnInitDialog() { BOOL bReturn = TRUE ; CDialog::OnInitDialog(); m_pDialog->Create( this ) ; // m_pDialog is set to &_custom // create a frame resource in your CBaseDialog to host the child // dialog CRect rFrame ; CWnd * pFrame = GetDlgItem(IDC_STATIC_FRAME) ; pFrame->GetWindowRect( rFrame ) ; ScreenToClient( rFrame ) ; m_pDialog->MoveWindow(rFrame, FALSE) ; // change bReturn to FALSE if you want to set the focus // to one of the child dialog items. return bReturn ; }
There is more to do such as chaining the child's DoDataExchange() in the CBaseDialog::DoDataExchange() and maintaining the dialog position in OnMouseMove() but that is a fairly trivial. -
Hey all, I've got to create 5 similar dialogs for a new app I'm writing. All 5 dialogs have the same 3 controls on them (a couple of CStatics and a CEdit control) in addition to a few controls that will be unique for each of the 5 dialogs. Is there any way to accomplish the following? - Create a base dialog (CBaseDialog), derive it from CDialog, add those 3 common controls to it. - Use that new CBaseDialog class as the base class for my 5 similar dialogs so that I don't have to copy/paste the 3 controls onto each of the 5 dialogs? I'm thinking that since the controls are created from the resource file, I might run into a problem with something like that... Thanks. -Mike -Mike Zinni "Don't take life too seriously, you'll never make it out alive" - Van Wilder
Yes, it is possible. Just make sure the common controls all have the same IDs. http://www.codeproject.com/dialog/dialogsubclass.asp[^]
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!