4. VC++ GUI Tutorials

In this tutorial , basic VC++ Graphical User Interface (GUI) programming using Microsoft Foundation Class (MFC) Library is covered. Sample applications such as Hello World, Simple Interest and Matrix Grid will help you to familiarize with common frequently used MFC classes. Various main MFC Classes such as CWinApp, CDialog, CEdit, CDC, CView, CString, CFrameWnd, CDocument, CBrush, CPen, CRect, CPoint, CMenu, COleDataSource, CArray etc are illustrated in these tutorials. Windows event handlers and implementing messages maps are also illustrated in these tutorials. Please go through MSDN for a detailed explanation about all these MFC classes and their member functions. Here is a brief explanation about all these classes.

CWinApp: The CWinApp class is the base class from which you derive a Windows application object. An application object provides member functions for initializing your application (and each instance of it) and for running the application.

Each application that uses the Microsoft Foundation classes can only contain one object derived from CWinApp. This object is constructed when other C++ global objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the Microsoft Foundation Class Library. Declare your derived CWinApp object at the global level.

When you derive an application class from CWinApp, override the InitInstance member function to create your application's main window object ie InitInstance is the main entry point (visible) of the VC++ MFC application similar to main() function entry point of  C++.

In addition to the CWinApp member functions, the Microsoft Foundation Class Library provides the following global functions to access your CWinApp object and other global information:

  • AfxGetApp   Obtains a pointer to the CWinApp object.
  • AfxGetInstanceHandle   Obtains a handle to the current application instance.
  • AfxGetResourceHandle   Obtains a handle to the application's resources.
  • AfxGetAppName   Obtains a pointer to a string containing the application's name. Alternately, if you have a pointer to the CWinApp object, use m_pszExeName to get the application's name.

CDialog: The CDialog class is the base class used for displaying dialog boxes on the screen. Dialog boxes are of two types: modal and modeless. A modal dialog box must be closed by the user for the application to continue. A modeless dialog box allows the user to display the dialog box and return to another task without canceling or removing the dialog box.

A CDialog object is a combination of a dialog template and a CDialog-derived class. Use the dialog editor to create the dialog template and store it in a resource, then use the Add Class wizard to create a class derived from CDialog.

A dialog box, like any other window, receives messages from Windows. In a dialog box, you are particularly interested in handling notification messages from the dialog box's controls since that is how the user interacts with your dialog box. Use the Properties window to select which messages you wish to handle and it will add the appropriate message-map entries and message-handler member functions to the class for you. You only need to write application-specific code in the handler member functions.

CEdit: The CEdit class provides the functionality of a Windows edit control. An edit control is a rectangular child window in which the user can enter text.

CDC: The CDC class defines a class of device-context objects. The CDC object provides member functions for working with a device context, such as a display or printer, as well as members for working with a display context associated with the client area of a window.

Do all drawing through the member functions of a CDC object. The class provides member functions for device-context operations, working with drawing tools, type-safe graphics device interface (GDI) object selection, and working with colors and palettes. It also provides member functions for getting and setting drawing attributes, mapping, working with the view port, working with the window extent, converting coordinates, working with regions, clipping, drawing lines, and drawing simple shapes, ellipses, and polygons. Member functions are also provided for drawing text, working with fonts, using printer escapes, scrolling, and playing metafiles.

CFrameWnd: The CFrameWnd class provides the functionality of a Windows Single Document Interface (SDI) overlapped or pop-up frame window, along with members for managing the window.

To create a useful frame window for your application, derive a class from CFrameWnd. Add member variables to the derived class to store data specific to your application. Implement message-handler member functions and a message map in the derived class to specify what happens when messages are directed to the window.

CDocument: The CDocument class provides the basic functionality for user-defined document classes. A document represents the unit of data that the user typically opens with the File->Open command and saves with the File->Save command. CDocument supports standard operations such as creating, loading, and saving a document. The framework manipulates documents using the interface defined by CDocument.

CView: The CView class provides the basic functionality for user-defined view classes. A view is attached to a document and acts as an intermediary between the document and the user. The view renders an image of the document on the screen or printer and interprets user input as operations upon the document.

CBrush: Windows provides a variety of drawing tools to use in device contexts. It provides pens to draw lines, brushes to fill interiors, and fonts to draw text. The CBrush class encapsulates a Windows graphics device interface (GDI) brush. To use a CBrush object, construct a CBrush object and pass it to any CDC member function that requires a brush.

Brushes can be solid, hatched, or patterned.

CPen: The CPen class encapsulates a Windows graphics device interface (GDI) pen.

CRect: A CRect contains member variables that define the top-left and bottom-right points of a rectangle.

CPoint: The CPoint class is similar to the Windows POINT structure. It also includes member functions to manipulate CPoint and POINT structures.

CMenu: The CMenu class is an encapsulation of the Windows HMENU. It provides member functions for creating, tracking, updating, and destroying a menu.

COleDataSource: The COleDataSource class acts as a cache into which an application places the data that it will offer during data transfer operations, such as Clipboard or drag-and-drop operations. 

By the end of this session you will be able to write any type of GUI application which suits to everyday need. Click on the appropriate  link below to see the Video and the source. 



 Simple Interest Example





 Matrix grid part 1 | Drawing Grid and Menu Operations





 Matrix grid part 2 | Mouse Click event, Zooming and Scrolling





 Matrix grid part 3 | Edit Ctrl, Context menu and Saving Manipulations


Download the Source For Simple Interest Example

Download the Source For Matrix grid part 1

Download the Source For Matrix grid part 2

Download the Source For Matrix grid part 3


Site optimized for IE6 and above. Copyright © 2010. Site Developed Using KTS WebCloud