关于继承自CView和CDrawPlaneView(CXXXXXView)类的继承

非常简单的应用场景 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);

};

抽象类时 需要普通类完全重写抽象类内的抽象函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值