Tutorial 2: Using Classes and Inheritance
The program in the previous tutorial calls the CWinApp
class
directly. Normally, however, we would inherit from this class to have
more control over the type of CWinApp
objects we create.
This is an example of how we would derive a class from CWinApp
.
// A class that inherits from CWinApp.
// It is used to run the application's message loop.
class CSimpleApp : public CWinApp
{
public:
CSimpleApp() {}
virtual ~CSimpleApp() {}
virtual BOOL InitInstance();
private:
CView m_view;
};
BOOL CSimpleApp::InitInstance()
{
// Create the Window
m_view.Create();
return TRUE;
}
Notice that we override InitInstance to determine what happens when the
application is started. In this instance we create the window for the m_view
member variable. The m_view member variable is a CView object inherited from
CWnd. The code for CView is shown below.
// Add the Win32++\include directory to project's additional include directories.
#include "wxx_wincore.h"
// A class that inherits from CWnd. It is used to create the window.
class CView : public CWnd
{
public:
CView() {}
virtual void OnDestroy() { PostQuitMessage(0); } // Ends the program
virtual ~CView() {}
};
The CSimpleApp and CView classes are used in WinMain as follows.
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
// Start Win32++.
CSimpleApp myApp;
// Run the application.
return myApp.Run();
}
The source code for this tutorial is located within the Tutorial folder
of the software available from SourceForge at
http://sourceforge.net/projects/win32-framework.