Solved

How to convert an existing MFC dialog into re-usable control?

Posted on 2014-04-17
1
432 Views
Last Modified: 2015-02-04
Hi,

I have a MFC dialog that has few controls on it (like web browser, buttons etc).
I want this MFC dialog to be reused by other dialogs in my project.

Like, if I can convert this into an active-x control or a custom control, I can just use the whole dialog and embed it onto another parent dialog.

Is there a way I can do this?

Thanks,
0
Comment
Question by:aravindgopaluni
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 40014310
Hi aravindgopaluni,

IMO building an ActiveX or a custom control makes sense when you plan to be able to use it in other projects without need to add/use the code, i.e. if you want to sell the control.

If this is not the case IMO easiest is to implement the control as a child dialog and load it at runtime in the wanted parent dialog.

To do so you'll have to implement a new CDialog-derived class and move all functionality which implements your current dialog to this new class (lets call it CMyControlDlg). Further you need to add a new Dialog to resources (in my sample below this uses IDD_MY_CONTROL as resource ID), where you'll have to following properties:
Border: None
Style: Child
Control: True

Next you should override the functions OnOk and OnCancel qnd remove the base-class calls in order to avoid the child dialog closes when these keys are used to close the parent dialog.

And you should implement a constructer like this:
class CMyControlDlg : public CDialog
{
public:
 CMyControlDlg::CMyControlDlg( CWnd* pParent = NULL )
 : CDialog( IDD_MY_CONTROL, pParent )
 {}
};

Open in new window

To use this like a control in a parent dialog you have to add a pointer as member to the parent dialog's class, instantiate it and create the child dialog and to add some code to handle the child control with some relevant messages like WM_SIZE and WM_DESTROY.

To simplify using the child dialog like a control I would suggest to implement another CDialog derived class (lets call it CMyControlParent) which acts as a wrapper and is later used as base class for parent dialogs.

The implementation of CMyControlParent could look somehow like this (only the relevant parts):
class CMyControlParent
{
 CMyControlDlg* m_pChild ;
public:
 ...
 int OnCreate(LPCREATESTRUCT lpCreateStruct);
 int OnDestroy();
 void OnSize(UINT nType, int cx, int cy); // optional (see comment below)
 void DoDataExchange(CDataExchange* pDX); // optional (see comment below)
};

int CMyControlParent::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
 if (CDialog::OnCreate(lpCreateStruct) == -1)
  return -1;

 m_pChild = new CMyControlDlg();
 m_pChild->Create(IDD_MY_CONTROL, this);
 m_pChild->ShowWindow(SW_SHOW);

 return 0;
}

int CMyControlParent::OnDestroy() 
{
 m_pChild->DestroyWindow();
 delete m_pChild;
}

void CMyControlParent::OnSize(UINT nType, int cx, int cy) 
{
 CDialog::OnSize(nType, cx, cy);

 CRect rect;
 GetClientRect(&rect);

 // some how calculate correct position and size here, this is just a dumb sample
 rect.Deflate(2, 2); 
 rect.bottom -= 20;

 m_pChild->MoveWindow(&rect);
}

void CMyControlParent::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 m_pChild->DoDataExchange(pDX);
}

Open in new window

Comments: The OnSize is only needed in resizable parent dialog, if you don't use this you should position the child dialog in a similar way in OnCreate. The DoDataExchange is only needed if CMyControlDlg uses DoDataExchange too.

The code doesn't include needed entries in the message map. As long as you use ClassWizard to override the mentioned functions/message handlers they're added autmatically, otherwise you'll have to add them manually. If you don't know how to do this please tell.

You should implement functions to pass data from/to the child dialog to the parent dialog as needed to avoid parent dialogs directly access member variables of the child dialog (at least just because it's always a good idea).

This way it should be enough to implement a new parent dialog by simply deriving it from CMyControlParent (take care to change all occurances of 'CDialog' in the new dialog to 'CMyControlParent').

Of course this can be still improved, i.e. I would implement a virtual function which is used to calculate position and size of the child control. This can be overridden in new parent dialogs if specific position is needed.

I hope this helps you getting an idea about how to do it this way. Doesn't look simple, but I promise building/using an ActiveX or a custom control is more complicated (allthough, as told above, offers possibilities which are not possible with the above suggestion).

Hope that helps,

ZOPPO


PS: The code is not tested, I just wrote it from the scratch here, so it may contain errors and/or it could be I missed something. If you have problems with it or get stuck somewhere please tell ...
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why isn't object file created? 6 105
HTTPSendRequest with WinINet delays on first call 11 119
Math Equation 23 147
troubleshoot a python script 8 113
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question