Click here to Skip to main content
15,868,016 members
Articles / Desktop Programming / MFC

Creating Your First Windows Application

Rate me:
Please Sign up or sign in to vote.
4.25/5 (27 votes)
14 Jun 2000 317.2K   3.2K   73   23
A brief step-by-step tutorial that demonstrates creating an SDI based application that does not use the MFC Doc/View architecture

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 its 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:

  1. A SDI program
  2. A MDI program
  3. 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.

Image 1

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.

Image 2

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".

Image 3

5. AppWizard - Step 6

Press "Finish". A dialog box will popup informing you of the settings you have chosen. Press OK.

Image 4

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.

Image 5

7. Drawing in the View

First thing we'll do is add a CString member variable named m_strText to CChildView:

C++
// ChildView.h : interface of the CChildView class
//...
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:

C++
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:

C++
void CChildView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	
	// Do not call CWnd::OnPaint() for painting messages
}

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 its 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:

C++
void CChildView::OnPaint() 
{
	CPaintDC dc(this);    // device context for painting
	
	CRect rect;           // Create a CRect object
	GetClientRect(rect);  // Retrieve the view's rectangle into the rect object
	
	// Draw the text in the middle of the view
	dc.DrawText(m_strText, rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE); 
	
	// Do not call CWnd::OnPaint() for painting messages
}

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.

Image 6

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.

Image 7

OnChar is called when the user enters a character in your view. The code created by ClassWizard:

C++
void CChildView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	
	CWnd::OnChar(nChar, nRepCnt, nFlags);
}

In OnChar, we'll add the code to change m_strText and then have the view redraw:

C++
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.

Image 8

Further Reading

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Web Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionone question on OnChar()? Pin
jeansea5-Feb-09 3:40
jeansea5-Feb-09 3:40 
AnswerRe: one question on OnChar()? Pin
ganesa1920-Jan-11 2:49
ganesa1920-Jan-11 2:49 
QuestionMFC ClassWizard in Vis C++ .NET [modified] Pin
sphereboy27-Jun-06 4:13
sphereboy27-Jun-06 4:13 
GeneralVery helpfull for beginners but.. Pin
Najmul hassan17-Dec-05 22:25
Najmul hassan17-Dec-05 22:25 
QuestionSDI or MDI project type? Pin
krugger14-Jan-04 8:57
krugger14-Jan-04 8:57 
Generalwerd Pin
Anonymous12-Apr-03 5:05
Anonymous12-Apr-03 5:05 
Generalc++ Pin
GaneshRam12-Apr-02 15:17
GaneshRam12-Apr-02 15:17 
GeneralAppreciation Pin
SyedAbdulhaq30-Mar-02 22:43
SyedAbdulhaq30-Mar-02 22:43 
GeneralRe: Appreciation Pin
Anonymous28-Dec-02 5:20
Anonymous28-Dec-02 5:20 
Generaldownload tutorial Pin
17-Feb-02 5:06
suss17-Feb-02 5:06 
Generalm00 Pin
31-Aug-01 0:17
suss31-Aug-01 0:17 
GeneralImporting existing dialog interfaces into a new SDI application. Pin
Kimball M. Rudeen25-May-01 13:06
Kimball M. Rudeen25-May-01 13:06 
GeneralAdding a List Control Pin
Tony22-Sep-00 18:09
Tony22-Sep-00 18:09 
GeneralNice Pin
Syed Tahir Rasul9-Aug-00 0:23
sussSyed Tahir Rasul9-Aug-00 0:23 
GeneralGood tutorials..... Pin
Member 487926-Jun-00 15:27
Member 487926-Jun-00 15:27 
GeneralRe: Good tutorials..... Pin
6-Feb-02 6:40
suss6-Feb-02 6:40 
GeneralNeat little ditty, but... Pin
Mark Das21-Jun-00 12:30
sussMark Das21-Jun-00 12:30 
GeneralRe: Neat little ditty, but... Pin
Daniel Kopitchinski21-Jun-00 23:28
Daniel Kopitchinski21-Jun-00 23:28 
GeneralRe: Neat little ditty, but... Pin
Mark Turner11-Jul-00 0:32
Mark Turner11-Jul-00 0:32 
GeneralRe: Neat little ditty, but... Pin
Kerry Sanders20-Jan-01 6:18
Kerry Sanders20-Jan-01 6:18 
GeneralRe: Neat little ditty, but... Pin
Kerry Sanders20-Jan-01 6:11
Kerry Sanders20-Jan-01 6:11 
GeneralThanx a lot Pin
mas17-Jun-00 20:15
mas17-Jun-00 20:15 
GeneralRe: Thanx a lot Pin
Member 104829-Jun-00 16:42
Member 104829-Jun-00 16:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.