MFC: Getting Started with Visual C++
|
---|
HOME | Consulting | Design | Maintenance | Project | Testing | Training | Turnkey | Java | C++ | SQL | HTML | JavaScript |
© 2002 - 2018 All Rights Reserved Total Application Works
|
---|
<html> <head> <title>Example of a link </title> </head> <body> <a href="http://sumtotalz.com/TotalAppsWorks/GettingStarted.html"> MFC: Getting Started with Visual C++ </a> </table> </body> </html> |
---|
Download Source and see a screen shot |
This tutorial is intended for that person who is getting started with Microsoft's MFC. In order for you to be able to follow this tutorial, you will need Microsoft's Visual C++ compiler. It assumes that you have completed A basic first C++ Program tutorial. In addition, you should be familiar with the concepts of inheritance. A more detailed explanation of inheritance can be found at
In this tutorial, we will create an Hello World application, using MFC, and then in the next tutorial, we will create a simple application that demonstrates event handling. The following section will give a brief description of classes and inheritance. Below are some links to tutorials on MFC.
Let's consider the class of automobile, which contains the set of all automobiles. A sampling might be:
class Toyota : automobile { ... } class Avalon : Toyota { ... } |
Figure 1: Avalon derived from automobile |
---|
If you look in a dictionary, template is defined as
In C++, a template is a keyword that allows you to create generic functions and/or classes. Java has a keyword interface that is similar, but for our purposes, we will say they are not the same.
How does the term template/pattern apply? Remember that a template is a pattern; therefore, the application template for Hello World looks like:
// include application framework windows class definitions: #include <afxwin.h> #include "MyApp.h" /**************************************** * constructor initializes resources ***************************************/ MyApp::MyApp() { } /**************************************** * destructor frees resources ***************************************/ MyApp::~MyApp() { } class CHelloWorldApp : public CWinApp { public: virtual BOOL InitInstance() // override default CWinApp method { m_pMainWnd = new MyApp(); // instantiate MyApp m_pMainWnd->ShowWindow( m_nCmdShow ); // make frame visible m_pMainWnd->UpdateWindow(); // refresh frame window return true; // report success } } helloWorldApp; |
Figure 2: Hello World Application |
---|
// define our window based on the MFC frame window class MyApp : public CFrameWnd { public: MyApp(); // MyApp constructor virtual ~MyApp(); // free-up resources private: }; |
Figure 3: Hello World Application - Header file |
---|
1- class CHelloWorldApp : public CWinApp 2- { 3- public: 4- virtual BOOL InitInstance() // override default CWinApp method 5- { 6- m_pMainWnd = new MyApp(); // instantiate MyApp 7- . . . 8- } 9- 10- } helloWorldApp; // instantiate app |
Figure 4: Hello World Application - Main |
---|
Where do we go from here? The next step is to use the template to create a Hello World application. In the next section we will take this next step.
In the section, we will create a
Create( 0, // default CFrameWnd class "Hello World", // window title WS_OVERLAPPEDWINDOW, // full-featured frame window CRect( 100, 100, // initial position and 600, 400 ) ); // frame size |
Figure 5: MyApp frame window |
---|
pHello = new CStatic(); // create a static control pHello->Create( // create Windows control "This is a slight modification of the Hello World. " "\n \n" "Hello world from Ron Holland" , // text WS_CHILD | WS_VISIBLE | WS_BORDER // window styles | SS_CENTER, // static object styles CRect( 50, 50, 450, 150 ), // window coordinates this ); |
Figure 6: Hello world |
---|
Using the template and the code to create a frame and static window, we get the following header file:
/******************************************************* * File: MyApp.h *******************************************************/ // define our window based on the MFC frame window class MyApp : public CFrameWnd { public: MyApp(); // MyApp constructor virtual ~MyApp(); // free-up resources private: CStatic *pHello; // pointer to a static control };and the following .cpp file. /************************************************************* * File: helloWorld.cpp *************************************************************/ // include application framework windows class definitions: #include |
Figure 7: Hello world |
---|
AFXWIN - This is a header for the Microsoft Foundation Classes C++ library. It must be included in all MFC applications.
CFrameWnd - The CFrameWnd class encapsulates the functionality of a Windows single document interface (SDI) frame window. A frame window is a window that frames an application.
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.
CStatic - The CStatic class provides the functionality of a Windows static control. A static control is a control used to display text, to draw frames or lines separating other controls, or to display icons. A static control doesn't accept user input, but it can notify its parent of stylus taps if it's created with SS_NOTIFY style.
event handling - an event occurs when a mouse is clicked, double clicked, a button is clicked, a key pressed, etc. Event handling is the code that is called as a result of an event.
inheritance - is the ability to create a class that derives properties from one or more base classes.
initinstance - Override InitInstance to initialize each new instance of your application running under Windows. Typically, you override InitInstance to construct your main window object and set the CWinThread::m_pMainWnd data member to point to that window. m_pMainWnd - Holds a pointer to the application’s main window. SS_CENTER - Designates a simple rectangle and displays the given text centered in the rectangle. The text is formatted before it is displayed. Words that would extend past the end of a line are automatically wrapped to the beginning of the next centered line. Template - is a unique class or pattern that can be used to build other software code. WS_BORDER - Creates a window that has a border. WS_CHILD - Creates a child window. Cannot be used with the WS_POPUP style. WS_VISIBLE - Creates a window that is initially visible.
top
[HOME]
[Consulting]
[Design]
[Maintenance]
[Project]
[Testing]
[Training]
[Turnkey]
[Java]
[C++]
[SQL]
[HTML]
[JavaScript ] © 2002 - 2018 All
Rights Reserved Total Application Works