Download demo project - 14.5 Kb
Introduction
In this tutorial we are going to create a simple SDI program using AppWizard that displays the
last key pressed in the middle of it's window. I assume you have good knowledge of C++ and
polymorphism. I also assume that you have worked with Windows before and that you are rather
familiar with Windows programs.
1. Choosing The Project Type
The first step to creating a new project is to choose New from the
File menu. This can also be done using the CTRL + N keyboard shortcut.
Click on the "project" tab to get a list of the available project types. In our case we will
create an MFC program, so choose MFC "AppWizard (exe)". The AppWizard is a wizard that
will create one of 3 typical programs:
- A SDI program
- A MDI program
- A Dialog based program.
Enter the name of your program in the "Project name" field. In our example we will
call the project SDIPROG. Change the location if you wish and then press OK.

2. AppWizard - Step 1
Choose the project you wish to create, in our case "Single Document". This will create an
application that has a single viewing window within a main frame. Uncheck the
"Document/View architecture support" checkbox and press "Next". If you specify that you
want to use the Doc/View architecture then the wizard will create CDocument and
CView derived classes. The CDocument derived class is where all
data should be stored and processed, and the CView derived class is used to
display this data. In our case we will not be using this architecture, and will instead use
a simple CWnd derived class that will handle the data and presentation in one
place.

3. AppWizard - Step 2 - 3
Accept the defaults and press "Next"
4. AppWizard - Step 4 - 5
You can experiment with the settings in these dialogs, but in this tutorial you should just
press "Next".

5. AppWizard - Step 6
Press "Finish". A dialog box will popup informing you of the settings you have chosen.
Press OK.

6. The View
After creating the project, AppWizard will generate a number of classes. One of them is
the "View". The View is the part of the program that is not part of the Frame, which includes
the borders, the caption, the menu and the toolbars. AppWizard creates a class called CChildView
that encapsulates the view. With CChildView you perform operations on the view, and you receive
input sent to the view.

7. Drawing in The view
First thing we'll do is add a CString member variable named m_strText to
CChildView:
class CChildView : public CWnd
{
protected:
CString m_strText;
Then we need to initialize m_strText, we'll do so by adding a call to m_strText's
constructor inside CChildView's constructor:
CChildView::CChildView() : m_strText(_T("The character you entered was:"))
{
}
CChildView has a virtual member function named OnPaint. OnPaint
is called by Windows telling your program to draw/redraw the view. Among other things, this can
occur when your view is created, when the view gets "uncovered" by a window, when your window
moves or when your window's size changes. When AppWizard creates your program it adds the
following code to your OnPaint() function:
void CChildView::OnPaint()
{
CPaintDC dc(this);
}
dc Is a CPaintDC object. CPaintDC encapsulates a Device
Context. A Device Context refers to an output device (such as a video card or printer) and it's
device driver. A device context is used to display graphics and text in a window. One of the
member functions of CPaintDC is DrawText(). DrawText()
allows you to draw text in the Device Context, which in our case is the view.
DrawText() needs a rectangle area in which it should draw. We'll get the
view's rectangle by
using the GetClientRect function. Next we'll add the code that draws the contents
of m_strText in the view:
void CChildView::OnPaint()
{
CPaintDC dc(this);
CRect rect; GetClientRect(rect);
dc.DrawText(m_strText, rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
8. Receiving Input
When your view gets mouse/keyboard input, Windows will send a message to your view. To get
the message you need to create a message handler. You create a message handler using
ClassWizard:
Press CTRL + W to start ClassWizard. Select the "class info" tab and choose "Child Window" in
the "Message Filter" combobox.

Select the "message map" tab. Choose WM_CHAR from the "Messages" list, and
press the "Add Function" button. Press "Edit Code" to edit the newly created OnChar function.

OnChar is called when the user enters a character in your view.
The code created by ClassWizard:
void CChildView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CWnd::OnChar(nChar, nRepCnt, nFlags);
}
In OnChar we'll add the code to change m_strText and then have the view redraw:
void CChildView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
m_strText = _T("The character you entered was: ");
m_strText += (TCHAR)nChar;
Invalidate();
CWnd::OnChar(nChar, nRepCnt, nFlags);
}
Invalidate() tells your view to redraw itself, and eventually calls
OnPaint().
9. Building And Running
Building the program is the most complicated stage - You have to press F5 and then... Wait. :)
Now that we have passed the complex building stage, the program should start. Click on the view and start typing. The text should be drawn according to the character you entered.

Further Reading