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.
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:
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:
wsprintf(txt, "You have clicked on the form %d times", ++numClicks);
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?