专业课程设计(2011/2012学年第2学期)题目一:简易画图板的设计与实现专业计算机通信学生姓名韩亮班级学号09002736指导教师王海艳指导单位计算机学院·计算机科学与技术系日期2012年6月20日教师评语教师签名:年月日成绩评定备注一、课题名称设计题目1:简易画图板的设计与实现二、课题内容和要求利用MFC完成画图板的基本功能。
此程序将实现简单的绘图功能,包括点、直线、矩形、椭圆、扇形和连续线的绘制。
并且能实现绘图的控制,包括线宽、线型和颜色的设置,图形的保存和打开以及笔刷的使用。
三、需求分析1) 在单文档菜单中,在菜单行中可插入一个菜单项,命名为绘图,在下拉菜单中可分别设置绘制的图形形状,如直线、矩形及椭圆,线宽选项,有1-5可供选择,还可以设置线色以及填充色,通过弹出的颜色对话框选择需要的颜色,如果不选择线宽、线色以及填充色,则按默认的画笔,画刷来绘制选择的图形。
2) 选择好图形后,通过鼠标可以绘制出相应的直线,矩形或椭圆,鼠标的按下确定图形的起点,鼠标的拖动则确定了图形的终点,即通过鼠标的拖动来决定图形的大小,当鼠标弹起,此图形则绘制完毕。
3) 增添工具栏,设置绘制的图形形状,线色以及填充色,可更方便地选择相应的功能。
四、概要设计1) 对需要用到的变量进行初始化。
2) 选择相应的图形之后就响应相应的消息处理函数,给shape赋对应的值。
选择不同的线宽,线色与填充色,即可改变画笔或画刷的属性。
3) 鼠标的按下响应函数OnLButtonDown(),捕捉鼠标当前位置得到起点的坐标,鼠标的拖动响函数OnMouseMove()改变终点的坐标,鼠标的弹起响应OnLButtonUp(),确定终点坐标,刷新,得到绘制图形。
4) 选择图形或其它属性,可进行下一次绘制。
开始定义并初始化变shape线色m_ncolor 填充色m_fcolor 画笔pen画刷brush绘图起点opoint绘图终点选择线宽改变画笔属性默认值(w=1)改变画刷属性默认值(黑色)默认值(黑色)选择填充改变画笔属性选择线色图1 程序流程图五、详细设计1)创建单文档 2) 编辑菜单 3)创建工具栏4)在相应消息函数处添加代码,实现其功能 5)选择要绘制的图形 6)改变线宽 7)改变线色 8)绘制图形 9)关键代码// GraphicView.cpp : implementation of the CGraphicView class //矩形鼠标左键按下 响应函数OnLButtonDown() opoint =point直线鼠标左键弹起响应函数OnLButtonUp()刷新,得到图形 鼠标移动 响应函数OnMouseMove()epoint=point响应OnPaint(),绘制图形 响应函数 OnEllipse() shape=3响应函数 OnRect() shape=2响应函数 OnLine() shape=1椭圆结束#include "stdafx.h"#include "Graphic.h"#include "GraphicDoc.h"#include "GraphicView.h"#include "SettingDlg.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif///////////////////////////////////////////////////////////////////////////// // CGraphicViewIMPLEMENT_DYNCREATE(CGraphicView, CView)BEGIN_MESSAGE_MAP(CGraphicView, CView)//{{AFX_MSG_MAP(CGraphicView)ON_COMMAND(IDM_DOT, OnDot)ON_COMMAND(IDM_ELL, OnEll)ON_COMMAND(IDM_LINE, OnLine)ON_COMMAND(IDM_RECT, OnRect)ON_WM_LBUTTONDOWN()ON_WM_LBUTTONUP()ON_COMMAND(IDM_SETTING, OnSetting)ON_COMMAND(IDM_COLOR, OnColor)ON_WM_MOUSEMOVE()//}}AFX_MSG_MAP// Standard printing commandsON_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()///////////////////////////////////////////////////////////////////////////// // CGraphicView construction/destructionCGraphicView::CGraphicView(){// TODO: add construction code herem_nDrawType=0;m_ptOrigin=0;m_nLineWidth=1;m_nLineStyle=0;m_clr=RGB(0,0,0);m_bDrawing=0;}CGraphicView::~CGraphicView(){}BOOL CGraphicView::PreCreateWindow(CREATESTRUCT& cs){// TODO: Modify the Window class or styles here by modifying// the CREATESTRUCT cscs.lpszClass=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,LoadCursor(NUL L,IDC_CROSS),(HBRUSH)GetStockObject(BLACK_BRUSH),0);return CView::PreCreateWindow(cs);}/////////////////////////////////////////////////////////////////////////////// CGraphicView drawingvoid CGraphicView::OnDraw(CDC* pDC){CGraphicDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);// TODO: add draw code for native data here}/////////////////////////////////////////////////////////////////////////////// CGraphicView printingBOOL CGraphicView::OnPreparePrinting(CPrintInfo* pInfo){// default preparationreturn DoPreparePrinting(pInfo);}void CGraphicView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) {// TODO: add extra initialization before printing}void CGraphicView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) {// TODO: add cleanup after printing}/////////////////////////////////////////////////////////////////////////////// CGraphicView diagnostics#ifdef _DEBUGvoid CGraphicView::AssertValid() const{CView::AssertValid();}void CGraphicView::Dump(CDumpContext& dc) const{CView::Dump(dc);}CGraphicDoc* CGraphicView::GetDocument() // non-debug version is inline {ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGraphicDoc)));return (CGraphicDoc*)m_pDocument;}#endif //_DEBUG///////////////////////////////////////////////////////////////////////////// // CGraphicView message handlersvoid CGraphicView::OnDot(){// TODO: Add your command handler code herem_nDrawType=1;}void CGraphicView::OnEll(){// TODO: Add your command handler code herem_nDrawType=4;}void CGraphicView::OnLine(){// TODO: Add your command handler code herem_nDrawType=2;}void CGraphicView::OnRect(){// TODO: Add your command handler code herem_nDrawType=3;}void CGraphicView::OnLButtonDown(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call default m_bDrawing=1;m_ptOrigin=point;m_ptTarget=point;CView::OnLButtonDown(nFlags, point);}void CGraphicView::OnLButtonUp(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call default if(!m_bDrawing)return;m_bDrawing=0;CClientDC dc(this);CPen pen(m_nLineStyle,m_nLineWidth,m_clr);dc.SelectObject(&pen);CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH)); dc.SelectObject(pBrush);switch(m_nDrawType){case 1:dc.SetPixel(point,m_clr);break;case 2:dc.MoveTo(m_ptOrigin);dc.LineTo(point);break;case 3:dc.Rectangle(CRect(m_ptOrigin,point));break;case 4:dc.Ellipse(CRect(m_ptOrigin,point));break;}CView::OnLButtonUp(nFlags, point);}void CGraphicView::OnSetting(){// TODO: Add your command handler code here CSettingDlg dlg;dlg.m_nLineWidth=m_nLineWidth;dlg.m_nLineStyle=m_nLineStyle;dlg.m_clr=m_clr;if(IDOK==dlg.DoModal())m_nLineWidth=dlg.m_nLineWidth;m_nLineStyle=dlg.m_nLineStyle;}void CGraphicView::OnColor(){// TODO: Add your command handler code here CColorDialog dlg;dlg.m_cc.Flags|=CC_RGBINIT;dlg.m_cc.rgbResult=m_clr;if(IDOK==dlg.DoModal())m_clr=dlg.m_cc.rgbResult;}void CGraphicView::OnMouseMove(UINT nFlags, CPoint point){// TODO: Add your message handler code here and/or call defaultif(!m_bDrawing)return;CClientDC dc(this);CPen pen(m_nLineStyle,m_nLineWidth,m_clr);dc.SelectObject(&pen);dc.SetROP2(R2_NOT);CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH)); dc.SelectObject(pBrush);switch(m_nDrawType){case 1:break;case 2:dc.MoveTo(m_ptOrigin);dc.LineTo(m_ptTarget);m_ptTarget = point;dc.MoveTo(m_ptOrigin);dc.LineTo(m_ptTarget);break;case 3:dc.Rectangle(CRect(m_ptOrigin,m_ptTarget)); m_ptTarget = point;dc.Rectangle(CRect(m_ptOrigin,m_ptTarget)); break;case 4:dc.Ellipse(CRect(m_ptOrigin,m_ptTarget));m_ptTarget = point;dc.Ellipse(CRect(m_ptOrigin,m_ptTarget)); break;}CView::OnMouseMove(nFlags, point);}六、测试数据及其结果分析七、调试过程中的问题在编程时遇到了图像在变换大小时,原来的图形会消失,后来通过老师的讲解,及查看相关资料,这里涉及的一个重绘问题。