《实现图形的重绘.doc》由会员分享,可在线阅读,更多相关《实现图形的重绘.doc(19页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-date实现图形的重绘实现图形的重绘实现图形的重绘在所给代码的基础上实现图形的重绘(窗口大小改变后所绘制图形不丢失)。1、修改stdafx.h删除 #include 增加#include #include #include 2、修改项目属性将 【配置属性】-【常规】-【MFC的使用】修改为 在共享的DLL中使用MFC3、修改FirstWnd.h增加以下代码:typedef s
2、truct tagDrawLinePOINT ptStart;POINT ptEnd;DRAWLINE,*PDRAWLINE;typedef struct tagDrawRectRECT rect;DRAWRECT,*PDRAWRECT;typedef enum tagSHAPESHAPELINE,SHAPERECT,SHAPEELLIPSESHAPE;typedef struct tagDrawShapeSHAPE shape;UINT penWidth;DWORD color;BOOL bFill;unionDRAWLINE ShapeLine;DRAWRECT ShapeRect;DRA
3、WSHAPE,*PDRAWSHAPE;4、修改firstWnd.cpp中 DrawStru的定义,增加CArray graphicArray;这一句,修改后的代码为typedef struct tagDrawStruenum DrawTool shape;BOOL bMouseDown;BOOL bFill;int xBegin,yBegin ,x,y;COLORREF color;HPEN hPen;HBRUSH hBr;int lineWidth;CArray graphicArray; DRAWSTRU ,*PDRAWSTRU;5、修改void LineMouseUp函数在函数的最后增加
4、以下代码DRAWSHAPE myShape;myShape.shape=SHAPELINE;myShape.bFill=dr.bFill;myShape.color=dr.color;myShape.penWidth=1;myShape.ShapeLine.ptStart.x=dr.xBegin;myShape.ShapeLine.ptStart.y=dr.yBegin;myShape.ShapeLine.ptEnd.x=dr.x;myShape.ShapeLine.ptEnd.y=dr.y;dr.graphicArray.Add(myShape);6、修改void EllipseMouseU
5、p函数在函数的最后增加以下代码DRAWSHAPE myShape;myShape.shape=SHAPEELLIPSE;myShape.bFill=dr.bFill;myShape.color=dr.color;myShape.penWidth=1;myShape.ShapeRect.rect.left=dr.xBegin;myShape.ShapeRect.rect.top=dr.yBegin;myShape.ShapeRect.rect.right=dr.x;myShape.ShapeRect.rect.bottom=dr.y;dr.graphicArray.Add(myShape);7、
6、修改void RectMouseUp函数参考6的代码8、修改WM_PAINT处理代码在窗口函数的开始部分增加以下定义:HPEN hPen,hOldPen;HBRUSH hBr,hOldBr;在/Todo后面增加以下代码int i;DRAWSHAPE myShape;for(i=0;idr.graphicArray.GetSize();i+)myShape=dr.graphicArrayi;hPen=CreatePen(PS_SOLID,myShape.penWidth,myShape.color);if(myShape.bFill)hBr=CreateSolidBrush(myShape.co
7、lor);elsehBr=(HBRUSH)GetStockObject(NULL_BRUSH);hOldPen=(HPEN)SelectObject(hdc,hPen);hOldBr=(HBRUSH)SelectObject(hdc,hBr);switch(myShape.shape)case SHAPELINE:MoveToEx(hdc,myShape.ShapeLine.ptStart.x,myShape.ShapeLine.ptStart.y,NULL);LineTo(hdc,myShape.ShapeLine.ptEnd.x,myShape.ShapeLine.ptEnd.y);bre
8、ak;case SHAPEELLIPSE:Ellipse(hdc,myShape.ShapeRect.rect.left,myShape.ShapeRect.rect.top,myShape.ShapeRect.rect.right,myShape.ShapeRect.rect.bottom);break;case SHAPERECT:Rectangle(hdc,myShape.ShapeRect.rect.left,myShape.ShapeRect.rect.top,myShape.ShapeRect.rect.right,myShape.ShapeRect.rect.bottom);br
9、eak;SelectObject(hdc,hOldPen);DeleteObject(hPen);if(myShape.bFill)SelectObject(hdc,hOldBr);DeleteObject(hBr);9、在菜单资源中增加菜单项在【File】菜单下增加【打开】菜单 Caption 为打开(&O),ID为 IDM_FILE_OPEN【保存】菜单 Caption 为保存(&S),ID为 IDM_FILE_SAVE10、在窗口函数中增加菜单处理代码如下:case IDM_FILE_SAVE:CFile file(TEXT(.g.dat),CFile:modeCreate|CFile:modeWrite);CArchive ar(&file,CArchive:store);dr.graphicArray.Serialize(ar);break;case IDM_FILE_OPEN:CFile file(TEXT(.g.dat),CFile:modeRead);CArchive ar(&file,CArchive:load);dr.graphicArray.Serialize(ar);InvalidateRect(hWnd,NULL,true);break;11、当程序退出时提示保存文件,测试并掌握以上程序-