简单画图程序课程设计报告.doc

上传人:文库蛋蛋多 文档编号:4192621 上传时间:2023-04-09 格式:DOC 页数:11 大小:47KB
返回 下载 相关 举报
简单画图程序课程设计报告.doc_第1页
第1页 / 共11页
简单画图程序课程设计报告.doc_第2页
第2页 / 共11页
简单画图程序课程设计报告.doc_第3页
第3页 / 共11页
简单画图程序课程设计报告.doc_第4页
第4页 / 共11页
简单画图程序课程设计报告.doc_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《简单画图程序课程设计报告.doc》由会员分享,可在线阅读,更多相关《简单画图程序课程设计报告.doc(11页珍藏版)》请在三一办公上搜索。

1、简单画图程序1 基本功能描述1) 在单文档菜单中,在菜单行中可插入一个菜单项,命名为绘图,在下拉菜单中可分别设置绘制的图形形状,如直线、矩形及椭圆,随手画选项,有1-5可供选择,按默认的画笔,画刷来绘制选择的图形。 2) 选择好图形后,通过鼠标可以绘制出相应的直线,矩形或椭圆,鼠标的按下确定图形的起点,鼠标的拖动则确定了图形的终点,即通过鼠标的拖动来决定图形的大小,当鼠标弹起,此图形则绘制完毕。2 设计思路 1) 对需要用到的变量进行初始化。2) 选择相应的图形之后就响应相应的消息处理函数。选择不同的线宽,即可改变画笔的属性。3) 鼠标的按下响应函数OnLButtonDown(),捕捉鼠标当前

2、位置得到起点的坐标,鼠标的拖动响函数OnMouseMove()改变终点的坐标,鼠标的弹起响应OnLButtonUp(),确定终点坐标,刷新,得到绘制图形。4) 选择图形或其它属性,可进行下一次绘制。 3 软件设计3.1 设计步骤1)创建单文档创建一个MFC AppWizardexe工程,命名为“draw”,并创建单文档。创建成功后,系统自动生成相应的类。2) 编辑菜单添加需要的菜单项,并在菜单的属性中设定好所对应的ID,(其中线宽菜单为弹出菜单,只需在菜单项目属性中的弹出选项前打勾即可,分隔线亦只需在菜单项目属性中选中分隔符选项即可)。建立类向导,在视图类CdrawView中,对各菜单项添加对

3、应的COMMAND消息处理函数,部分菜单项还添加对应的UPDATE_COMMAND_UI消息函数,。 建立类向导在插入处选择资源,新建工具栏, 在工具栏上添加相应的按钮,在属性处更改其ID, 4)在相应消息函数处添加代码,实现其功能建立类向导,在视图类CdrawView中,添加WM_PAINT消息,以及WM_LBUTTONDOWN、WM_LBUTTONUP、WM_MOUSEMOVE消息,并在其中添加相应的代码。 3.3 关键功能实现1)选择要绘制的图形在直线、矩形以及椭圆的消息处理函数里为shape分别赋值为1,2,3,在OnPaint()函数里添加3个if语句的代码来这实现这三个图形的绘制:

4、当选择画直线时,shape=1,则执行绘制直线代码,当选择画矩形时,shape =2,则执行绘制矩形的代码,当性选择画椭圆时,shape=3,则执行绘制椭圆的代码。5)绘制图形 鼠标左键按下,则捕获鼠标此时的位置坐标为画图的起点,此时在OnLButtonDown()函数里,将捕获的坐标值赋给定义作为起点的变量opoint;按下鼠标左键拖动鼠标时,捕捉鼠标当前位置坐标值为终点坐标值,此时在OnMouseMove()函数里,把捕获的坐标值赋给定义作为终点的变量cpoint,OnPaint()中绘图的代码中的坐标都得到了,进行绘图;鼠标左键弹起时,此时绘图结束,鼠标弹起时所在位置就是画图的最终终点位

5、置,画图结束后,刷新窗口,以便重新画图,此时执行的是OnLButtonUp()函数。4 结论与心得体会 这次课程设计我成功的完成了。通过本次课程设计,我完成了用移动鼠标对直线,矩形,椭圆的简单绘制工具的的制作,其中完成了绘图形状的选择的设置。 基于平时课外上机实验的练习,对菜单的创建,COMMAND消息处理函数和 UPDATE_COMMAND_UI消息函数的添加过程比较熟练,这部分还算简单。难的是代码的添加,对绘制图形的程序有大概的思路,但具体编写时变量的运用以及函数的调用设置。这次实验中关键就是确定画图起点及终点坐标,因为是用鼠标绘制图形,所以需用函数来捕获鼠标当前所在位置的坐标,因此我设置

6、了OnLButtonDown()函数来捕获起点坐标,OnMouseMove()函数来捕获鼠标移动是的当前位置坐标,OnLButtonUp()函数来捕获终点坐标。而绘制图形使用的函数是OnPaint(),将图形的起点和终点坐标和鼠标的位置坐标相联系后就能用鼠标来绘制图形了,所以在它们之间用opoint和cpoint两变量实现链接。另外我觉得将绘图语句集中在OnPaint()函数中,然后其他函数来调用OnPaint()函数,这样不容易引起混乱,同时程序更简单,更有条理,便于修改。这次课程设计虽只是做了一个极其简单的绘图工具的制作,但也让我从设计者的角度看到了设计程序以及电脑软件工具的实质及难度,看

7、似一个很简单的工具,其制作的背后工作是很大的,不光是图标,按钮的设置,程序的编写,代码的实现都是很精密且不容许一点错误的工作。平时我们使用的很顺手的一个小小工具,其开发的各项小号都不可小觑,由此,我们需要学的东西还太多太多。5. 参考文献Csdn 朱志强6 测试结果1)点击链接,编译,运行后的窗口所示。7 主要代码 / DrawView.cpp : implementation of the CDrawView class/#include stdafx.h#include Draw.h#include DrawDoc.h#include DrawView.h#ifdef _DEBUG#def

8、ine new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CDrawViewIMPLEMENT_DYNCREATE(CDrawView, CView)BEGIN_MESSAGE_MAP(CDrawView, CView)/AFX_MSG_MAP(CDrawView)ON_COMMAND(ID_DRAW_ELLIPSE, OnDrawEllipse)ON_COMMAND(ID_DRAW_RECTANGLE, OnDrawRectangle)ON_COMMAND(ID_DRAW_SKETCH, OnDrawSke

9、tch)ON_WM_LBUTTONDOWN()ON_WM_LBUTTONUP()ON_WM_MOUSEMOVE()ON_UPDATE_COMMAND_UI(ID_DRAW_ELLIPSE, OnUpdateDrawEllipse)ON_UPDATE_COMMAND_UI(ID_DRAW_RECTANGLE, OnUpdateDrawRectangle)ON_UPDATE_COMMAND_UI(ID_DRAW_SKETCH, OnUpdateDrawSketch)/AFX_MSG_MAP/ Standard printing commandsON_COMMAND(ID_FILE_PRINT, C

10、View:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_DIRECT, CView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView:OnFilePrintPreview)END_MESSAGE_MAP()/ CDrawView construction/destructionCDrawView:CDrawView()/ TODO: add construction code hereCDrawApp theApp;/ CDrawApp initializationBOOL CDrawApp:InitInstan

11、ce()AfxEnableControlContainer();/ Standard initialization/ If you are not using these features and wish to reduce the size/ of your final executable, you should remove from the following/ the specific initialization routines you do not need.#ifdef _AFXDLLEnable3dControls();/ Call this when using MFC

12、 in a shared DLL#elseEnable3dControlsStatic();/ Call this when linking to MFC statically#endif/ Change the registry key under which our settings are stored./ TODO: You should modify this string to be something appropriate/ such as the name of your company or organization.SetRegistryKey(_T(Local AppW

13、izard-Generated Applications);LoadStdProfileSettings(); / Load standard INI file options (including MRU)/ Register the applications document templates. Document templates/ serve as the connection between documents, frame windows and views.CSingleDocTemplate* pDocTemplate;pDocTemplate = new CSingleDo

14、cTemplate(IDR_MAINFRAME,RUNTIME_CLASS(CDrawDoc),RUNTIME_CLASS(CMainFrame), / main SDI frame windowRUNTIME_CLASS(CDrawView);AddDocTemplate(pDocTemplate);/ Parse command line for standard shell commands, DDE, file openCCommandLineInfo cmdInfo;ParseCommandLine(cmdInfo);/ Dispatch commands specified on

15、the command lineif (!ProcessShellCommand(cmdInfo)return FALSE;/ The one and only window has been initialized, so show and update it.m_pMainWnd-ShowWindow(SW_SHOW);m_pMainWnd-UpdateWindow();return TRUE;/ CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialogpublic:CAboutDlg();/ Dialog Da

16、ta/AFX_DATA(CAboutDlg)enum IDD = IDD_ABOUTBOX ;/AFX_DATA/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); / DDX/DDV support/AFX_VIRTUAL/ Implementationprotected:/AFX_MSG(CAboutDlg)/ No message handlers/AFX_MSGDECLARE_M

17、ESSAGE_MAP();CAboutDlg:CAboutDlg() : CDialog(CAboutDlg:IDD)/AFX_DATA_INIT(CAboutDlg)/AFX_DATA_INITvoid CAboutDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CAboutDlg)/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CAboutDlg, CDialog)/AFX_MSG_MAP(CAboutDlg)/ No message handlers/AFX_MS

18、G_MAPEND_MESSAGE_MAP()/ App command to run the dialogvoid CDrawApp:OnAppAbout()CAboutDlg aboutDlg;aboutDlg.DoModal();/ CDrawApp message handlers/ DrawDoc.cpp : implementation of the CDrawDoc class/#include stdafx.h#include Draw.h#include DrawDoc.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEsta

19、tic char THIS_FILE = _FILE_;#endif/ CDrawDocIMPLEMENT_DYNCREATE(CDrawDoc, CDocument)BEGIN_MESSAGE_MAP(CDrawDoc, CDocument)/AFX_MSG_MAP(CDrawDoc)/ NOTE - the ClassWizard will add and remove mapping macros here./ DO NOT EDIT what you see in these blocks of generated code!/AFX_MSG_MAPEND_MESSAGE_MAP()/

20、 CDrawDoc construction/destructionCDrawDoc:CDrawDoc()/ TODO: add one-time construction code hereCDrawDoc:CDrawDoc()BOOL CDrawDoc:OnNewDocument()if (!CDocument:OnNewDocument()return FALSE;/ TODO: add reinitialization code here/ (SDI documents will reuse this document)return TRUE;/ CDrawDoc serializat

21、ionvoid CDrawDoc:Serialize(CArchive& ar)if (ar.IsStoring()/ TODO: add storing code hereelse/ TODO: add loading code here/ CDrawDoc diagnostics#ifdef _DEBUGvoid CDrawDoc:AssertValid() constCDocument:AssertValid();void CDrawDoc:Dump(CDumpContext& dc) constCDocument:Dump(dc);#endif /_DEBUG/ CDrawDoc co

22、mmands/ MainFrm.cpp : implementation of the CMainFrame class/#include stdafx.h#include Draw.h#include MainFrm.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CMainFrameIMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)/AFX_MSG

23、_MAP(CMainFrame)/ NOTE - the ClassWizard will add and remove mapping macros here./ DO NOT EDIT what you see in these blocks of generated code !ON_WM_CREATE()/AFX_MSG_MAPEND_MESSAGE_MAP()static UINT indicators =ID_SEPARATOR, / status line indicatorID_INDICATOR_CAPS,ID_INDICATOR_NUM,ID_INDICATOR_SCRL,

24、;/ CMainFrame construction/destructionCMainFrame:CMainFrame()/ TODO: add member initialization code hereCMainFrame:CMainFrame()int CMainFrame:OnCreate(LPCREATESTRUCT lpCreateStruct)if (CFrameWnd:OnCreate(lpCreateStruct) = -1)return -1;if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISI

25、BLE | CBRS_TOP| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) |!m_wndToolBar.LoadToolBar(IDR_MAINFRAME)TRACE0(Failed to create toolbarn);return -1; / fail to createif (!m_wndStatusBar.Create(this) |!m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT)TRACE0(Faile

26、d to create status barn);return -1; / fail to create/ TODO: Delete these three lines if you dont want the toolbar to/ be dockablem_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);EnableDocking(CBRS_ALIGN_ANY);DockControlBar(&m_wndToolBar);return 0;BOOL CMainFrame:PreCreateWindow(CREATESTRUCT& cs)if( !CFram

27、eWnd:PreCreateWindow(cs) )return FALSE;/ TODO: Modify the Window class or styles here by modifying/ the CREATESTRUCT csreturn TRUE;/ CMainFrame diagnostics#ifdef _DEBUGvoid CMainFrame:AssertValid() constCFrameWnd:AssertValid();void CMainFrame:Dump(CDumpContext& dc) constCFrameWnd:Dump(dc);#endif /_DEBUG/ CMainFrame message handlers

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 办公文档 > 其他范文


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号