创建MFC工程
1、单文档类型
2、选择菜单栏
3、选择文档程序
添加绘画类:
CLayer头文件
class CLayer
{//提供画的接口 是纯虚函数的类 提供给具体的函数使用
public:
CLayer();
~CLayer();
virtual void OnDraw(CDC *pCD) = 0;
virtual void OnLButtonDown(UINT nFlags, CPoint point) = 0;
virtual void OnLButtonUp(UINT nFlags, CPoint point) = 0;
};
直线类:
#pragma once
#include"CLayer.h"
class CLine:public CLayer
{
CPoint m_start, m_end; //直线需要按下、弹起是的点
void OnDraw(CDC *pCD);
void OnLButtonDown(UINT nFlags, CPoint point);
void OnLButtonUp(UINT nFlags, CPoint point);
public:
CLine();
~CLine();
};
#include "stdafx.h"
#include "CLine.h"
void CLine::OnDraw(CDC * pCD)
{
//从鼠标按下的点画到鼠标弹起是点 m_start是起点 m_end是终点
pCD->MoveTo(m_start);
pCD->LineTo(m_end);
}
void CLine::OnLButtonDown(UINT nFlags, CPoint point)
{
m_start = point;
}
void CLine::OnLButtonUp(UINT nFlags, CPoint point)
{
m_end = point;
}
CLine::CLine() :m_start(0,0), m_end(0,0)
{
}
CLine::~CLine()
{
}
矩形类:
#pragma once
#include "CLayer.h"
class CRectang :
public CLayer
{
public:
CRectang();
~CRectang();
private:
CRect m_rect;
void OnDraw(CDC *pCD);
void OnLButtonDown(UINT nFlags, CPoint point);
void OnLButtonUp(UINT nFlags, CPoint point);
};
#include "stdafx.h"
#include "CRectang.h"
CRectang::CRectang() :m_rect(0,0,0,0)
{
}
CRectang::~CRectang()
{
}
void CRectang::OnDraw(CDC * pCD)
{
pCD->Rectangle(m_rect);
}
void CRectang::OnLButtonDown(UINT nFlags, CPoint point)
{
m_rect.TopLeft() = point;
}
void CRectang::OnLButtonUp(UINT nFlags, CPoint point)
{
m_rect.BottomRight() = point;
}
椭圆类:
#pragma once
#include "CLayer.h"
class CEllipse :
public CLayer
{
public:
CEllipse();
~CEllipse();
private:
CRect m_rect;
void OnDraw(CDC *pCD);
void OnLButtonDown(UINT nFlags, CPoint point);
void OnLButtonUp(UINT nFlags, CPoint point);
};
#include "stdafx.h"
#include "CEllipse.h"
CEllipse::CEllipse() :m_rect(0)
{
}
CEllipse::~CEllipse()
{
}
void CEllipse::OnDraw(CDC * pCD)
{
pCD->Ellipse(m_rect);
}
//将用户输入的数据
void CEllipse::OnLButtonDown(UINT nFlags, CPoint point)
{
m_rect.TopLeft() = point;
}
void CEllipse::OnLButtonUp(UINT nFlags, CPoint point)
{
m_rect.BottomRight() = point;
}
CGDI11View 类里添加:
enum{DRAW_ROUND,DRAW_RECTANGE,DRAW_LINE}; //用enum来定义
CArray<CLayer*> m_array;
UINT m_nIndex; //用来记录是画什么图形
创建工具栏的直线、矩形、椭圆选线:
在类向导–命令—ID添加 就可以添加函数,并且需要在消息里一起创建UPDATA_COMMAND_UI
这是矩形选项处理程序
void CGDI11View::OnUpdateDrawRectange(CCmdUI *pCmdUI)
{
// TODO: 在此添加命令更新用户界面处理程序代码
pCmdUI->SetCheck(m_nIndex == DRAW_RECTANGE);
}
点击选择换矩形是,m_nIndex = DRAW_RECTANGE
void CGDI11View::OnDrawRectange()
{
// TODO: 在此添加命令处理程序代码
m_nIndex = DRAW_RECTANGE;
}
获取鼠标按下时的点和保存
void CGDI11View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
/*m_array.Add(point);*/
CView::OnLButtonDown(nFlags, point);
if (m_nIndex >= DRAW_ROUND && m_nIndex <= DRAW_LINE)
{
CLayer *pLayer = NULL;
//判断要创建哪一种类型
switch (m_nIndex)
{
case DRAW_LINE:
pLayer = new CLine;
break;
case DRAW_RECTANGE:
pLayer = new CRectang;
break;
case DRAW_ROUND:
pLayer = new CEllipse;
break;
}
if (pLayer)
{
pLayer->OnLButtonDown(nFlags, point); //将点的信息传到CLayer
m_array.Add(pLayer); //保存
}
}
}
获取鼠标弹起时的点
void CGDI11View::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CView::OnLButtonUp(nFlags, point);
int nSize = m_array.GetSize();
if (0 >= nSize)
return;
m_array[nSize - 1]->OnLButtonUp(nFlags, point);
Invalidate(FALSE);
}
绘制图形:
void CGDI11View::OnDraw(CDC* pDC)
{
CGDI11Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: 在此处为本机数据添加绘制代码
//如果实在在这几个绘制定义里面,就绘制图形
int nSize = m_array.GetSize(), i = 0;
while (i < nSize)
{
CLayer *layer = (CLayer*)m_array[i++]; //获取具体的数据
layer->OnDraw(pDC);
}
}