非常简单的应用场景 DrawPlaneView为绘图类MCF
调用Ondraw()函数画几个点
enum Option
{ DRAW = 0,
ERASER,
PICK,
DELETEELE,
BREAK};
struct _currentSettings
{ Option currentOption;
CPoint mPoint;
COLORREF RGB;
int scale; }currentSettings;
// CDrawPlaneView 消息处理程序
void CDrawPlaneView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
currentSettings.RGB = RGB(255,0,0);
currentSettings.mPoint = point;
currentSettings.currentOption = Option::DRAW;
currentSettings.scale = 9;
/*MessageBox(NULL, L"Hello, Windows!", MB_OK);*/
Invalidate();
CView::OnLButtonUp(nFlags, point);
}
//方法2
DrawElement temp;
void CDrawPlaneView::OnDraw(CDC* pDC)
{
CDrawPlaneDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
//方法1
/*DrawPoint(pDC, currentSettings.mPoint, currentSettings.scale, currentSettings.RGB);*/
//方法2 3
temp.DrawPoint(pDC, currentSettings.mPoint, currentSettings.scale, currentSettings.RGB)
};
方法1
直接在DrawPlaneView.cpp 中实现DrawPoint并在头文件上添加定义
void CDrawPlaneView::DrawPoint(CDC *pDc, CPoint point, int scale, COLORREF color)
{
switch (scale)
{
case 1:pDc->SetPixel(point, color); break;
case 2:pDc->SetPixel(point, color); pDc->SetPixel(point.x + 1, point.y, color); break;
case 4:pDc->SetPixel(point, color); pDc->SetPixel(point.x + 1, point.y, color);
pDc->SetPixel(point.x, point.y + 1, color); pDc->SetPixel(point.x + 1, point.y + 1, color); break;
case 9:
pDc->SetPixel(point.x - 1, point.y - 1, color);
pDc->SetPixel(point.x, point.y - 1, color);
pDc->SetPixel(point.x + 1, point.y - 1, color);
pDc->SetPixel(point.x - 1, point.y, color);
pDc->SetPixel(point, color);
pDc->SetPixel(point.x + 1, point.y, color);
pDc->SetPixel(point.x - 1, point.y + 1, color);
pDc->SetPixel(point.x, point.y + 1, color);
pDc->SetPixel(point.x + 1, point.y + 1, color); break;
default:
break;
}
}
方法2:
新建类
#pragma once
#include "DrawPlaneDoc.h"//这个是必要的,如果没有会报错
//参见[*GetDocument() const编译时错误:缺少“;”(在“*”的前面)](http://blog.csdn.net/hanjieson/article/details/8194337)#include "DrawPlaneView.h"
class DrawElement :CDrawPlaneView
{
public:
void DrawPoint(CDC *pDc,CPoint point,int scale, COLORREF color);
void DrawLine(CDC *pDc, CPoint startpoint, CPoint endpoint,int scale, COLORREF color);
//public:
// DrawElement();
// virtual ~DrawElement();
};
然后再实现DrawPoint
第三种方法
不继承任何类
#pragma once
class DrawElement
{
public:
void DrawPoint(CDC *pDc,CPoint point,int scale, COLORREF color);
void DrawLine(CDC *pDc, CPoint startpoint, CPoint endpoint,int scale, COLORREF color);
//public:
DrawElement();
// virtual ~DrawElement();
};
第四种方法
使DrawElement继承自CView
那么要重写CView的虚函数
class DrawElement :CView
{
public:
DrawElement();
virtual ~DrawElement();
// 重写
public:
virtual void OnDraw(CDC* pDC); // 重写以绘制该视图
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);
// 实现
public:
void DrawPoint(CDC *pDc,CPoint point,int scale, COLORREF color);
void DrawLine(CDC *pDc, CPoint startpoint, CPoint endpoint,int scale, COLORREF color);
};
抽象类时 需要普通类完全重写抽象类内的抽象函数