版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实习题目一一、【需求规格说明】1、题目描述: 利用鼠标在视图上绘制线,能够设置线的颜色和宽度; 能够用鼠标拾取绘制的某条线,被选中的线能够高亮显示,并且弹出对话框,输入线的属性保存起来; 能够将绘制的线数据保存在Line.dat文本文件中; 打开线数据文件Line.dat,可以显示出文件里面的线图形。2、问题分析: 通过获取鼠标在显示界面的坐标,运用MoveTo()以及LineTO()函数,绘制出画线;通过调用GetColor()函数,调用系统的调色板,将获得的信息传到程序中,改变画线的颜色;通过改变宽度函数的变量,来改变画线的宽度。 首先获取鼠标的坐标,通过计算,在右击鼠标后,在鼠标有限的距
2、离之内的画线有效,然后选定,然后再调用相应的函数来改变选定直线的属性。 用CFile 定义一个对象,通过对象调用Open()函数来打开保存的文件。文件的保存是先用Open()函数,然后再保存要保存的数据。二、【算法设计】、基本思想在视图中画好线后,存在文档里面,再从文档存到磁盘,打开文件时,从磁盘到文档,由OnDraw()函数调用重绘函数进行重绘。程序设计:/ DrawView.h : interface of the CDrawView class#if !defined(AFX_DRAWVIEW_H_D9CAA283_2CAD_4A04_86EE_42FBE886F459_INCLUDED
3、_)#define AFX_DRAWVIEW_H_D9CAA283_2CAD_4A04_86EE_42FBE886F459_INCLUDED_#if _MSC_VER 1000#pragma once#endif / _MSC_VER 1000class CDrawView : public CViewpublic:void ChangeLine(CPoint point);public:BOOL m_bIsLine; /记录是否处于画线状态CPoint point0; /记录线段起点坐标int m_nStep; /记录画线的操作步数CPoint point1; /记录线段终点坐标 COLOR
4、REF m_nLineColor; /线条颜色int m_nLineWidth; /线条宽度int i; /循环控制变量BOOL ClickSaveButton; /点击自己的保存时给它赋值为假int drawmodel; /划线模式 0:D-U-D 1:D-D CString filename; /用于存取已绘制的图像的文件名BOOL m_nChange; /选中线,改变属性BOOL m_nChangeReDraw; /选中进行重绘/protected: / create from serialization onlyCDrawView();DECLARE_DYNCREATE(CDrawVi
5、ew)/ Attributespublic:CDrawDoc* GetDocument(); / Operationspublic:/ Overrides/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CDrawView)public:virtual void OnDraw(CDC* pDC); / overridden to draw this viewvirtual BOOL PreCreateWindow(CREATESTRUCT& cs);protected:virtual BOOL OnPreparePri
6、nting(CPrintInfo* pInfo);virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);/AFX_VIRTUAL/ Implementationpublic:virtual CDrawView();#ifdef _DEBUGvirtual void AssertValid() const;virtual void Dump(CDumpContext& dc) const;#endifprotected:/
7、Generated message map functionsprotected:/AFX_MSG(CDrawView)afx_msg void OnDrawLine();afx_msg void OnUpdateDrawLine(CCmdUI* pCmdUI);afx_msg void OnSetColor();afx_msg void OnSetWidth();afx_msg void OnLButtonDown(UINT nFlags, CPoint point);afx_msg void OnMouseMove(UINT nFlags, CPoint point);afx_msg BO
8、OL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);afx_msg void OnRButtonDown(UINT nFlags, CPoint point);afx_msg void OnDeleteLine();afx_msg void OnSave();afx_msg void OnOpen();afx_msg void OnClear();afx_msg void OnLButtonUp(UINT nFlags, CPoint point);afx_msg void OnDownUpDown();afx_msg void On
9、DownUP();afx_msg void OnChange();afx_msg void OnUpdateChange(CCmdUI* pCmdUI);afx_msg void OnChangeReDraw();/AFX_MSGDECLARE_MESSAGE_MAP();#ifndef _DEBUG / debug version in DrawView.cppinline CDrawDoc* CDrawView:GetDocument() return (CDrawDoc*)m_pDocument; #endif/AFX_INSERT_LOCATION/ Microsoft Visual
10、C+ will insert additional declarations immediately before the previous line.#endif / DrawDoc.h : interface of the CDrawDoc class#if !defined(AFX_DRAWDOC_H_1959C5D8_A40D_4E17_AE59_A7650B71CC9F_INCLUDED_)#define AFX_DRAWDOC_H_1959C5D8_A40D_4E17_AE59_A7650B71CC9F_INCLUDED_#if _MSC_VER 1000#pragma once#
11、endif / _MSC_VER 1000#include using namespace std;class CDrawDoc : public CDocumentpublic:void SetWindowCwnd(CWnd *pcWnd); CWnd* m_cWndView;public:vector distance; /距离vector StorePoint; /存入点COLORREF color50;int width50;/=void DrawLines(CDC *pDC);protected: / create from serialization onlyCDrawDoc();
12、DECLARE_DYNCREATE(CDrawDoc)/ Attributespublic:/ Operationspublic:/ Overrides/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CDrawDoc)public:virtual BOOL OnNewDocument();virtual void Serialize(CArchive& ar);/AFX_VIRTUAL/ Implementationpublic:virtual CDrawDoc();#ifdef _DEBUGvirtual void
13、 AssertValid() const;virtual void Dump(CDumpContext& dc) const;#endifprotected:/ Generated message map functionsprotected:/AFX_MSG(CDrawDoc)/AFX_MSGDECLARE_MESSAGE_MAP();/AFX_INSERT_LOCATION/ Microsoft Visual C+ will insert additional declarations immediately before the previous line.#endif / !defin
14、ed(AFX_DRAWDOC_H_1959C5D8_A40D_4E17_AE59_A7650B71CC9F_INCLUDED_)/ DrawDoc.cpp : implementation of the CDrawDoc class#include stdafx.h#include Draw.h#include DrawDoc.h#include DrawView.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CDrawDocIMPLEMENT_DYNCREAT
15、E(CDrawDoc, CDocument)BEGIN_MESSAGE_MAP(CDrawDoc, CDocument)/AFX_MSG_MAP(CDrawDoc)/AFX_MSG_MAPEND_MESSAGE_MAP()/ CDrawDoc construction/destructionCDrawDoc:CDrawDoc()/ TODO: add one-time construction code hereCDrawDoc:CDrawDoc()BOOL CDrawDoc:OnNewDocument()if (!CDocument:OnNewDocument()return FALSE;/
16、 TODO: add reinitialization code here/ (SDI documents will reuse this document)return TRUE;/ CDrawDoc serializationvoid CDrawDoc:Serialize(CArchive& ar)if (ar.IsStoring() / TODO: add storing code here int NumOfPoint=(int)StorePoint.size(); arNumOfPoint; for(int i=0;iNumOfPoint;i+=2) arStorePointiSto
17、rePointi+1; arwidthiNumOfPoint; for(int i=0;ipoint0point1; StorePoint.push_back(CPoint(point0); StorePoint.push_back(CPoint(point1); arwidthicolori;/ CDrawDoc diagnostics#ifdef _DEBUGvoid CDrawDoc:AssertValid() constCDocument:AssertValid();void CDrawDoc:Dump(CDumpContext& dc) constCDocument:Dump(dc)
18、;#endif /_DEBUG/ CDrawDoc commandsvoid CDrawDoc:DrawLines(CDC *pDC)int NumOfPoint=(int)StorePoint.size();for (int j=0;jSelectObject(& newPen);/将画笔选入设备环境,同时保存旧画笔 /=/pDC-MoveTo(StorePointj);pDC-LineTo(StorePointj+1);pDC-SelectObject(pOldPen); /绘图完毕后恢复原有环境void SetWindowCwnd(CWnd *pcWnd) CWnd* m_cWndVie
19、w;m_cWndView = pcWnd; / DrawView.cpp : implementation of the CDrawView class#include stdafx.h#include Draw.h#include math.h#include DrawDoc.h#include DrawView.h#include SetWidthDlg.h#include NameFileDlg.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CDrawVi
20、ewIMPLEMENT_DYNCREATE(CDrawView, CView)BEGIN_MESSAGE_MAP(CDrawView, CView)/AFX_MSG_MAP(CDrawView)ON_COMMAND(ID_DrawLine, OnDrawLine)ON_UPDATE_COMMAND_UI(ID_DrawLine, OnUpdateDrawLine)ON_COMMAND(ID_SetColor, OnSetColor)ON_COMMAND(ID_SetWidth, OnSetWidth)ON_WM_LBUTTONDOWN()ON_WM_MOUSEMOVE()ON_WM_SETCU
21、RSOR()ON_WM_RBUTTONDOWN()ON_COMMAND(ID_DeleteLine, OnDeleteLine)ON_COMMAND(ID_SAVE, OnSave)ON_COMMAND(ID_OPEN, OnOpen)ON_COMMAND(ID_CLEAR, OnClear)ON_WM_LBUTTONUP()ON_COMMAND(ID_DownUpDown, OnDownUpDown)ON_COMMAND(ID_DownUP, OnDownUP)ON_COMMAND(ID_Change, OnChange)ON_UPDATE_COMMAND_UI(ID_Change, OnU
22、pdateChange)ON_COMMAND(ID_ChangeReDraw, OnChangeReDraw)/AFX_MSG_MAP/ Standard printing commandsON_COMMAND(ID_FILE_PRINT, CView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_DIRECT, CView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView:OnFilePrintPreview)END_MESSAGE_MAP()/ CDrawView construction/destructi
23、onCDrawView:CDrawView()/ TODO: add construction code herem_bIsLine=FALSE; /开始没有处于画线状态 point0=point1=(0,0);/线段的起点坐标初始为0m_nStep=0; /操作步数初始为0m_nLineWidth=1; /线宽为1i=0; /循环控制变量ClickSaveButton=TRUE; /是否已经保存filename=_T(); /存磁盘时的文件名m_nChange=FALSE; /是否选中m_nChangeReDraw=FALSE; /是否每次进行重绘CDrawView:CDrawView()B
24、OOL CDrawView:PreCreateWindow(CREATESTRUCT& cs)/ TODO: Modify the Window class or styles here by modifying/ the CREATESTRUCT csreturn CView:PreCreateWindow(cs);/ CDrawView drawingvoid CDrawView:OnDraw(CDC* pDC)CDrawDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);/ TODO: add draw code for native data he
25、repDoc-DrawLines(pDC);/ CDrawView printingBOOL CDrawView:OnPreparePrinting(CPrintInfo* pInfo)/ default preparationreturn DoPreparePrinting(pInfo);void CDrawView:OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ TODO: add extra initialization before printingvoid CDrawView:OnEndPrinting(CDC* /*pDC
26、*/, CPrintInfo* /*pInfo*/)/ TODO: add cleanup after printing/ CDrawView diagnostics#ifdef _DEBUGvoid CDrawView:AssertValid() constCView:AssertValid();void CDrawView:Dump(CDumpContext& dc) constCView:Dump(dc);CDrawDoc* CDrawView:GetDocument() / non-debug version is inlineASSERT(m_pDocument-IsKindOf(R
27、UNTIME_CLASS(CDrawDoc);return (CDrawDoc*)m_pDocument;#endif /_DEBUG/ CDrawView message handlersvoid CDrawView:OnDrawLine() / TODO: Add your command handler code hereif(drawmodel != 0 & drawmodel != 1)MessageBox(请下先在视图中画图);if(m_bIsLine=FALSE)m_bIsLine=TRUE; /允许画线elsem_bIsLine=FALSE; /中止画线void CDrawVi
28、ew:OnUpdateDrawLine(CCmdUI* pCmdUI) / TODO: Add your command update UI handler code herepCmdUI-SetCheck(m_bIsLine); /控制用户界面更(新菜单项和工具栏的画线按钮处于选中未选中状态)void CDrawView:OnSetColor() / TODO: Add your command handler code hereCColorDialog dlg; /系统颜色对话框if(dlg.DoModal()=IDOK)m_nLineColor=dlg.GetColor(); void
29、CDrawView:OnSetWidth() / TODO: Add your command handler code hereSetWidthDlg dlg;if(dlg.DoModal()=IDOK)m_nLineWidth=dlg.m_nLineWidth;void CDrawView:OnLButtonDown(UINT nFlags, CPoint point) / TODO: Add your message handler code here and/or call defaultCDrawDoc* pDoc = GetDocument();CDC *pDC = GetDC()
30、;CClientDC dc(this); /获取设备环境if(m_nChange|m_nChangeReDraw)ChangeLine(point);else /= CPen newPen(PS_SOLID,m_nLineWidth,m_nLineColor); /画线前创建一支画笔 CPen *pOldPen=dc.SelectObject(& newPen); /将画笔选入设备环境,同时保存旧画笔 /=; if(m_bIsLine) /若处于画线状态 if(m_nStep = 0) point0=point; m_nStep+; /操作步数加1 pDoc-StorePoint.push_b
31、ack(point0); else if(m_nStep = 1 & drawmodel = 0) /如果是终点,则画线 dc.MoveTo(point0); dc.LineTo(point); point1=point; pDoc-StorePoint.push_back(point1); pDoc-colori=m_nLineColor; pDoc-widthi=m_nLineWidth; /= m_nStep=0; /画线完毕后,操作步数立即归0 point0=point1=(0,0); /画线完毕后,起点坐标立即归0 /一条线段画完后,终点坐标归0 i+=2; else dc.Sele
32、ctObject(pOldPen); / 绘图完毕后恢复原有环境 ReleaseDC(pDC);if(ClickSaveButton=FALSE) pDoc-SetModifiedFlag(TRUE); /表明文档被修改,当未保存文件而关闭程序时,就会弹出对话框询问是否保存CView:OnLButtonDown(nFlags, point);void CDrawView:OnMouseMove(UINT nFlags, CPoint point) / TODO: Add your message handler code here and/or call defaultCClientDC dc
33、(this); /获取设备环境CPen newPen(PS_SOLID,m_nLineWidth,m_nLineColor); /画线前创建一支画笔 CPen *pOldPen=dc.SelectObject(& newPen); /将画笔选入设备环境,同时保存旧画笔if(m_nStep!=0) /第一次点击鼠标之后,m_nStep=1,则拉出橡皮条dc.SetROP2(R2_NOT); /设置线条模式R2_NOTXORPEN/R2_NOTif(point1!=CPoint(0,0)/保留最后一次画的线,之前擦出(也就是重画一次)dc.MoveTo(point0);dc.LineTo(poin
34、t1); /更新终点的坐标 point1=point; /画出新的线段(最后一次画的那条) dc.MoveTo(point0); dc.LineTo(point1);CView:OnMouseMove(nFlags, point);/设置光标的函数BOOL CDrawView:OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) / TODO: Add your message handler code here and/or call default if(m_bIsLine) /若处于画线状态,则使用Windows系统的IDC_CROSS
35、十字光标HCURSOR hCursor;if(m_nChange=true|m_nChangeReDraw=true) /需选中线条改变属性时把光标改为箭头hCursor=AfxGetApp()-LoadStandardCursor(IDC_ARROW);/加载光标else hCursor=AfxGetApp()-LoadStandardCursor(IDC_CROSS);/加载光标 SetCursor(hCursor); return TRUE;return CView:OnSetCursor(pWnd, nHitTest, message);/*IDC_CROSS 标志十字光标IDC_AR
36、ROW 标志箭头光标IDC_IBEAM I型光标IDC_NO 禁止光标IDC_APPSTARTING 标志箭头光标附带沙漏IDC_SIZEALL 四向箭头光标IDC_SIZENESW 东北,西南方向斜线箭头光标IDC_SIZENS 南北方向箭头光标IDC_SIZENWSE 西北,东南方向斜线箭头光标IDC_SIZEWE 东西方向箭头光标 IDC_UPARROW 向上箭头光标IDC_WAIT 沙漏光标 */void CDrawView:OnRButtonDown(UINT nFlags, CPoint point) / TODO: Add your message handler code he
37、re and/or call defaultm_bIsLine=FALSE; /m_nStep=0;i=0;CView:OnRButtonDown(nFlags, point);/删除所在屏幕上的线条void CDrawView:OnDeleteLine() / TODO: Add your command handler code hereCDrawDoc* pDoc = GetDocument(); CDC *pDC = GetDC();pDC-SetROP2(R2_NOT);pDoc-DrawLines(pDC); OnClear(); i=0; /相当于清空color和width pD
38、oc-StorePoint.clear(); /清空存点 容器/用白颜色清空屏幕区域void CDrawView:OnClear() / TODO: Add your command handler code hereCDrawDoc* pDoc = GetDocument();CDC *pDC=GetDC(); CRect rect;this-GetClientRect(&rect);pDC-SetROP2(R2_WHITE);/(R2_BLACK);pDC-Rectangle(&rect);ReleaseDC(pDC);i=0; pDoc-StorePoint.clear();/存文件到磁
39、盘啊void CDrawView:OnSave() / TODO: Add your command handler code her NameFileDlg dlg;if(dlg.DoModal()=IDOK) filename=dlg.m_FileName;CDrawDoc* pDoc = GetDocument(); ClickSaveButton=TRUE; /退出画图程序时系统不会弹出是否保存的提示 CFile f;f.Open(filename,CFile:modeCreate|CFile:modeReadWrite); CArchive ar(&f,CArchive:store)
40、; int NumOfPoint=(int)pDoc-StorePoint.size();if(NumOfPoint!=0)arNumOfPoint; for(int i=0;iNumOfPoint;i+=2) arStorePointi)StorePointi+1); arwidthi)colori);ar.Close(); f.Close(); MessageBox(文件+filename+已经存好);elseMessageBox(对不起!你还没有画线);/打开磁盘文件void CDrawView:OnOpen() / TODO: Add your command handler code
41、 hereCDC *pDC = GetDC();CDrawDoc* pDoc = GetDocument();CFile f;if(f.Open(filename,CFile:modeRead)=FALSE)return;CArchive ar(&f,CArchive:load); if(!ar.IsStoring()int NumOfPoint=(int)(pDoc-StorePoint.size(); arNumOfPoint; for(int i=0;ipoint0point1; pDoc-StorePoint.push_back(point0); pDoc-StorePoint.pus
42、h_back(point1); ar(pDoc-widthi)(pDoc-colori);pDoc-DrawLines(pDC); ar.Close(); f.Close();void CDrawView:OnLButtonUp(UINT nFlags, CPoint point) / TODO: Add your message handler code here and/or call defaultCDrawDoc* pDoc =GetDocument();CClientDC dc(this); /获取设备环境/= CPen newPen(PS_SOLID,m_nLineWidth,m_
43、nLineColor); /画线前创建一支画笔CPen *pOldPen=dc.SelectObject(& newPen); /将画笔选入设备环境,同时保存旧画笔/=if(m_nStep = 1 & drawmodel = 1) /若处于画线状态dc.MoveTo(point0);dc.LineTo(point);point1=point;pDoc-StorePoint.push_back(point1);pDoc-colori=m_nLineColor;pDoc-widthi=m_nLineWidth;/=m_nStep=0; /画线完毕后,操作步数立即归0 point0=point1=(0,0);/画线完毕后,起点坐标立即归0 /一条线段画完后,终点坐标归0 i+=2;dc.SelectObject(pOldPen)
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2023年沼气专用发电装置项目评价分析报告
- 采购合同签订制度
- 不可抗力融资租赁合同
- 编外合同用工标准
- 智慧路灯系统解决方案
- 颈椎病手术前后配合
- 线粒体脑病的护理
- 山东省枣庄市台儿庄区2024-2025学年九年级上学期期中考试历史试题
- 辽宁省鞍山市海城市西部集团2024-2025学年七年级上学期11月期中生物学试题(含答案)
- 河南省邓州市2024-2025学年七年级上学期期中历史试题(含答案)
- 北京市《配电室安全管理规范》(DB11T 527-2021)地方标准
- 教科版小学科学三年级上册单元测试题附答案(全册)
- 生涯发展展示
- 第七讲社会主义现代化建设的教育、科技、人才战略教学课件
- 小学课爱国主义教育教案
- 安全护理:穿脱隔离衣
- 珠宝鉴赏智慧树知到答案章节测试2023年
- GB 4806.11-2016食品安全国家标准食品接触用橡胶材料及制品
- 最新:河北省另收费用一次性物品管理目录(2022版)
- 社区管理(第三版)教学课件汇总完整版电子教案
- 柴油发电机组技术规范书
评论
0/150
提交评论