资源描述
C++实习报告学生学籍成绩管理系统+画线(完整版)
(文档可以直接使用,也可根据实际需要修改使用,可编辑 欢迎下载)
实习题目一
一、【需求规格说明】
1、题目描述:
² 利用鼠标在视图上绘制线,能够设置线的颜色和宽度;
² 能够用鼠标拾取绘制的某条线,被选中的线能够高亮显示,并且弹出对话框,输入线的属性保存起来;
² 能够将绘制的线数据保存在Line.dat文本文件中;
² 打开线数据文件Line.dat,可以显示出文件里面的线图形。
2、问题分析:
⑴ 通过获取鼠标在显示界面的坐标,运用MoveTo()以及LineTO()函数,绘制出画线;通过调用GetColor()函数,调用系统的调色板,将获得的信息传到程序中,改变画线的颜色;通过改变宽度函数的变量,来改变画线的宽度。
⑵ 首先获取鼠标的坐标,通过计算,在右击鼠标后,在鼠标有限的距离之内的画线有效,然后选定,然后再调用相应的函数来改变选定直线的属性。
⑶ 用CFile 定义一个对象,通过对象调用Open()函数来打开保存的文件。文件的保存是先用Open()函数,然后再保存要保存的数据。
二、【算法设计】
⑴、基本思想
在视图中画好线后,存在文档里面,再从文档存到磁盘,打开文件时,从磁盘到文档,由OnDraw()函数调用重绘函数进行重绘。
⑵程序设计:
//////////////////////////////////////////////////////////
// DrawView.h : interface of the CDrawView class
#if !defined(AFX_DRAWVIEW_H__D9CAA283_2CAD_4A04_86EE_42FBE886F459__INCLUDED_)
#define AFX_DRAWVIEW_H__D9CAA283_2CAD_4A04_86EE_42FBE886F459__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CDrawView : public CView
{
public:
void ChangeLine(CPoint point);
public:
BOOL m_bIsLine; //记录是否处于画线状态
CPoint point0; //记录线段起点坐标
int m_nStep; //记录画线的操作步数
CPoint point1; //记录线段终点坐标
COLORREF 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 only
CDrawView();
DECLARE_DYNCREATE(CDrawView)
// Attributes
public:
CDrawDoc* GetDocument();
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CDrawView)
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CDrawView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{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 BOOL 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 OnDownUP();
afx_msg void OnChange();
afx_msg void OnUpdateChange(CCmdUI* pCmdUI);
afx_msg void OnChangeReDraw();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#ifndef _DEBUG // debug version in DrawView.cpp
inline CDrawDoc* CDrawView::GetDocument()
{ return (CDrawDoc*)m_pDocument; }
#endif
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual 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
#endif // _MSC_VER > 1000
#include <vector>
using namespace std;
class CDrawDoc : public CDocument
{
public:
void SetWindowCwnd(CWnd *pcWnd);
CWnd* m_cWndView;
public:
vector<double> distance; //距离
vector<CPoint> StorePoint; //存入点
COLORREF color[50];
int width[50];
//=======================
void DrawLines(CDC *pDC);
protected: // create from serialization only
CDrawDoc();
DECLARE_DYNCREATE(CDrawDoc)
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CDrawDoc)
public:
virtual BOOL OnNewDocument();
virtual void Serialize(CArchive& ar);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CDrawDoc();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(CDrawDoc)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(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_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDrawDoc
IMPLEMENT_DYNCREATE(CDrawDoc, CDocument)
BEGIN_MESSAGE_MAP(CDrawDoc, CDocument)
//{{AFX_MSG_MAP(CDrawDoc)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDrawDoc construction/destruction
CDrawDoc::CDrawDoc()
{
// TODO: add one-time construction code here
}
CDrawDoc::~CDrawDoc()
{
}
BOOL CDrawDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CDrawDoc serialization
void CDrawDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
int NumOfPoint=(int)StorePoint.size();
ar<<NumOfPoint;
for(int i=0;i<NumOfPoint;i+=2)
{
ar<<StorePoint[i]<<StorePoint[i+1];
ar<<width[i]<<color[i];
}
}
else
{
// TODO: add loading code here
int NumOfPoint=(int)StorePoint.size();
ar>>NumOfPoint;
for(int i=0;i<NumOfPoint;i+=2)
{
CPoint point0,point1;
ar>>point0>>point1;
StorePoint.push_back(CPoint(point0));
StorePoint.push_back(CPoint(point1));
ar>>width[i]>>color[i];
}
}
}
/////////////////////////////////////////////////////////////////////////////
// CDrawDoc diagnostics
#ifdef _DEBUG
void CDrawDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CDrawDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDrawDoc commands
void CDrawDoc::DrawLines(CDC *pDC)
{
int NumOfPoint=(int)StorePoint.size();
for (int j=0;j<NumOfPoint;j+=2)
{
//=================================
CPen newPen(PS_SOLID,width[j],color[j]);//画线前创建一支画笔
CPen *pOldPen=pDC->SelectObject(& newPen);//将画笔选入设备环境,同时保存旧画笔
//===================================
/////////////////////////////
pDC->MoveTo(StorePoint[j]);
pDC->LineTo(StorePoint[j+1]);
pDC->SelectObject(pOldPen);
//绘图完毕后恢复原有环境
}
}
void SetWindowCwnd(CWnd *pcWnd)
{
CWnd* m_cWndView;
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_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDrawView
IMPLEMENT_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_SETCURSOR()
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, OnUpdateChange)
ON_COMMAND(ID_ChangeReDraw, OnChangeReDraw)
//}}AFX_MSG_MAP
// Standard printing commands
ON_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/destruction
CDrawView::CDrawView()
{
// TODO: add construction code here
m_bIsLine=FALSE; //开始没有处于画线状态
point0=point1=(0,0);//线段的起点坐标初始为0
m_nStep=0; //操作步数初始为0
m_nLineWidth=1; //线宽为1
i=0; //循环控制变量
ClickSaveButton=TRUE; //是否已经保存
filename=_T(""); //存磁盘时的文件名
m_nChange=FALSE; //是否选中
m_nChangeReDraw=FALSE; //是否每次进行重绘
}
CDrawView::~CDrawView()
{
}
BOOL CDrawView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CDrawView drawing
void CDrawView::OnDraw(CDC* pDC)
{
CDrawDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
pDoc->DrawLines(pDC);
}
/////////////////////////////////////////////////////////////////////////////
// CDrawView printing
BOOL CDrawView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDrawView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CDrawView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CDrawView diagnostics
#ifdef _DEBUG
void CDrawView::AssertValid() const
{
CView::AssertValid();
}
void CDrawView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CDrawDoc* CDrawView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawDoc)));
return (CDrawDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDrawView message handlers
void CDrawView::OnDrawLine()
{
// TODO: Add your command handler code here
if(drawmodel != 0 && drawmodel != 1)
{
MessageBox("请下先在视图中画图");
}
if(m_bIsLine==FALSE)
{
m_bIsLine=TRUE; //允许画线
}
else
m_bIsLine=FALSE; //中止画线
}
void CDrawView::OnUpdateDrawLine(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_bIsLine); //控制用户界面更(新菜单项和工具栏的画线按钮处于选中\未选中状态)
}
void CDrawView::OnSetColor()
{
// TODO: Add your command handler code here
CColorDialog dlg; //系统颜色对话框
if(dlg.DoModal()==IDOK)
{
m_nLineColor=dlg.GetColor();
}
}
void CDrawView::OnSetWidth()
{
// TODO: Add your command handler code here
SetWidthDlg 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 default
CDrawDoc* pDoc = GetDocument();
CDC *pDC = GetDC();
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_back(point0);
}
else if(m_nStep == 1 && drawmodel == 0) //如果是终点,则画线
{
dc.MoveTo(point0);
dc.LineTo(point);
point1=point;
pDoc->StorePoint.push_back(point1);
pDoc->color[i]=m_nLineColor;
pDoc->width[i]=m_nLineWidth;
//=================================
m_nStep=0; //画线完毕后,操作步数立即归0
point0=point1=(0,0); //画线完毕后,起点坐标立即归0
//一条线段画完后,终点坐标归0
i+=2;
}
else
{}
}
dc.SelectObject(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 default
CClientDC dc(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_NOT
if(point1!=CPoint(0,0))
{
//保留最后一次画的线,之前擦出(也就是重画一次)
dc.MoveTo(point0);
dc.LineTo(point1);
}
//更新终点的坐标
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十字光标
{
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_ARROW 标志箭头光标
IDC_IBEAM I型光标
IDC_NO 禁止光标
IDC_APPSTARTING 标志箭头光标附带沙漏
IDC_SIZEALL 四向箭头光标
IDC_SIZENESW 东北,西南方向斜线箭头光标
IDC_SIZENS 南北方向箭头光标
IDC_SIZENWSE
展开阅读全文