1.项目名称:简单绘图程序
2.项目内容
设计一个简单的绘图程序,能够绘制椭圆(圆)、矩形、任意线。
3.功能设计思路
利用MFC制作这样一个绘图程序!实现绘制椭圆(圆)、矩形、任意线。需要通过菜单按钮实现,可以提高自己对于MFC基本架构的理解,提高自己的VC++编程能力!
4.编码实施
首先建立一个利用向导生成一个MFC程序名为CTDraw;
添加菜单绘制图形菜单——画圆ID_MENUYUAN、画矩形ID_MENUJUXING、线段ID_MENUDRAW;
右击菜单画圆在类向导中为CCTdrawView类添加OnLButtonDown、OnLButtonUp、OnMouseMove事件,为ID_MENUYUAN添加Commond处理函数,为ID_MENUJUXING添加Commond处理函数,为ID_MENUDRAW添加Commond处理函数、添加函数ResetAllFlags()。
添加变量:
bool m_draw;
bool m_yuan;
bool m_juxing;
CPoint m_start;
CPoint m_end;
添加代码如下:
1 void CCTdrawView::ResetAllFlags() 2 { 3 m_draw = false; 4 m_juxing = false; 5 m_yuan = false; 6 } 7 8 void CCTdrawView::OnMenudraw() 9 {10 // TODO: Add your command handler code here11 ResetAllFlags();12 m_draw = true;13 }14 void CCTdrawView::OnMenujuxing() 15 {16 // TODO: Add your command handler code here17 ResetAllFlags();18 m_juxing = true;19 }20 void CCTdrawView::OnMenuyuan() 21 {22 // TODO: Add your command handler code here23 ResetAllFlags();24 m_yuan = true;25 }26 void CCTdrawView::OnUpdateMenujuxing(CCmdUI* pCmdUI) 27 {28 // TODO: Add your command update UI handler code here29 pCmdUI->SetCheck(m_juxing);30 }31 void CCTdrawView::OnUpdateMenuyuan(CCmdUI* pCmdUI) 32 {33 // TODO: Add your command update UI handler code here34 pCmdUI->SetCheck(m_yuan);35 }36 void CCTdrawView::OnUpdateMenudraw(CCmdUI* pCmdUI) 37 {38 // TODO: Add your command update UI handler code here39 pCmdUI->SetCheck(m_draw);40 }41 void CCTdrawView::OnLButtonDown(UINT nFlags, CPoint point) 42 {43 // TODO: Add your message handler code here and/or call default44 if(m_draw)45 {46 m_start = point;47 }48 if(m_juxing||m_yuan)49 {50 m_start = point;51 m_end = point;52 SetCapture();53 }54 CView::OnLButtonDown(nFlags, point);55 }56 void CCTdrawView::OnLButtonUp(UINT nFlags, CPoint point) 57 {58 // TODO: Add your message handler code here and/or call default59 ReleaseCapture();60 CView::OnLButtonUp(nFlags, point);61 }62 void CCTdrawView::OnMouseMove(UINT nFlags, CPoint point) 63 {64 // TODO: Add your message handler code here and/or call default65 CClientDC dc(this);66 if(m_draw&&(nFlags&&MK_LBUTTON))67 {68 dc.MoveTo(m_start);69 dc.LineTo(point);70 m_start = point;71 }72 if(m_juxing&&(nFlags&&MK_LBUTTON))73 {74 CGdiObject*object = dc.SelectStockObject(NULL_BRUSH);75 int mdoe = dc.GetROP2();76 dc.SetROP2(R2_NOTCOPYPEN);77 dc.Rectangle(m_end.x,m_end.y,m_start.x,m_start.y);78 dc.SetROP2(mdoe);79 dc.Rectangle(m_start.x,m_start.y,point.x,point.y);80 dc.SelectObject(object);81 m_end = point;82 }83 if(m_yuan&&(nFlags&&MK_LBUTTON))84 {85 CGdiObject*object = dc.SelectStockObject(NULL_BRUSH);86 int mdoe = dc.GetROP2();87 dc.SetROP2(R2_NOTCOPYPEN);88 dc.Ellipse(m_end.x,m_end.y,m_start.x,m_start.y);89 dc.SetROP2(mdoe);90 dc.Ellipse(m_start.x,m_start.y,point.x,point.y);91 dc.SelectObject(object);92 m_end = point;93 }94 CView::OnMouseMove(nFlags, point);95 }
5.功能实现
6.总结
本设计实现了简单的图形绘制,一个简单的问题就是当视图大小变化时显示的图形就会丢失!再有比如数据的序列化问题,数据的缓冲处理,当数据量很大的时候重绘必须用的缓冲机制。很高。