Home arrow C++ arrow Page 6 - Using MFC in C++ Part 1: A Basic Application Skeleton

Using MFC in C++ Part 1: A Basic Application Skeleton

Microsoft Foundation Classes allow C++ developers to write windows apps easier and faster. In this article, Mitch starts from scratch and digs deep into the MFC toolbox to provide you with all of the code and details to create a simple MFC based Win32 app.

Author Info:
By: Mitchell Harper
Rating: 5 stars5 stars5 stars5 stars5 stars / 79
November 26, 2001
  1. · Using MFC in C++ Part 1: A Basic Application Skeleton
  2. · Building the framework
  3. · Creating the framework classes
  4. · Adding the code to MyClass.cpp
  5. · Making sure our application runs
  6. · Responding to windows messages
  7. · Conclusion

print this article

Using MFC in C++ Part 1: A Basic Application Skeleton - Responding to windows messages
(Page 6 of 7 )

We’ve already talked about windows and messages. Windows sends a variety of messages to your application when it is running, including messages to repaint the window (WM_PAINT), handle a double click (WM_DBLCLK) and handle a mouse-wheel scroll (WM_MOUSEWHEEL). The values mentioned in brackets are the message ids that windows sends to your application. All windows messages start with WM_ and are encapsulated into the MFC class files through afxwin.h and other files included by afxwin.h.

Lets jump right in and enter some code to respond to a click from the left mouse button on our form. We will keep count of the number of clicks on the form, and when a user clicks on the form, the number of clicks will be displayed and incremented.

In MyClass.h, just under the CmainWin constructor line, enter the following line:

afx_msg void OnLButtonDown(UINT Flags, CPoint Point);

This is a simple function decleration that will override the derived virtual function OnLButtonDown. This function is called when the left button is down. It takes two parameters. The first contains an unsigned integer, which represents several flags for the click event. The second contains an MFC Cpoint structure. The Cpoint structure simply contains an x and y co-ordinate and several operator() member functions. It represents the point on the form where it was clicked in units relative to the window (pixels).

The afx_msg part of the declaration is implementation specific and signifies that the function is related to an MFC windows message. All MFC functions are prefixed with afx_msg.

For the function code, double click on MyClass.cpp. Just under the #include “MyClass.h” file, enter the following line:

UINT numClicks = 0;

This is just a simple global unsigned integer, which will allow us to keep a count of the number of times our form has been clicked. Next, we must tell windows that our application will respond to the left mouse button being pressed down. This is done by adding the following line between the BEGIN_MESSAGE_MAP… and END_MESSAGE_MAP… lines:


The ON_WM_LBUTTONDOWN() macro tells windows that our application will respond to the WM_LBUTTONDOWN message. There are several messages that our application can respond to and we must tell windows which ones we will provide implementation details for. To handle a WM_PAINT message (which is called when the window needs to be repainted), we would enter ON_WM_PAINT between the BEGIN_MESSAGE_MAP() and END_MESSAGE_MAP() lines.

Lastly, we will add the implementation code. Add the following code just above the CApp App; line in the MyClass.cpp file:

afx_msg void CMainWin::OnLButtonDown(UINT Flags, CPoint Point)


char txt[50];

wsprintf(txt, "You have clicked on the form %d times", ++numClicks);

CClientDC dc(this);

dc.TextOut(1, 1, txt);


The code for our OnLButtonDown function uses the numClicks variable to keep track of the number of times the form has been clicked. The wsprintf() function (The windows version of sprintf) creates the string that will be outputted onto our form.

Next, a device context is created. A device context is like an interrupt to a form and allows us to write on the form. Its parameter, this, tells windows that we are after a client device context (CclientDC) for the current window (CmainWin). The last line prints the text “You have clicked on the form [number of clicks] times” on our form at X,Y co-ordinates 1, 1.

Compile your app and click on the form. The text should change with the number of times the form has been clicked. Neat ey?

Our form once it has been clicked on 6 times
blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials