程序代码

时间:2022-07-12 05:13:13 阅读: 最新文章 文档下载
说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。
1 在“MainFrm.h”中,在class CMainFrame : public CFrameWnd里的protected

下定义:

CToolBar m_drawToolBar; //工具栏 2 MainFrm.cpp中,CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)里添加以下代码

3 if(!m_drawToolBar.Create(this)||!m_drawToolBar.LoadToolBar(IDR_DRAW)) { TRACE0("Failed to create status bar\n"); return -1; }

4 在“yuyangyiView.h”中,在class CyuyangyiView : public CView里的public定义相关变量: COLORREF m_nColor;

COLORREF m_fColor; CPoint cpoint; CPoint opoint;

BOOL m_Down; int w;int shape;

5 yuyangyiView.cpp CyuyangyiView::CyuyangyiView() { w=1; //初始线宽默认为1 shape=0; }

void Cyuyangyi::OnW1() { w=1; //令线宽为1 } void CyuyangyiView::OnW2()

{ w=2; //令线宽为2 } void CyuyangyiView::OnW3() { w=3; //令线宽为3 } void CyuyangyiView::OnW4() { w=4; //令线宽为4 } void CyuyangyiView::OnW5() { w=5; //线5 } void CyuyangyiView::OnUpdateW1(CCmdUI* pCmdUI) { pCmdUI->SetRadio(w==1); //当线宽为1时,此选项前标有圆点

武汉理工大学《专业课程设计2(面向对象程序设计)》课程设计说明书

15 } void CyuyangyiView::OnUpdateW2(CCmdUI* pCmdUI) { pCmdUI->SetRadio(w==2); //当线宽为2时,此选项前标有圆点 } void CyuyangyiView::OnUpdateW3(CCmdUI* pCmdUI) { pCmdUI->SetRadio(w==3); //线3 } void CyuyangyiView::OnUpdateW4(CCmdUI* pCmdUI) { pCmdUI->SetRadio(w==4); //线4 } void CyuyangyiView::OnUpdateW5(CCmdUI* pCmdUI) { pCmdUI->SetRadio(w==5); //线5 } void CyuyangyiView::OnLine() { shape=1; //选择直线,则令shape1 } void CyuyangyiView::OnRect() { shape=2; //选择矩形,则令shape2 } void CyuyangyiView::OnEllipse() { shape=3; //选择椭圆,则令shape3 } 武汉理工大学《专业课程设计2(面向对象程序设计) 课程设计说明书 16 void CyuyangyiView::OnUpdateLine(CCmdUI* pCmdUI) { pCmdUI->SetCheck(shape==1); //shape1时,直线选项前打上勾 } void CyuyangyiView::OnUpdateRect(CCmdUI* pCmdUI)


{ pCmdUI->SetCheck(shape==2); //shape2时,矩形选项前打上勾 } void CyuyangyiView::OnUpdateEllipse(CCmdUI* pCmdUI) { pCmdUI->SetCheck(shape==3); //shape3时,椭圆选项前打上勾 } void CyuyangyiView::OnColor() { CColorDialog Color; // if(Color.DoModal() == IDOK) // OK m_nColor=Color.GetColor(); // ,m_nColor } void

CyuyangyiView::OnFillColor() { CColorDialog FillColor; // if(FillColor.DoModal() == IDOK) // OK m_fColor=FillColor.GetColor(); // ,m_fColor } void

CyuyangyiView::OnPaint() { CPaintDC dc(this); // device context for painting CPen pen; //定义画笔 CBrush brush; //定义画刷 武汉理工大学《专业课程设计2(面向对象程序设计)》课程设计说明书

17 pen.CreatePen(PS_SOLID,w, m_nColor); // brush.CreateSolidBrush(m_fColor); //创建画刷 dc.SelectObject(&pen); //获取画笔 dc.SelectObject(&brush); //获取画刷 if(shape==1) {dc.MoveTo(opoint.x,opoint.y); dc.LineTo(cpoint.x,cpoint.y);} //绘制直线,opointepoint线 if(shape==2) dc.Rectangle(opoint.x,opoint.y,cpoint.x,cpoint.y); // if(shape==3) dc.Ellipse(opoint.x,opoint.y,cpoint.x,cpoint.y); // } void CyuyangyiView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default m_Down=TRUE; //鼠标按下 SetCapture(); //获取坐标 opoint=point; //把鼠opoint ReleaseCapture(); // CView::OnLButtonDown(nFlags, point); } void

CyuyangyiView::OnMouseMove(UINT nFlags, CPoint point) { if(m_Down) //如果鼠标按下 cpoint=point; //把鼠标所在点坐标赋给终点坐标epoint CView::OnMouseMove(nFlags, point); } void CyuyangyiView::OnLButtonUp(UINT

nFlags, CPoint point) { m_Down=FALSE; //鼠标不按下,即弹起 Invalidate(0); //刷新 CView::OnLButtonUp(nFlags, point); }


本文来源:https://www.wddqw.com/doc/4d3518eb52ea551811a68714.html