|
|
|
Getting started with MFCWe 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.
You will get something like this.
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.
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() 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.
|