02. MFC_One_Step_At_A_Time vc++.pdf
-
Upload
selvaraj-villy -
Category
Documents
-
view
212 -
download
0
Transcript of 02. MFC_One_Step_At_A_Time vc++.pdf
7/21/2019 02. MFC_One_Step_At_A_Time vc++.pdf
http://slidepdf.com/reader/full/02-mfconestepatatime-vcpdf 1/19
MFC One step At A TimeBy: Brandon Sean Fogerty
1
MFC One Step At A Time
By: Brandon Fogerty
7/21/2019 02. MFC_One_Step_At_A_Time vc++.pdf
http://slidepdf.com/reader/full/02-mfconestepatatime-vcpdf 2/19
MFC One step At A TimeBy: Brandon Sean Fogerty
2
Development Environment
Operating System: Windows XP/NT
Development Studio: Microsoft .Net Visual C++ 2005
7/21/2019 02. MFC_One_Step_At_A_Time vc++.pdf
http://slidepdf.com/reader/full/02-mfconestepatatime-vcpdf 3/19
MFC One step At A TimeBy: Brandon Sean Fogerty
3
Step 1:
Fire up Visual Studio. Then go to File->New->Project
Now we will be creating a new MFC Application project. Click on Visual
Studio C++ Projects in the Project Type directory List Box. From there,
choose the MFC Application Template. In the Name textbox, call your
project MFCTest or some other MFC related name.
7/21/2019 02. MFC_One_Step_At_A_Time vc++.pdf
http://slidepdf.com/reader/full/02-mfconestepatatime-vcpdf 4/19
MFC One step At A TimeBy: Brandon Sean Fogerty
4
You will see a new window similar to this one.
7/21/2019 02. MFC_One_Step_At_A_Time vc++.pdf
http://slidepdf.com/reader/full/02-mfconestepatatime-vcpdf 5/19
MFC One step At A TimeBy: Brandon Sean Fogerty
5
Select Application Type. We will want to change the MFC Application’s
properties a little bit so that we can strip away the extra code that we don’t
need for this tutorial.
We will make this project a Dialog based application to keep things simple
with one window and few controls. We will also add the MFC classes as a
static library so that the distribution of our application will be simple.
7/21/2019 02. MFC_One_Step_At_A_Time vc++.pdf
http://slidepdf.com/reader/full/02-mfconestepatatime-vcpdf 6/19
MFC One step At A TimeBy: Brandon Sean Fogerty
6
You will see a new window design pane. Here we will design our
Application’s graphical user interface.
For our simple MFC Application, we will make an button widget and a
textbox widget. Once the button is clicked, the text box will say
“Hello World From The MFC World!”
7/21/2019 02. MFC_One_Step_At_A_Time vc++.pdf
http://slidepdf.com/reader/full/02-mfconestepatatime-vcpdf 7/19
MFC One step At A TimeBy: Brandon Sean Fogerty
7
Click on the toolbar tab to the left side of the screen. After doing this, you
will see the Toolbox Window Pane.
7/21/2019 02. MFC_One_Step_At_A_Time vc++.pdf
http://slidepdf.com/reader/full/02-mfconestepatatime-vcpdf 8/19
MFC One step At A TimeBy: Brandon Sean Fogerty
8
Select and draw a Button on our Application’s window.
7/21/2019 02. MFC_One_Step_At_A_Time vc++.pdf
http://slidepdf.com/reader/full/02-mfconestepatatime-vcpdf 9/19
MFC One step At A TimeBy: Brandon Sean Fogerty
9
Now we will change the caption of our Button’s Caption name.
In other words, change the text on the button from
“Button1” to “Click Me!”.
Click on the Button and in the Properties window, scroll to the Caption field
and change the text from “Button1” to “Click Me!”
7/21/2019 02. MFC_One_Step_At_A_Time vc++.pdf
http://slidepdf.com/reader/full/02-mfconestepatatime-vcpdf 10/19
MFC One step At A TimeBy: Brandon Sean Fogerty
10
Now we need to look and remember the Buttons Macro ID. Remember this
Macro for the Button because we will need it when we write code for the
button later.
In this situation, the button’s macro id is “IDC_BUTTON1”. Again,
remember this macro id because we will need it for later.
7/21/2019 02. MFC_One_Step_At_A_Time vc++.pdf
http://slidepdf.com/reader/full/02-mfconestepatatime-vcpdf 11/19
MFC One step At A TimeBy: Brandon Sean Fogerty
11
Now we will add a text box to our MFC Application.
Again, choose the toolbox pane and select the edit control. Then draw the
textbox onto the Application’s window.
7/21/2019 02. MFC_One_Step_At_A_Time vc++.pdf
http://slidepdf.com/reader/full/02-mfconestepatatime-vcpdf 12/19
MFC One step At A TimeBy: Brandon Sean Fogerty
12
Don’t worry about the “Sample edit Text” in our edit control. It won’t be
there during Runtime of our Application. However we need to remember
the name of our textbox’s macro id as we did with our button. Select our
textbox and look at the properties pane.
The name of our textbox’s macro is “IDC_EDIT1”. Again, remember this
macro id because we will need in later on in our code.
Now select the Class View tab above the properties pane. In CMFCTestDlg,
choose the DoInitDialog(void) method. This method is in control of
initializing our application.
7/21/2019 02. MFC_One_Step_At_A_Time vc++.pdf
http://slidepdf.com/reader/full/02-mfconestepatatime-vcpdf 13/19
MFC One step At A TimeBy: Brandon Sean Fogerty
13
We will need to create two new objects that will give us access to our button
and textbox.
Now we will bind our two new objects to the button and textbox that wedrew on our form. So inside the CMFCTestDlg::OnInitDialog() method,
write the following code.
button1 = (CButton *) GetDlgItem(IDC_BUTTON1);
textBox1 = (CEdit *) GetDlgItem(IDC_EDIT1);
Now we can access these controls directly and manipulate the textbox’s and
button’s properties at runtime.
7/21/2019 02. MFC_One_Step_At_A_Time vc++.pdf
http://slidepdf.com/reader/full/02-mfconestepatatime-vcpdf 14/19
MFC One step At A TimeBy: Brandon Sean Fogerty
14
Now go we will want to add an event to our button so that we know when it
has been clicked. So to go back to our window design view, click on
“Resource View” which is next to the right of “Class View”. Then Select
Dialog->IDD_MFCTEST_DIALOG.
7/21/2019 02. MFC_One_Step_At_A_Time vc++.pdf
http://slidepdf.com/reader/full/02-mfconestepatatime-vcpdf 15/19
MFC One step At A TimeBy: Brandon Sean Fogerty
15
Now right click on the button we created and select “Add Event Handler”.
7/21/2019 02. MFC_One_Step_At_A_Time vc++.pdf
http://slidepdf.com/reader/full/02-mfconestepatatime-vcpdf 16/19
MFC One step At A TimeBy: Brandon Sean Fogerty
16
Choose BN_CLICKED in the Message type. Then click Edit Code.
7/21/2019 02. MFC_One_Step_At_A_Time vc++.pdf
http://slidepdf.com/reader/full/02-mfconestepatatime-vcpdf 17/19
MFC One step At A TimeBy: Brandon Sean Fogerty
17
Then type the following code to change the textbox’s text to our message
and change our button’s caption as well.
textBox1->SetWindowText(_T("Hello World From MFC!"));
button1->SetWindowText(_T("You Clicked me!"));
This code will now change the properties of our controls at runtime. Now
we will compile and build our program to test it.
Go to Build->Build Solution.
7/21/2019 02. MFC_One_Step_At_A_Time vc++.pdf
http://slidepdf.com/reader/full/02-mfconestepatatime-vcpdf 18/19
MFC One step At A TimeBy: Brandon Sean Fogerty
18
Now hopefully our program will have been compiled and linked
successfully. It is now time to run our program.
Go to Debug->Start
Now we see our program. It looks like the following.
7/21/2019 02. MFC_One_Step_At_A_Time vc++.pdf
http://slidepdf.com/reader/full/02-mfconestepatatime-vcpdf 19/19
MFC One step At A TimeBy: Brandon Sean Fogerty
19
Now to see our code work, click on the “Click Me!” button that we made
and see what happens! You should see the following.
And that’s it! We have now successfully wrote an MFC Application.
For further information on MFC programming, I would suggest reading,
MS Press - Programming Windows with MFC - 2nd Ed
I hope that you enjoyed this tutorial. For more tutorials, please visit my
tutorials page at www.brandonfogerty.com.
-- Brandon Fogerty
GOD Bless you Always!!!!