1、直线,带箭头的线,任意线,圆形,椭圆,矩形。2) 可以对画笔进行设置:画笔的颜色,画笔的线宽,画笔的形状。3) 可以保存所有的图形到内存中,所有图形绘制后,拉动窗口发生重绘不改变。4) 可以将所有绘制的图形保存到文件中,并且可以读取。5) 可以“新建”空白绘图文档。6) 可以查看最近打开的文档。7) 可以打印用户绘制好后的图形,打印预览你绘制的图形。8) 可以完成一部分的“撤消”功能,(注:任意曲线的图形绘制撤消不能实现)9) 可以完成图形“清空”功能。有以下几个次要功能1) 有个软件启动画面。2) 有个绘图工具栏。3) 在状态栏可以显示鼠标的坐标和当前的时间。4) 可以打开txt文本文件,并
2、且修改字体。5) 可以打开bmp图像文件,并且显示图像。6) 可以打开wav音频文件,并且播放音乐。2总体设计本程序用的MFC中的单文档 文档/视图类结构。在视图类中完成图形的绘制,显示,重绘,清空,撤消,设置画笔,设置字体,打开txt,wav,bmp文件。在文档类保存了,绘制图形的各种数据,所有图形的点的坐标,画笔的信息。3详细设计设计了保存图形的类class CGraph : public CObject/派生于 CObject /将CGraph串行话 需要5个步骤 /1.要派生于CObject类 /2.重新写void Serialize( CArchive& ar ) /3.正在头文件定
3、义宏定义DECLARE_SERIAL( CGraphPoint ) /4.无参数的构造函数CGraph(); /5.在实现文件cpp中 使用宏定义DECLARE_SERIAL( CGraph,CObject,1/*类的版本号*/) DECLARE_SERIAL( CGraph ) /文档串行化的 宏定义DECLARE_SERIAL( CGraph )public: void Serialize( CArchive& ar );/重新写Serialize函数 void Draw(CDC *pDC);/绘图的方法 UINT m_DrawIndex;/绘图的类型 CPoint m_FirstPoin
4、t;/起始坐标 CPoint m_SecondPoint;/结束坐标 COLORREF m_color; /颜色 UINT m_nLineWidth; /线宽 int m_nLineStyle; /线形 CGraph(); CGraph(UINT DrawIndex,CPoint FirstPoint,CPoint SecondPoint,COLORREF color,UINT LineWidth,int LineStyle);/4个参数构造函数 virtual CGraph();实现文件IMPLEMENT_SERIAL( CGraph, CObject, 1 )/文档串行化的 宏定义DECL
5、ARE_SERIAL( CGraph )CGraph:CGraph()CGraph(UINT DrawIndex,CPoint FirstPoint,CPoint SecondPoint,COLORREF color,UINT LineWidth,int LineStyle)/5 this-m_DrawIndex = DrawIndex;m_FirstPoint = FirstPoint;m_SecondPoint= SecondPoint;m_color = color;m_nLineStyle = LineStyle;m_nLineWidth = LineWidth;CGraph()voi
6、d CGraph:Draw(CDC *pDC)/CGraph m_DrawIndex CPen hpen(m_nLineStyle,m_nLineWidth,m_color); CBrush *pBrush = CBrush:FromHandle(HBRUSH)GetStockObject(NULL_BRUSH); CBrush *pOldBrush = pDC-SelectObject(pBrush); switch(m_DrawIndex) case 1: pDC-SelectObject(&hpen);MoveTo(m_FirstPoint);LineTo(m_SecondPoint);
7、 break; case 2: case 3: float flRlt; flRlt=(float)(m_FirstPoint.x-m_SecondPoint.x)*(m_FirstPoint.x-m_SecondPoint.x)+(m_FirstPoint.y-m_SecondPoint.y)*(m_FirstPoint.y-m_SecondPoint.y); flRlt=sqrt(flRlt); if(flRlt = 0) return; float flLength = 15; float flAngle = 100;/ float tmpX = (float)(m_SecondPoin
8、t.x) + (float)(m_FirstPoint.x)-(float)(m_SecondPoint.x)*flLength/flRlt; float tmpY = (float)(m_SecondPoint.y) + (float)(m_FirstPoint.y)-(float)(m_SecondPoint.y)*flLength/flRlt; float fl1X = (tmpX-(float)(m_SecondPoint.x)*cos(-flAngle/2) - (tmpY-(float)(m_SecondPoint.y)*sin(-flAngle/2) + (float)(m_Se
9、condPoint.x); float fl1Y = (tmpY-(float)(m_SecondPoint.y)*cos(-flAngle/2) + (tmpX-(float)(m_SecondPoint.x)*sin(-flAngle/2) + (float)(m_SecondPoint.y); float fl2X = (tmpX-(float)(m_SecondPoint.x)*cos(flAngle/2) - (tmpY-(float)(m_SecondPoint.y)*sin(flAngle/2) + (float)(m_SecondPoint.x); float fl2Y = (
10、tmpY-(float)(m_SecondPoint.y)*cos(flAngle/2) + (tmpX-(float)(m_SecondPoint.x)*sin(flAngle/2) + (float)(m_SecondPoint.y);MoveTo(m_SecondPoint.x,m_SecondPoint.y);LineTo(fl1X,fl1Y);LineTo(fl2X,fl2Y);MoveTo(fl1X,fl1Y); case 4:Ellipse(CRect(m_FirstPoint,m_SecondPoint);SelectObject(pOldBrush); case 5: cas
11、e 6:Rectangle(CRect(m_FirstPoint,m_SecondPoint); Serialize(CArchive &ar)/CArchive / if(ar.IsStoring() arm_DrawIndexm_FirstPointm_SecondPointm_colorm_nLineWidthm_DrawIndexm_FirstPointm_SecondPointm_colorm_nLineWidth;任意曲线类class CStroke : public CObject/ CStroke(UINT nPenWidth,COLORREF color);protected
12、: CStroke(); DECLARE_SERIAL(CStroke)/ Attributes UINT m_nPenWidth; / one pen width applies to entire stroke /颜色 CArray m_pointArray; / series of connected points/ Operations BOOL DrawStroke(CDC* pDC); virtual void Serialize(CArchive& ar);实现文件CStroke.cppIMPLEMENT_SERIAL(CStroke, CObject, 2)CStroke:CS
13、troke() / This empty constructor should be used by serialization onlyCStroke(UINT nPenWidth,COLORREF color) m_nPenWidth = nPenWidth; m_color = color;void CStroke:Serialize(CArchive& ar) if (ar.IsStoring() ar (WORD)m_nPenWidth w m_nPenWidth = w;BOOL CStroke:DrawStroke(CDC* pDC) CPen penStroke; if (!p
14、enStroke.CreatePen(PS_SOLID, m_nPenWidth, m_color) return FALSE; CPen* pOldPen = pDC-penStroke); pDC-MoveTo(m_pointArray0); for (int i=1; i GetWindowRect(&rect);/得到示例静态空间的矩形区域 ScreenToClient(&/坐标转换 dc.MoveTo(rect.left+20,rect.top+rect.Height()/2); dc.LineTo(rect.right-20,rect.top+rect.Height()/2); /
15、 Do not call CDialog:OnPaint() for painting messages在CMainFrame中添加的代码添加成员变量CToolBar m_newToolBar 一个新的绘图工具栏在CMainFrame中的OnCreat()函数中添加的代码:CSplashWnd:ShowSplashScreen(this);/启动画面Sleep(1000);/程序暂停/-新建画图工具栏-/if (!m_newToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER |
16、 CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) | !m_newToolBar.LoadToolBar(IDR_DRAW_TOOLBAR) TRACE0(Failed to create toolbarn); return -1; / fail to createm_newToolBar.EnableDocking(CBRS_ALIGN_ANY);EnableDocking(CBRS_ALIGN_ANY);DockControlBar(&m_newToolBar);在CMFCDoc中添加的代码CMFCDoc.h中 virtual CMFCDoc
17、(); CPen m_penCur; / pen created according to CTypedPtrList m_strokeList; / current user-selected pen width /颜色 CPen* GetCurrentPen(); CStroke* NewStroke(); void InitDocument(); /初始化任意曲线的画笔 void DeleteContents();/删除任意曲线 BOOL OnOpenDocument(LPCTSTR lpszPathName);CMFCDoc.cpp中CStroke* CMFCDoc:NewStroke
18、()/newStroke POSITION pos = GetFirstViewPosition(); CMFCView *pView = (CMFCView*)GetNextView(pos); /获取视类的指针 /获取视类的画笔宽度和颜色 m_nPenWidth = pView-m_nLineWidth; m_color = pView-m_color; CStroke* pStrokeItem = new CStroke(m_nPenWidth,m_color); m_strokeList.AddTail(pStrokeItem); SetModifiedFlag(); / Mark t
19、he document as having been modified, for / purposes of confirming File Close. return pStrokeItem;BOOL CMFCDoc:OnNewDocument()/新建空白文档CDocument:OnNewDocument() add reinitialization code here / (SDI documents will reuse this document) /新建空白文档就是把当前的数据全部删除掉 int max = pView-m_graph.GetSize(); for(int i=0;im_graph.RemoveAt(max-i-1);/
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1