Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
SSW_8_11.doc
Скачиваний:
2
Добавлен:
01.05.2025
Размер:
5 Мб
Скачать
☆

The application framework creation

To built the application on base of architecture of creation and view of documents, there is need:

  1. To origin the classes, aimed to create the application, main window, document, application and view.

  2. To permit the dynamic object creation for main window classes, document and view.

  3. To create the document framework, united the classes of main window, document and view.

  4. To re-define the required functions, for instance: CObject::Serialize() or CView::OnDraw()

The main window and application classes creation

The class of main window contains less functions then application class, which supports the serialization mechanism.

The application class is originated from CWinApp class, as earlier.

For the main window class (originated from CFrameWnd) there is provided the dynamic object creation. So that description contains DECLARE_DYNCREATE(). That class encapsulates all the objects of control elements. Listing 8.1 demonstrates such main window class.

Listing 8.1 The example of main window class in Document / View concept

Class CMainWin : public CFrameWnd

{DECLARE_DYNCREATE(CMainWin)

// the private section of class

public:

CMainWin();

// the opened section of class

DECLARE_MESSAGE_MAP()

};

The documents class creation

The own document class is originated from CDocument class. It contains DECLARE_DYNCREATE() to create objects dynamically. Also it’s included the message map for all the commands of documents state changing. It provides the documents data procession (in general case).

Some functions of CDocument are re-defined:

For example: On menu item “New Document”, there will be at first called the corresponding function virtual BOOL CDocument::OnNewDocument(); of father class.

Often the function Serialize() is also re-defined.

The example of such user-defined document class id in Listing 8.2

Listing 8.2 The example of document class in Document / View concept

Class CSampleDoc : public CDocument

{DECLARE_DYNCREATE(CSampleDoc)

// the private section of class

public:

CSampleDoc();

BOOL OnNewDocument(); // the initiation of every new document

void Serialize(Carchive &arch); // the data reception or writing

// the opened section of class

DECLARE_MESSAGE_MAP()

};

The view class creation

The view class controls the display of document. The view overlays the main window, but that controls the main functions on work with window – scaling and minimization. The view class is originated from CView or daughter class (f.e. CScrollView). It’s also declared as dynamic (by DECLARE_DYNCREATE() directive).

CView contains a number of functions. Among them, at least, one function – OnDraw() would be re-defined. OnDraw() is called on view contents changing (f.e. overlaying by other window or on data changing, resembles OnPaint() function).

virtual void CView::OnDraw( CDC* pDC ) = 0;

pDC   Points to the device context to be used for rendering an image of the document.

That function is re-defined in every daughter class.

The example of simplest view class is in the Listing 8.3.

Listing 8.2 The example of document class in Document / View concept

Class CSampleView : public CView

{DECLARE_DYNCREATE(CSampleView)

// the private section of class

public:

void OnDraw(CDC *dc); // the data reception or writing

// the opened section of class

DECLARE_MESSAGE_MAP()

};

The dynamic objects creation switching

The first step on this way is to include DECLARE_DYNCREATE() to the created class. The second is to include to program file the macro IMPLEMENT_ DYNCREATE():

IMPLEMENT_ DYNCREATE(CMainWin, CFrameWnd)

IMPLEMENT_ DYNCREATE(CsampleView, CView)

IMPLEMENT_ DYNCREATE(CsampleDoc, CDocument)

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]