1
votes

Background

I am woefully inexperienced with MFC and C++.

I have a set of dialogs that all have a small section with the same set of controls and extremely similar code.

I would like to separate that small section of controls from all dialogs move the code from all the dialog classes into a single class.

Problem

I'm not sure how to go about it. All my ideas all seem to have their own problems because I am so inexperienced.

Could I make a super class these dialogs inherit from that creates the controls dynamically given an (x, y) and that hooks up all the connections and communicates the few specifics through virtual methods? The problem is I don't know the specifics:

  1. Where would the super class inherit from? (CWnd? CDialog?)

  2. Where would I create the controls in the super class? (OnInit? Constructor?)

  3. Where would I initialize the super class in its subclasses? (OnInit? Constructor?)

  4. Would I just have two message maps? One for the super class and one for the sub class?

Are there any other pitfalls I should watch out for?

1
Does your common controls in each dialog have different functionality depending on which dialog they appear or all does the same thing?Haja Maideen
They each do the same thing. The only difference is the variables they affect. For example a dimension would read a different variable for each different dialog.Saevax

1 Answers

1
votes

The small section that you want to reuse can be an ordinary modeless dialog, derived from CDialog. You can create its controls with the resource editor - just like any other dialog - so they won't have to be created dynamically. The trick is to turn off the dialog's titlebar style (in the resource editor) so it will not be visually apparent that this section is a separate dialog. It will blend right in with the parent dialog.

For each place you want to reuse this dialog just create it and place it on the parent dialog with (x, y) coordinates using SetWindowPos.