The help you needed in C, C++, MFC
Complete tutorials with free source code
PDF Print E-mail

Custom dialogs

In this page, you can see how to create your own dialog box and display it from the main dialog. Here is your main dialog interface.

example application dialog

Add a CString variable to the static text field with text "you haven't opened the custom dialog yet", with a suitable name such as "name". You will not be able to do that unless you change its id to something else other than "IDC_STATIC". So, first you have to open the properties of the static text field and change its id to something like IDC_NAME. then you will be able to add a variable to it.

Now you have to add your custom dialog window. To do that, go to resource view. Then, expand the tree and right click on custom dialog.rc. The name may change depending on the project name you gave. From the menu, select "Add resource". Then you will get a dialog with various resource types. Click on "Dialog" and click the button "New". A new dialog will be inserted. Delete existing controls in that dialog and make it similar to the following one.

custom dialog

Now, as you gave a variable to a control, you have to assign a class for your dialog. Then, you can use your dialog in the same way as you used CFileDialog class to get a file open dialog in the previous example.

To add a class to your dialog, right click on the dialog and select "Add Class". You can give a name to the class such as "TestDlg". Keep the base class in CDialog and click ok.

Now, add a CString variable "name" to the edit field in our custom dialog. Also, add following line to the handler of OK button.

void TestDlg::OnBnClickedButton1()
{
OnOK();}

It should be clear what would happen when OK button is pressed. It will close our custom dialog.

Now you are done with your custom dialog. Now, what is left to do is using it. We have to display our custom dialog when the button "show custom dialog" is pressed in the main dialog. So, add the following code to the button handler.

void CcustomdialogDlg::OnBnClickedButton1()
{
TestDlg dialog;dialog.DoModal();name = dialog.name;UpdateData(FALSE);}

What we are doing here is simply creating a variable from the class we just created and using that variable, displaying the dialog by calling its DoModal() function. DoModal() will return when our custom dialog is closed. Then we assign the text user typed in the text field in the custom dialog to the static text field and updating the static text field, so the static text field in the main dialog will contain what user has typed.

If you try to compile the project now, it will give you a compiler error. Because we are using the class we created for our custom dialog (TestDlg), we have to include the header file where that class is defined to the top of the page with above code. The name of the header file is "TestDlg.h". Name may vary according to the class name you gave. After including the header, the includes should look like following.

// custom dialogDlg.cpp : implementation file
//


#include "stdafx.h"
#include "custom dialog.h"
#include "custom dialogDlg.h"
#include "TestDlg.h"

Now you should be able to compile and run the application.

Download the source code.