Customizing MFC dialog boxes

The MFC dialogs created with the Embed DLL wizard are fairly simplistic. You can customize them inside Microsoft Visual Studio by editing the CPP and H files and using the Class wizard. Note, however, that MSVS is a complex integrated development environment; if you are not proficient using MSVS, refer to the many online documents that describe how to use it.

The remainder of this section steps you through the process of customizing a MFC dialog created in the previous section with the Embed DLL wizard.

To customize an MFC dialog

The Embed DLL wizard creates a VSI.CPP file that contains the source C++ code for the DLL, as well as placeholders for simulation level functions and block level functions that you may use in your DL. This structure is passed to the VSMDIALOG.CPP function to display and allow editing of values in a dialog. A default structure is created with block parameters of three data types: Integer, Double, and String.

1.    Start Microsoft Visual Studio, if it is not already running, and open the project created in step 15 in the previous section.

2.    In the Solution Explorer window, open VSI.CPP.

3.    Add a second floating point double to the structure at line 16. Note that NewDialog1_INFO_STR changes from IFs to IFFs at line 21.

Graphical user interface, text, application

Description automatically generated

To properly save and restore block parameter values, a text string is encoded with data types of structure elements in an Info String macro. Each character of the string corresponds to a data type. Any changes to the block parameter structure requires corresponding changes to Info String. Info String tells Embed what block data must be saved in the VSM file. The Embed DLL wizard adds a brief description string:

// This string tells VisSim what structure elements to save to .vsm

// I=int, F=64 bit float, f=32 bit float, s=string, c=char, b=byt

Good practice: The data structure can contain non-persistent data that can be used for block execution but does not need to be saved in the VSM file. In this case, the block data structure must have all persistent data on the top with matching Info String. Nonpersistent data should be at the bottom of the block data structure past the Info String entries so that needless transient data is not saved to the VSM file.

4.    VSI.CPP contains block functions. In this example, the displayDLG base function is used. displayDLG with no suffix (line 89) is called at each time step to implement the simulation behavior of the block. In this sim function:

      Output 1 (line 91) = input 1 * mult0 // mult0 is integer param

      Output 2 (line 92) = input 2 * (mult1 + mult2) // mult1 & mult2 are float params

      Output 3 (line 93) = str1 // str1 is a string param

Text, application

Description automatically generated 

5.    Within the file, the functionnamePI function initializes the block parameters (displayDLGPI).Using this function, initialize the block parameters with default values for the dialog, as shown in the highlighted text below:

Graphical user interface, text

Description automatically generated 

6.    The basefunctionnamePC function displays and allows editing of block parameters in the dialog (displayDLGPC). In this function, the MFC dialog object dlg in line 126 is instantiated and block parameter values are assigned to dlg object members, as shown in the highlighted text below:


Description automatically generated with low confidence 

7.    The CVsmDialog class is defined in VSMDIALOG.H and contains dialog member elements. To add or change elements, invoke the Class Wizard from Visual Studio Project > Class Wizard (CTRL+SHIFT+ X) .

8.    Select the dialog class name.


9.    Switch to the Member Variables tab.

10.  For required Control IDs, add dialog class variables.


11.  In simple cases, the variable category should be Value. Enter the desired variable type and variable name.


12.  Click Finish.

Note: If you click Next, ignore options on that screen and press Finish.

13.  Alternatively, you can declare the variables in VSMDIALOG.H, as highlighted in the text below:

Graphical user interface, text, application

Description automatically generated

14.  Make sure you map your variables to the correct IDs.

a.    Double-click NEWDIALOG1.RC to see the sample dialog.

b.    Click on any input form (example – edit box)

c.     Verify or change the ID value under the Properties browser.


15.  The declared variables for the dialog in VSMDIALOG.H can be defined in VSMDIALOG.CPP under the public CVsmDialog class.

Graphical user interface, text, application

Description automatically generated

16.  Data exchange between dialog and defined variables is ensured with the DDX_Text() function, which lists all variables and their ID values within VSMDIALOG.CPP, as highlighted in the text below:

Graphical user interface, text, application

Description automatically generated 

17.  A clean build of the DLL file is shown below:

Graphical user interface, text, application, email

Description automatically generated