C++
  Home arrow C++ arrow Page 6 - Using MFC in C++ Part 1: A Basic Applicati...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Using MFC in C++ Part 1: A Basic Application Skeleton
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 54
    2001-11-26

    Table of Contents:
  • Using MFC in C++ Part 1: A Basic Application Skeleton
  • Building the framework
  • Creating the framework classes
  • Adding the code to MyClass.cpp
  • Making sure our application runs
  • Responding to windows messages
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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:

    ON_WM_LBUTTONDOWN()

    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

    More C++ Articles
    More By Mitchell Harper


     

    C++ ARTICLES

    - Paths and Files
    - Directories in C++
    - Focusing on C++ Files
    - Const Correctness in C++
    - Manipulating Streams and Files with C++
    - Streams and Files
    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT