Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Методичка VC++6укр.doc
Скачиваний:
1
Добавлен:
20.12.2018
Размер:
287.74 Кб
Скачать

Після цього треба натиснути кнопку Add Function, після цього – кнопку Edit Code. В функцію-обробник додамо код:

::SetTimer( this->m_hWnd , 1 , 100 , 0);

Додамо також код в обробник OnDraw:

for(int i=0; i<vect.size(); i++)

vect[i]->draw( pDC );

Треба створити наші об’єкти зробимо це в конструкторі класу coopSampleView:

COOPSampleView::COOPSampleView()

{

// TODO: add construction code here

vect.push_back( new CCustLine( 10, 10, 50, 10) );

vect.push_back( new CCustLine( 110, 10, 150, 10) );

vect.push_back( new CCustRect( 10 ,100 , 30 ,20 ));

}

Таким чином файли з вихідним кодом матимуть наступний вміст:

// OOPSampleView.cpp : implementation of the COOPSampleView class

//

#include "stdafx.h"

#include "OOPSample.h"

#include "OOPSampleDoc.h"

#include "OOPSampleView.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

/////////////////////////////////////////////////////////////////////////////

// COOPSampleView

IMPLEMENT_DYNCREATE(COOPSampleView, CView)

BEGIN_MESSAGE_MAP(COOPSampleView, CView)

//{{AFX_MSG_MAP(COOPSampleView)

ON_COMMAND(ID_TIMER_RUNTIMER, OnTimerRuntimer)

ON_WM_TIMER()

//}}AFX_MSG_MAP

// Standard printing commands

ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)

ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)

ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// COOPSampleView construction/destruction

COOPSampleView::COOPSampleView()

{

// TODO: add construction code here

vect.push_back( new CCustLine( 10, 10, 50, 10) );

vect.push_back( new CCustLine( 110, 10, 150, 10) );

vect.push_back( new CCustRect( 10 ,100 , 30 ,20 ));

}

COOPSampleView::~COOPSampleView()

{

for(int i=0; i<vect.size(); i++)

delete vect[i];

}

BOOL COOPSampleView::PreCreateWindow(CREATESTRUCT& cs)

{

// TODO: Modify the Window class or styles here by modifying

// the CREATESTRUCT cs

return CView::PreCreateWindow(cs);

}

/////////////////////////////////////////////////////////////////////////////

// COOPSampleView drawing

void COOPSampleView::OnDraw(CDC* pDC)

{

COOPSampleDoc* pDoc = GetDocument();

ASSERT_VALID(pDoc);

for(int i=0; i<vect.size(); i++)

vect[i]->draw( pDC );

// TODO: add draw code for native data here

}

/////////////////////////////////////////////////////////////////////////////

// COOPSampleView printing

BOOL COOPSampleView::OnPreparePrinting(CPrintInfo* pInfo)

{

// default preparation

return DoPreparePrinting(pInfo);

}

void COOPSampleView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)

{

// TODO: add extra initialization before printing

}

void COOPSampleView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)

{

// TODO: add cleanup after printing

}

/////////////////////////////////////////////////////////////////////////////

// COOPSampleView diagnostics

#ifdef _DEBUG

void COOPSampleView::AssertValid() const

{

CView::AssertValid();

}

void COOPSampleView::Dump(CDumpContext& dc) const

{

CView::Dump(dc);

}

COOPSampleDoc* COOPSampleView::GetDocument() // non-debug version is inline

{

ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COOPSampleDoc)));

return (COOPSampleDoc*)m_pDocument;

}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////

// COOPSampleView message handlers

void COOPSampleView::OnTimerRuntimer()

{

// TODO: Add your command handler code here

::SetTimer( this->m_hWnd , 1 , 100 , 0);

}

void COOPSampleView::OnTimer(UINT nIDEvent)

{

// TODO: Add your message handler code here and/or call default

CView::OnTimer(nIDEvent);

Invalidate();

}

Заголовний файл:

// OOPSampleView.h : interface of the COOPSampleView class

//

/////////////////////////////////////////////////////////////////////////////

#if !defined( AFX_OOPSAMPLEVIEW_H__B33E03CE_0E32_4F2C_94D8_EB288C1BC70C__INCLUDED_)

#define AFX_OOPSAMPLEVIEW_H__B33E03CE_0E32_4F2C_94D8_EB288C1BC70C__INCLUDED_

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

#include "CCustClass.h"

#include <vector>

class COOPSampleView : public CView

{

protected: // create from serialization only

COOPSampleView();

DECLARE_DYNCREATE(COOPSampleView)

// Attributes

public:

COOPSampleDoc* GetDocument();

// Operations

public:

// Overrides

// ClassWizard generated virtual function overrides

//{{AFX_VIRTUAL(COOPSampleView)

public:

virtual void OnDraw(CDC* pDC); // overridden to draw this view

virtual BOOL PreCreateWindow(CREATESTRUCT& cs);

protected:

virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);

virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);

virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);

//}}AFX_VIRTUAL

// Implementation

public:

std::vector<CCustGraph *> vect;

virtual ~COOPSampleView();

#ifdef _DEBUG

virtual void AssertValid() const;

virtual void Dump(CDumpContext& dc) const;

#endif

protected:

// Generated message map functions

protected:

//{{AFX_MSG(COOPSampleView)

afx_msg void OnTimerRuntimer();

afx_msg void OnTimer(UINT nIDEvent);

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

#ifndef _DEBUG // debug version in OOPSampleView.cpp

inline COOPSampleDoc* COOPSampleView::GetDocument()

{ return (COOPSampleDoc*)m_pDocument; }

#endif

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined( AFX_OOPSAMPLEVIEW_H__B33E03CE_0E32_4F2C_94D8_EB288C1BC70C__INCLUDED_)

Робота з таймером у Visial C++ полягає у використанні функцій операційної системи SetTimer. Оператор :: розширює область видимості (з класу до глобальної) . Для обробки події за допомогою ClassWizard створюється обробник, код якого наведений вище, який реагує на подію WM_TIMER. Цей обробник викликає функцію Invalidate, яка призводить до перемальовування вікна. Цей процес програмується в методі OnDraw. В цьому методі для кожного з обєктів списку викликається метод draw. Механізм С++ перевизначення методів викличе необхідний метод, хоча ми маємо список вказівників на базовий клас.