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

Getting started with MFC

We will create a simple dialog based application using Visual Studio 2008 to get familiarize with MFC and Visual Studio. The interface in Visual Studio 2005 is also similar to this.

  • Open Visual Studio and select File->New->Project. You will get a dialog box asking which type of an application you are going to build. Select Visual C++->MFC->MFC Application as project type. Type a name for your application like "test" and select a folder where you need to create your new project and click OK.
  • In the next dialog, you will see some information about the application you are going to create. Click next.
  • Next Dialog will ask you some settings for your application. Select Dialog based in Application type. Also, select Use MFC in static library if you need to run the application in another machine where MFC is not available. If you select to use MFC in dynamic library, you will not be able to run the application exe file in another computer where Visual Studio or MFC is not installed. Click next.
  • Next dialog will ask you the styles for the dialog box you will get for your application. Select the items you need and click next.
  • Click Finish in the next dialog box, with default settings.

You will get something like this.

Visual Studio Main Window

You can see the dialog box created for you by the wizard in the middle. The pane in the right side shows the solution explorer, which shows the source files and other files belong to the project. The view in this pane can be changed by the tabs in the bottom. The most useful views in this pane are solution explore, class view and resource view. If these views are not there, you can get them from View menu. Close other views and keep only those 3 views.

In the bottom, you can see the output window, which give you information when you build, compile the project, such as compiling errors or the status of compilation. I recommend you to drag this window to the middle so it will be another tab there, and select the tab with your dialog box. This will give more room for your code and output window tab will automatically brought to top when you need it, when you are compiling.

In the right side, you can see a pane called Toolbox with a list of various GUI elements. These are known as controls. You can drag them to your dialog and use them.

Now, let us see how to add code to our application. Delete all 3 controls in the dialog box, 2 buttons and the static text. Then drag a new button and edit control to the dialog form the Toolbox. Drag a static text control also. Now your dialog should look like the following.

dialog box

Now, you need to add variable to the edit control we added, so what the user types there is stored in that variable. Right click the edit control and select Add Variable. In the dialog box you get, in the category field, change type to value from control. That means the variable we are going to create stores the value in that text field. Other type, control, is used to do various actions of the text box such as showing, hiding, disabling the it. Keep variable type as CString and type "name" in the variable name field. click OK.

Then, double click the button in your dialog box. You will be taken to the function which is called when the user clicks the button. So, you need to place the code which should be executed when the user clicks the button there.

We will add some code there such that if the user types "Joe" in the text box and clicks the button, he will get a message saying "hello michel". Place the following code in that function.

void CtestDlg::OnBnClickedButton1()
{
UpdateData(TRUE);CString message=TEXT("hello ");message.Append(name);MessageBox(message);}

The first line in the function transfers what the user has typed in the edit field to our variable "name". This function updates all the variables attached with the controls, and we have only one here. If you send FALSE to the function UpdateData, the data is transferred in the reverse direction, from the variables to corresponding controls.

Then in second and third lines, we have declared a CString variable and put the message there, which you should be able to understand easily. We have appended what the user has typed which is stored in the variable "name" to the string "hello ". In the third line, the MessageBox shows the message to the user.

Now, form the main menu, select Build->Build Solution. Then the output window will appear and tell that the application compiled successfully.

Go to the location where you selected to save the project, in the first step of creating the project. There will be a folder called Debug inside the project folder. In that folder, you will find the executable of your application, test.exe. Double click it and you will see your small application.

You can download sample program source code here.

Here are some tips which will be useful for beginners.

  • If you closed the window with your dialog box in Visual studio, you can find it from the resource view pane in the right -> test.rc->Dialog->IDD_TEST_DIALOG
  • You can easily edit the styles of the dialog such as caption, maximize, minimize buttons etc from the dialog properties. To get Dialog properties, just right click the dialog and choose Properties.
  • If you exit Visual Studio, you can open the project again by double clicking test.sln file in the project folder.
  • Study the various views in the pane in the right side of the window such as resource view, class view and solution explorer.