




已阅读5页,还剩21页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验报告计算机图形学实验大作业 课程名称 计算机图形学 实验名称 二维绘图与计算机动画 姓 名 学 号 专业班级 成 绩 指导教师 二维绘图工具文档说明目录1简介 1.1基本绘图功能- 1.2附加功能-2程序代码实现 2.0设计的实现曲线对话框等的类- 2.1鼠标左键按下消息响应函数- 2.2鼠标左键弹起消息响- 2.3鼠标移动消息响应函数- 2.4鼠标右键按下消息响应函数-2.5选择绘画类型消息相应函数- 2.6画直线线函数- 2.7画有向箭头的函数- 2.8画长方形函数- 2.9画圆函数- 2.10画椭圆函数- 2.11点击“撤消” 消息响应函数- 2.12点击“清空”消息响应函数- 2.13点击“设置线宽线形”消息响应函数- 2.14点击“设置颜色”消息响应函- 2.15点击“设置字体”消息响应函数- 2.16点击“打开bmp格式测试文件”消息响应函数-3程序的运行结果3.1启动图片-3.2打开的测试图片-3.3运行程序界面-4总结 4.1 程序的问题和缺陷-4.2通过这次编写程序的认知和感触-1简介 使用VC开发平台,MFC框架实现一个画图程序,尽可能多的实现Windows自带的画图功能,并扩展其功能。在视图类中完成图形的绘制,显示,重绘,清空,撤消,设置画笔,设置字体,打开bmp图片。在文档类保存了,画笔的信息。1.1基本绘图功能:1) 用鼠标可以绘制图形,包括一下几种:直线,带箭头的线,任意线,圆形,椭圆,矩形。2) 可以对画笔进行设置:画笔的颜色,画笔的线宽,画笔的形状。3) 可以保存所有的图形到内存中,所有图形绘制后,拉动窗口发生重绘不改变。4) 可以将所有绘制的图形保存到文件中,并且可以读取。5) 可以“新建”空白绘图文档。6) 可以查看最近打开的文档。7) 可以打印用户绘制好后的图形,打印预览你绘制的图形。8) 可以完成一部分的“撤消”功能,1.2附加功能:(1)应用程序的标题栏上有程序的图标。(2)可选择打开或关闭工具栏。(3)可以打开一些文件中放的测试图片。、(4) 有一个开机启动画面。(5)有个绘图工具栏。2程序实现的主要代码/实现文件IMPLEMENT_SERIAL( CGraph, CObject, 1 )/文档串行化的 宏定义DECLARE_SERIAL( CGraph )CGraph:CGraph()CGraph:CGraph(UINT DrawIndex,CPoint FirstPoint,CPoint SecondPoint,COLORREF color,UINT LineWidth,int LineStyle)/5this-m_DrawIndex = DrawIndex;this-m_FirstPoint = FirstPoint;this-m_SecondPoint= SecondPoint;this-m_color = color;this-m_nLineStyle = LineStyle;this-m_nLineWidth = LineWidth;CGraph:CGraph()void 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);pDC-MoveTo(m_FirstPoint);pDC-LineTo(m_SecondPoint);break;case 2:break;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_SecondPoint.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_SecondPoint.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 = (tmpY-(float)(m_SecondPoint.y)*cos(flAngle/2) + (tmpX-(float)(m_SecondPoint.x)*sin(flAngle/2) + (float)(m_SecondPoint.y);pDC-SelectObject(&hpen);pDC-MoveTo(m_FirstPoint);pDC-LineTo(m_SecondPoint);pDC-MoveTo(m_SecondPoint.x,m_SecondPoint.y);pDC-LineTo(fl1X,fl1Y);pDC-MoveTo(m_SecondPoint.x,m_SecondPoint.y);pDC-LineTo(fl2X,fl2Y);pDC-MoveTo(fl1X,fl1Y);pDC-LineTo(fl2X,fl2Y);break;case 4:pDC-SelectObject(&hpen);pDC-Ellipse(CRect(m_FirstPoint,m_SecondPoint);pDC-SelectObject(pOldBrush);break;case 5:pDC-SelectObject(&hpen);pDC-Ellipse(CRect(m_FirstPoint,m_SecondPoint);pDC-SelectObject(pOldBrush);break;case 6:pDC-SelectObject(&hpen);pDC-Rectangle(CRect(m_FirstPoint,m_SecondPoint);pDC-SelectObject(pOldBrush);break;void CGraph:Serialize(CArchive &ar)/CArchive /if(ar.IsStoring()arm_DrawIndexm_FirstPointm_SecondPointm_colorm_nLineWidthm_DrawIndexm_FirstPointm_SecondPointm_colorm_nLineWidthm_nLineStyle;/任意曲线类class CStroke : public CObject/public:CStroke(UINT nPenWidth,COLORREF color);protected:CStroke();DECLARE_SERIAL(CStroke)/ Attributesprotected:UINT m_nPenWidth; / one pen width applies to entire strokeCOLORREF m_color; /颜色public:CArray m_pointArray; / series of connected points/ Operationspublic:BOOL DrawStroke(CDC* pDC);public:virtual void Serialize(CArchive& ar);实现文件CStroke.cppIMPLEMENT_SERIAL(CStroke, CObject, 2)CStroke:CStroke()/ This empty constructor should be used by serialization onlyCStroke:CStroke(UINT nPenWidth,COLORREF color)m_nPenWidth = nPenWidth;m_color = color;void CStroke:Serialize(CArchive& ar)if (ar.IsStoring()ar (WORD)m_nPenWidth w m_color;m_nPenWidth = w;m_pointArray.Serialize(ar);BOOL CStroke:DrawStroke(CDC* pDC)CPen penStroke;if (!penStroke.CreatePen(PS_SOLID, m_nPenWidth, m_color)return FALSE;CPen* pOldPen = pDC-SelectObject(&penStroke);pDC-MoveTo(m_pointArray0);for (int i=1; i LineTo(m_pointArrayi);pDC-SelectObject(pOldPen);return TRUE;/线形状和线宽设置对话框类class CSettingDlg : public CDialogpublic:CSettingDlg(CWnd* pParent = NULL); /AFX_DATA(CSettingDlg)enum IDD = IDD_DIALOG1 ;UINTm_nLineWidth; /线宽intm_nLineStyle; /线形状COLORREF m_color; /线的颜色/AFX_DATA/ Overrides/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CSettingDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); / DDX/DDV support/AFX_VIRTUALprotected:/ Generated message map functions/AFX_MSG(CSettingDlg)afx_msg void OnPaint();/AFX_MSGDECLARE_MESSAGE_MAP();实现文件CSettingDlg.cppCSettingDlg:CSettingDlg(CWnd* pParent /*=NULL*/): CDialog(CSettingDlg:IDD, pParent)/AFX_DATA_INIT(CSettingDlg)m_nLineWidth = 0;m_nLineStyle = -1;/AFX_DATA_INITvoid CSettingDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CSettingDlg)DDX_Text(pDX, IDC_EDIT2, m_nLineWidth);DDX_Radio(pDX, IDC_RADIO1, m_nLineStyle);/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CSettingDlg, CDialog)/AFX_MSG_MAP(CSettingDlg)ON_WM_PAINT()/AFX_MSG_MAPEND_MESSAGE_MAP()/ CSettingDlg message handlersvoid CSettingDlg:OnPaint() /这里添加了代码 用于对话框的示例显示CPaintDC dc(this); / device context for painting/ TODO: Add your message handler code hereUpdateData();CPen pen(m_nLineStyle,m_nLineWidth,m_color);/定义画笔dc.SelectObject(&pen);CRect rect;/定义矩形区域GetDlgItem(IDC_STATIC1)-GetWindowRect(&rect);/得到示例静态空间的矩形区域ScreenToClient(&rect);/坐标转换dc.MoveTo(rect.left+20,rect.top+rect.Height()/2);dc.LineTo(rect.right-20,rect.top+rect.Height()/2);/ Do not call CDialog:OnPaint() for painting messages/添加成员变量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 | 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.h中public:virtual CMFCDoc();CPen m_penCur; / pen created according toCTypedPtrList m_strokeList;UINT m_nPenWidth; / current user-selected pen widthCOLORREF m_color; /颜色CPen* GetCurrentPen();CStroke* NewStroke();void InitDocument(); /初始化任意曲线的画笔void DeleteContents();/删除任意曲线BOOL OnOpenDocument(LPCTSTR lpszPathName);CMFCDoc.cpp中CStroke* CMFCDoc:NewStroke()/newStrokePOSITION 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 the document as having been modified, for/ purposes of confirming File Close.return pStrokeItem;BOOL CMFCDoc:OnNewDocument()/新建空白文档if (!CDocument:OnNewDocument()return FALSE;/ TODO: add reinitialization code here/ (SDI documents will reuse this document)/新建空白文档就是把当前的数据全部删除掉POSITION pos = GetFirstViewPosition();CMFCView *pView = (CMFCView*)GetNextView(pos);int max = pView-m_graph.GetSize();for(int i=0;im_graph.RemoveAt(max-i-1);return TRUE;/ CMFCDoc serializationvoid CMFCDoc:Serialize(CArchive& ar)/获取视类的指针POSITION pos = GetFirstViewPosition();CMFCView *pView = (CMFCView*)GetNextView(pos);if (ar.IsStoring()/ TODO: add storing code hereelse/ TODO: add loading code herem_strokeList.Serialize(ar);pView-m_graph.Serialize(ar);/void CMFCDoc:InitDocument()/初始化画笔和颜色m_nPenWidth = 0; / default 2 pixel pen widthm_color = RGB(0,0,0); m_penCur.CreatePen(PS_SOLID, m_nPenWidth, m_color);void CMFCDoc:DeleteContents() /删除保存了任意曲线的数据结构while (!m_strokeList.IsEmpty()delete m_strokeList.RemoveHead();CDocument:DeleteContents();BOOL CMFCDoc:OnOpenDocument(LPCTSTR lpszPathName)/打开一个新的文档if (!CDocument:OnOpenDocument(lpszPathName)return FALSE;return TRUE;CPen* CMFCDoc:GetCurrentPen()/返回画笔return &m_penCur; CMFCView类的代码/CMFCView类class CMFCView : public CView public:DWORD m_DrawIndex; / 当前所选绘画类型的索引CPoint m_random_start; / 任意曲线起点坐标BOOL m_bDraw; /判断左键是否按下CPoint m_start; /起点CPoint m_end; /终点CObArray m_graph; /保存图形 为了重绘 和保存 文件中去COLORREF m_color; /保存画笔颜色CFont m_font; /保存字体CString m_strFontName; /保存字体名称UINTm_nLineWidth; /保存线宽int m_nLineStyle; /保存线形状/-打开bmp图像文件用的一些变量-/BOOL m_bmp; /打开位图文件的标记BITMAPINFO * pBmpInfo; / BITMAPINFO结构指针BYTE* pBmpData; /DIB图像数据指针BITMAPFILEHEADER BmpHeader; /位图文件头BITMAPINFOHEADER BmpInfo; /位图信息DWORD dataBytes;CStroke* m_pStrokeCur; / the stroke in progressCPoint m_ptPrev; / the last mouse pt in the stroke in progress/ Operations/ CMFCView中添加的函数(消息响应函数)public:float GetDistance(CPoint start, CPoint end);/获得两点坐标的距离的函数void DrawArrowLine(CPoint start,CPoint end);/画带箭头线的函数void DrawLine(CPoint start,CPoint end); /画直线函数void DrawEllipse(CPoint start,CPoint end); /画椭圆函数void DrawCircle(CPoint start,CPoint& end); /画圆函数void DrawRect(CPoint start,CPoint end); /画长方形函数protected:/ Generated message map functionsprotected: /AFX_MSG(CMFCView)afx_msg void OnLButtonDown(UINT nFlags, CPoint point); /鼠标左键按下消息响应数afx_msg void OnLButtonUp(UINT nFlags, CPoint point); /鼠标左键弹起消息响应数afx_msg void OnMouseMove(UINT nFlags, CPoint point); /鼠标移动消息响应函数afx_msg void OnRButtonDown(UINT nFlags, CPoint point); /鼠标右键按下消息响应数afx_msg void OnDrawChange(WORD nID); /选择绘画类型索引afx_msg void OnUpdateOperDrawChange(CCmdUI * pCmdUI); afx_msg void OnEditUndo(); /点击 撤消 消息响应函数afx_msg void OnClear(); /点击 清空 消息响应函数afx_msg void OnSetLineStyleWidth(); /点击 设置线宽线型 消息响应函数afx_msg void OnSetFont(); /点击 设置字体 消息响应函数afx_msg void OnSetColor(); /点击 设置颜色 消息响应函数afx_msg void OnOpenBmp(); /点击 打开bmp图像文件 消息响应函数/AFX_MSGDECLARE_MESSAGE_MAP();CMFCView:CMFCView()/在构造函数中 初始化一些变量/ TODO: add construction code herem_bDraw = false;/左键按下标志m_nLineStyle = 0;/线形状m_nLineWidth = 1;/线宽m_DrawIndex = 0;/绘画类型索引 0的画 为鼠标 m_color = RGB(0,0,0);/初始化画笔颜色void CMFCView:OnDraw(CDC* pDC)/在OnDraw函数中添加的代码CMFCDoc* pDoc = GetDocument(); /-重绘时显示任意曲线-/CTypedPtrList& strokeList = pDoc-m_strokeList;POSITION pos = strokeList.GetHeadPosition();while (pos != NULL)CStroke* pStroke = strokeList.GetNext(pos);pStroke-DrawStroke(pDC); /-重绘时显示保存在m_graph中的图形-/int pCount = m_graph.GetSize();for(int j=0;jDraw(pDC); / 用于 绘制有橡皮条功能的图形的代码-/if(m_bDraw = true)switch(m_DrawIndex)case 1:DrawLine(m_start,m_end);break;case 3:DrawArrowLine(m_start,m_end);break;case 4:DrawCircle(m_start,m_end);break;case 5:DrawEllipse(m_start,m_end);break;case 6:DrawRect(m_start,m_end);break; /用于文本文件重绘的代码-/if(m_txt = true & m_DrawIndex = 0)CRect Rect; GetClientRect(&Rect);pDC-SelectObject(m_font);pDC-DrawText(pBuf,Rect,DT_WORDBREAK); /用于图片重绘的代码-/if(m_bmp = true & m_DrawIndex = 0)pDC-SetStretchBltMode(COLORONCOLOR);StretchDIBits(pDC-GetSafeHdc(),0,0,BmpInfo.biWidth,BmpInfo.biHeight,0,0,BmpInfo.biWidth,BmpInfo.biHeight,pBmpData,pBmpInfo,DIB_RGB_COLORS,SRCCOPY);ASSERT_VALID(pDoc);/ TODO: add draw code for native data here/ 2.1鼠标左键按下消息响应函数void CMFCView:OnLButtonDown(UINT nFlags, CPoint point)/ TODO: Add your message handler code here and/or call defaultswitch(m_DrawIndex)case 1:m_bDraw = true;m_start = point;break;case 2:m_bDraw = true;m_pStrokeCur = GetDocument()-NewStroke();/ Add first point to the new strokem_pStrokeCur-m_pointArray.Add(point);m_random_start = point; / Serves as the MoveTo() anchor point for the / LineTo() the next point, as the user drags the / mouse.break;case 3:m_bDraw = true;m_start = point;break;case 4:m_bDraw = true;m_start = point;break;case 5:m_bDraw = true;m_start = point;break;case 6:m_bDraw = true;m_start = point;break;m_end = point;CView:OnLButtonDown(nFlags, point);/2.2鼠标左键弹起消息响应函数void CMFCView:OnLButtonUp(UINT nFlags, CPoint point) / TODO: Add your message handler code here and/or call defaultm_bDraw = false;CClientDC dc(this);CMFCDoc * pDoc = GetDocument();CPen* pOldPen = dc.SelectObject(pDoc-GetCurrentPen();switch(m_DrawIndex)case 1:m_end = point;DrawLine(m_start,m_end);CGraph *graph = new CGraph(m_DrawIndex,m_start,m_end,m_color,m_nLineWidth,m_nLineStyle);m_graph.Add(graph);break;case 2:/m_bDraw = false;dc.MoveTo(m_random_start);dc.LineTo(point);dc.SelectObject(pOldPen);m_pStrokeCur-m_pointArray.Add(point);break;case 3:m_end = point;DrawArrowLine(m_start,m_end);CGraph *graph = new CGraph(m_DrawIndex,m_start,m_end,m_color,m_nLineWidth,m_nLineStyle);m_graph.Add(graph);break;case 4:m_end = point;DrawCircle(m_start,m_end);CGraph *graph = new CGraph(m_DrawIndex,m_start,m_end,m_color,m_nLineWidth,m_nLineStyle);m_graph.Add(graph);break;case 5:m_end = point;DrawEllipse(m_start,m_end);CGraph *graph = new CGraph(m_DrawIndex,m_start,m_end,m_color,m_nLineWidth,m_nLineStyle);m_graph.Add(graph);break;case 6:m_end = point;DrawRect(m_start,m_end);CGraph *graph =
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 前端技术面试题及答案
- 2025年中国合同法中的漏洞与改进
- 2025员工试用期合同协议书范本「标准版」
- 2025珠宝首饰购销合同范本
- 2025酒店管理租赁合同范本
- 婚内财产协议书范本(正式文本)
- 公告知识培训课件
- 搭建帐篷安全知识培训班课件
- 2025设备租赁合同补充协议范本
- 公司财务知识培训视课件
- 江苏南通开放大学招聘笔试真题2024
- 2025年甘肃省高考历史试卷真题(含答案解析)
- 食堂肉类备货方案(3篇)
- 智能微电网应用技术课件
- DB42T 1497-2019 公路工程地质调绘技术规程
- 光谷华科附小数学试卷
- 中国2030年能源电力发展规划研究及2060年展望
- 外协供应商品质管理制度
- BIM技术在绿色建筑碳足迹计算与优化中的应用研究
- 易制毒考试试题及答案
- 2025至2030中国普惠金融行业产业运行态势及投资规划深度研究报告
评论
0/150
提交评论