《VisualC++ 面向对象与可视化程序设计》实验报告
实验二 :菜单显示实例
班级:XXX
姓名:XXX
学号:XXX
一、实验目的
了解Windows图形处理的基本原理。 掌握Windows图形和文本输出。
二、实验内容
MFC曲线绘制实现
操作结果
三、程序代码
由于代码过长,以下只列出了有作修改的代码
E09620129_杨一诺_实验三View.h
// E09620129_杨一诺_实验三View.h : interface of the CE09620129__View class //
/////////////////////////////////////////////////////////////////////////////
#if !defined(AFX_E09620129__VIEW_H__C617D477_C63D_46AC_B76A_1E594A57B377__INCLUDED_)
#define AFX_E09620129__VIEW_H__C617D477_C63D_46AC_B76A_1E594A57B377__INCLUDED_
#if _MSC_VER > 1000 #pragma once
#endif // _MSC_VER > 1000 const double PI=3.1416;
class CE09620129__View : public CView {
protected: // create from serialization only CE09620129__View(); DECLARE_DYNCREATE(CE09620129__View)
// Attributes public: CE09620129__Doc* GetDocument();
// Operations public:
// Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CE09620129__View) 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: void drawaxis(CDC* pDC); void drawline(CDC* pDC,int x1,int y1,int x2,int y2); void drawcurve(CDC* pDC); double calcos(int x); double calsin(int x); COLORREF m_col; int m_wid; int m_typ; int m_able; virtual ~CE09620129__View(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif
protected:
// Generated message map functions protected: //{{AFX_MSG(CE09620129__View) afx_msg void OnSin(); afx_msg void OnCos(); afx_msg void OnWidsp2(); afx_msg void OnUpdateWidsp3(CCmdUI* pCmdUI); afx_msg void OnColr(); afx_msg void OnColb(); afx_msg void OnWidsp3(); afx_msg void OnUpdateColr(CCmdUI* pCmdUI); afx_msg void OnUpdateColb(CCmdUI* pCmdUI); afx_msg void OnUpdateWidsp2(CCmdUI* pCmdUI); //}}AFX_MSG DECLARE_MESSAGE_MAP() };
#ifndef _DEBUG // debug version in E09620129_杨一诺_实验三View.cpp inline CE09620129__Doc* CE09620129__View::GetDocument() { return (CE09620129__Doc*)m_pDocument; } #endif
E09620129_杨一诺_实验三View.cpp
#include \"stdafx.h\"
#include \"E09620129_杨一诺_实验三.h\"
#include \"E09620129_杨一诺_实验三Doc.h\" #include \"E09620129_杨一诺_实验三View.h\"
#ifdef _DEBUG
#define new DEBUG_NEW #undef THIS_FILE
static char THIS_FILE[] = __FILE__; #endif
#include \"math.h\" #include \"MainFrm.h\"
///////////////////////////////////////////////////////////////////////////// // CE09620129__View
IMPLEMENT_DYNCREATE(CE09620129__View, CView)
BEGIN_MESSAGE_MAP(CE09620129__View, CView)
//{{AFX_MSG_MAP(CE09620129__View) ON_COMMAND(ID_SIN, OnSin) ON_COMMAND(ID_COS, OnCos) ON_COMMAND(ID_WIDSP2, OnWidsp2)
ON_UPDATE_COMMAND_UI(ID_WIDSP3, OnUpdateWidsp3) ON_COMMAND(ID_COLR, OnColr) ON_COMMAND(ID_COLB, OnColb) ON_COMMAND(ID_WIDSP3, OnWidsp3)
ON_UPDATE_COMMAND_UI(ID_COLR, OnUpdateColr) ON_UPDATE_COMMAND_UI(ID_COLB, OnUpdateColb) ON_UPDATE_COMMAND_UI(ID_WIDSP2, OnUpdateWidsp2) //}}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()
///////////////////////////////////////////////////////////////////////////// // CE09620129__View construction/destruction
CE09620129__View::CE09620129__View() { }
CE09620129__View::~CE09620129__View() {
// TODO: add construction code here m_typ=0; //初始时窗口中不显示曲线 m_col=RGB(0,0,0); //颜色初始为黑色 m_wid=1; //线宽初始为1
m_able=0; //初始时除\"正弦\"和\"余弦\"菜单项外,新增的其他菜单项均//为不可用
}
BOOL CE09620129__View::PreCreateWindow(CREATESTRUCT& cs) { }
///////////////////////////////////////////////////////////////////////////// // CE09620129__View drawing
void CE09620129__View::OnDraw(CDC* pDC) { }
///////////////////////////////////////////////////////////////////////////// // CE09620129__View printing
BOOL CE09620129__View::OnPreparePrinting(CPrintInfo* pInfo) { }
void CE09620129__View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { }
void CE09620129__View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) {
// TODO: add cleanup after printing
// TODO: add extra initialization before printing // default preparation
return DoPreparePrinting(pInfo); CE09620129__Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc);drawcurve(pDC);
// TODO: add draw code for native data here return CView::PreCreateWindow(cs);
// TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs
}
///////////////////////////////////////////////////////////////////////////// // CE09620129__View diagnostics
#ifdef _DEBUG
void CE09620129__View::AssertValid() const { }
void CE09620129__View::Dump(CDumpContext& dc) const { }
CE09620129__Doc* CE09620129__View::GetDocument() // non-debug version is inline { }
#endif //_DEBUG
///////////////////////////////////////////////////////////////////////////// // CE09620129__View message handlers
double CE09620129__View::calsin(int x) { }
double CE09620129__View::calcos(int x) {
double y; double y;
y=sin(x*PI/180); //计算正弦 return y;
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CE09620129__Doc))); return (CE09620129__Doc*)m_pDocument; CView::Dump(dc); CView::AssertValid();
}
y=cos(x*PI/180); //计算余弦 return y;
void CE09620129__View::drawcurve(CDC *pDC) { 确定 }
void CE09620129__View::drawline(CDC *pDC, int x1, int y1, int x2, int y2) { }
void CE09620129__View::drawaxis(CDC *pDC) {
pDC->MoveTo(x1,y1); pDC->LineTo(x2,y2);
drawaxis(pDC); //调用成员函数绘制坐标系 CPoint point[750]; CPen mypen,*oldpen;
mypen.CreatePen(PS_SOLID,m_wid,m_col); //创建实线画笔、线宽及颜//色由参数m_wid 和m_col oldpen=pDC->SelectObject(&mypen); for(int i=0;i<722;i++) {
point[i].x=i+50;
if(m_typ==1) //根据参数m_typ 的值确定曲线类型 point[i].y=(int)(150-calsin(i)*100); else if(m_typ==2)
point[i].y=(int)(150-calcos(i)*100); }
for(i=0;i<721;i++)
drawline(pDC,point[i].x,point[i].y,point[i+1].x,point[i+1].y); //调用函数绘制曲线 pDC->SelectObject(oldpen); mypen.DeleteObject();
}
CString str;
CFont myfont,*oldfont;
myfont.CreatePointFont(80,\"Arial\定义坐标刻度字体 oldfont=pDC->SelectObject(&myfont); drawline(pDC,50,10,50,290); //绘制Y 轴 drawline(pDC,45,150,780,150); //绘制X 轴 for(int i=50;i<780;i=i+90) {
drawline(pDC,i,145,i,150); //绘制X 轴上刻度线 str.Format(\"%d\
if((i-50)!=0) pDC->TextOut(i,155,str); //输出X 轴上刻度值 }
for(i=0;i<11;i++) //绘制Y 轴上刻度线,输出刻度值 {
drawline(pDC,50,50+20*i,55,50+20*i); str.Format(\"%.2f\
if(i!=5) pDC->TextOut(15,45+20*i,str); else pDC->TextOut(15,45+20*i,\"0\"); }
pDC->SelectObject(oldfont); myfont.DeleteObject();
void CE09620129__View::OnSin() {
m_typ=1; //选择正弦曲线
m_able=1; //不可用菜单项变为可用 Invalidate(); //更新视窗
// TODO: Add your command handler code here }
void CE09620129__View::OnCos() {
m_typ=2; m_able=1;
Invalidate();
// TODO: Add your command handler code here }
void CE09620129__View::OnWidsp2() {
m_wid=2; //设置线宽为2
Invalidate();
// TODO: Add your command handler code here }
void CE09620129__View::OnUpdateWidsp3(CCmdUI* pCmdUI) {
pCmdUI->Enable(m_able);
if(m_wid==3)
pCmdUI->SetCheck(true); else
pCmdUI->SetCheck(false);
// TODO: Add your command update UI handler code here }
void CE09620129__View::OnColr() {
m_col=RGB(255,0,0); //选择红色
Invalidate();
// TODO: Add your command handler code here }
void CE09620129__View::OnColb() {
m_col=RGB(0,0,255);
Invalidate();
// TODO: Add your command handler code here }
void CE09620129__View::OnWidsp3() {
m_wid=3;
Invalidate();
// TODO: Add your command handler code here }
void CE09620129__View::OnUpdateColr(CCmdUI* pCmdUI) {
pCmdUI->Enable(m_able);
if(m_col==RGB(255,0,0)) pCmdUI->SetCheck(true); else
pCmdUI->SetCheck(false);
// TODO: Add your command update UI handler code here }
void CE09620129__View::OnUpdateColb(CCmdUI* pCmdUI) {
pCmdUI->Enable(m_able); //\"红色\"菜单项变为可用
if(m_col==RGB(0,0,255))
pCmdUI->SetCheck(true); //\"红色\"菜单项加标记 else
pCmdUI->SetCheck(false);
// TODO: Add your command update UI handler code here }
void CE09620129__View::OnUpdateWidsp2(CCmdUI* pCmdUI) {
pCmdUI->Enable(m_able);
if(m_wid==2)
pCmdUI->SetCheck(true); else
pCmdUI->SetCheck(false);
// TODO: Add your command update UI handler code here }
CE09620129__View
// E09620129_杨一诺_实验三View.cpp : implementation of the CE09620129__View class //
#include \"stdafx.h\"
#include \"E09620129_杨一诺_实验三.h\"
#include \"E09620129_杨一诺_实验三Doc.h\" #include \"E09620129_杨一诺_实验三View.h\"
#ifdef _DEBUG
#define new DEBUG_NEW #undef THIS_FILE
static char THIS_FILE[] = __FILE__; #endif
#include \"math.h\" #include \"MainFrm.h\"
///////////////////////////////////////////////////////////////////////////// // CE09620129__View
IMPLEMENT_DYNCREATE(CE09620129__View, CView)
BEGIN_MESSAGE_MAP(CE09620129__View, CView) //{{AFX_MSG_MAP(CE09620129__View) ON_COMMAND(ID_SIN, OnSin) ON_COMMAND(ID_COS, OnCos) ON_COMMAND(ID_WIDSP2, OnWidsp2) ON_UPDATE_COMMAND_UI(ID_WIDSP3, OnUpdateWidsp3) ON_COMMAND(ID_COLR, OnColr) ON_COMMAND(ID_COLB, OnColb) ON_COMMAND(ID_WIDSP3, OnWidsp3) ON_UPDATE_COMMAND_UI(ID_COLR, OnUpdateColr) ON_UPDATE_COMMAND_UI(ID_COLB, OnUpdateColb) ON_UPDATE_COMMAND_UI(ID_WIDSP2, OnUpdateWidsp2) //}}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()
///////////////////////////////////////////////////////////////////////////// // CE09620129__View construction/destruction
CE09620129__View::CE09620129__View() { m_typ=0; //初始时窗口中不显示曲线 m_col=RGB(0,0,0); //颜色初始为黑色 m_wid=1; //线宽初始为1 m_able=0; //初始时除\"正弦\"和\"余弦\"菜单项外,新增的其他菜单项均//为不可用 // TODO: add construction code here }
CE09620129__View::~CE09620129__View() { }
BOOL CE09620129__View::PreCreateWindow(CREATESTRUCT& cs)#include \"stdafx.h\" #include \"E09620129_杨一诺_实验三.h\"
#include \"E09620129_杨一诺_实验三Doc.h\" #include \"E09620129_杨一诺_实验三View.h\"
#ifdef _DEBUG
#define new DEBUG_NEW #undef THIS_FILE
static char THIS_FILE[] = __FILE__; #endif
#include \"math.h\" #include \"MainFrm.h\"
///////////////////////////////////////////////////////////////////////////// // CE09620129__View
IMPLEMENT_DYNCREATE(CE09620129__View, CView)
BEGIN_MESSAGE_MAP(CE09620129__View, CView) //{{AFX_MSG_MAP(CE09620129__View) ON_COMMAND(ID_SIN, OnSin) ON_COMMAND(ID_COS, OnCos) ON_COMMAND(ID_WIDSP2, OnWidsp2) ON_UPDATE_COMMAND_UI(ID_WIDSP3, OnUpdateWidsp3)
ON_COMMAND(ID_COLR, OnColr) ON_COMMAND(ID_COLB, OnColb) ON_COMMAND(ID_WIDSP3, OnWidsp3) ON_UPDATE_COMMAND_UI(ID_COLR, OnUpdateColr) ON_UPDATE_COMMAND_UI(ID_COLB, OnUpdateColb) ON_UPDATE_COMMAND_UI(ID_WIDSP2, OnUpdateWidsp2) //}}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()
///////////////////////////////////////////////////////////////////////////// // CE09620129__View construction/destruction
CE09620129__View::CE09620129__View() { m_typ=0; //初始时窗口中不显示曲线 m_col=RGB(0,0,0); //颜色初始为黑色 m_wid=1; //线宽初始为1 m_able=0; //初始时除\"正弦\"和\"余弦\"菜单项外,新增的其他菜单项均//为不可用 // TODO: add construction code here }
CE09620129__View::~CE09620129__View() { }
BOOL CE09620129__View::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); }
///////////////////////////////////////////////////////////////////////////// // CE09620129__View drawing
void CE09620129__View::OnDraw(CDC* pDC) { CE09620129__Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc);drawcurve(pDC); // TODO: add draw code for native data here }
///////////////////////////////////////////////////////////////////////////// // CE09620129__View printing
BOOL CE09620129__View::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); }
void CE09620129__View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing }
void CE09620129__View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing }
///////////////////////////////////////////////////////////////////////////// // CE09620129__View diagnostics
#ifdef _DEBUG
void CE09620129__View::AssertValid() const { CView::AssertValid(); }
void CE09620129__View::Dump(CDumpContext& dc) const { CView::Dump(dc); }
CE09620129__Doc* CE09620129__View::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CE09620129__Doc))); return (CE09620129__Doc*)m_pDocument; }
#endif //_DEBUG
///////////////////////////////////////////////////////////////////////////// // CE09620129__View message handlers
double CE09620129__View::calsin(int x) { double y; y=sin(x*PI/180); //计算正弦 return y; }
double CE09620129__View::calcos(int x) {
double y; y=cos(x*PI/180); //计算余弦 return y; }
void CE09620129__View::drawcurve(CDC *pDC) { drawaxis(pDC); //调用成员函数绘制坐标系 CPoint point[750]; CPen mypen,*oldpen; mypen.CreatePen(PS_SOLID,m_wid,m_col); //创建实线画笔、线宽及颜//色由参数m_wid 和m_col 确定 oldpen=pDC->SelectObject(&mypen); for(int i=0;i<722;i++) { point[i].x=i+50; if(m_typ==1) //根据参数m_typ 的值确定曲线类型 point[i].y=(int)(150-calsin(i)*100); else if(m_typ==2) point[i].y=(int)(150-calcos(i)*100); } for(i=0;i<721;i++) drawline(pDC,point[i].x,point[i].y,point[i+1].x,point[i+1].y); //调用函数绘制曲线 pDC->SelectObject(oldpen); mypen.DeleteObject(); }
void CE09620129__View::drawline(CDC *pDC, int x1, int y1, int x2, int y2) { pDC->MoveTo(x1,y1); pDC->LineTo(x2,y2); }
void CE09620129__View::drawaxis(CDC *pDC) { CString str; CFont myfont,*oldfont; myfont.CreatePointFont(80,\"Arial\定义坐标刻度字体 oldfont=pDC->SelectObject(&myfont); drawline(pDC,50,10,50,290); //绘制Y 轴 drawline(pDC,45,150,780,150); //绘制X 轴 for(int i=50;i<780;i=i+90) { drawline(pDC,i,145,i,150); //绘制X 轴上刻度线 str.Format(\"%d\ if((i-50)!=0) pDC->TextOut(i,155,str); //输出X 轴上刻度值 } for(i=0;i<11;i++) //绘制Y 轴上刻度线,输出刻度值
{ drawline(pDC,50,50+20*i,55,50+20*i); str.Format(\"%.2f\ if(i!=5) pDC->TextOut(15,45+20*i,str); else pDC->TextOut(15,45+20*i,\"0\"); } pDC->SelectObject(oldfont); myfont.DeleteObject(); }
void CE09620129__View::OnSin() { m_typ=1; //选择正弦曲线 m_able=1; //不可用菜单项变为可用 Invalidate(); //更新视窗
// TODO: Add your command handler code here }
void CE09620129__View::OnCos() { m_typ=2; m_able=1; Invalidate();
// TODO: Add your command handler code here }
void CE09620129__View::OnWidsp2() { m_wid=2; //设置线宽为2 Invalidate();
// TODO: Add your command handler code here }
void CE09620129__View::OnUpdateWidsp3(CCmdUI* pCmdUI) { pCmdUI->Enable(m_able); if(m_wid==3)
pCmdUI->SetCheck(true); else
pCmdUI->SetCheck(false);
// TODO: Add your command update UI handler code here }
void CE09620129__View::OnColr() {
m_col=RGB(255,0,0); //选择红色 Invalidate();
// TODO: Add your command handler code here }
void CE09620129__View::OnColb() { m_col=RGB(0,0,255); Invalidate();
// TODO: Add your command handler code here }
void CE09620129__View::OnWidsp3() { m_wid=3; Invalidate();
// TODO: Add your command handler code here }
void CE09620129__View::OnUpdateColr(CCmdUI* pCmdUI) { pCmdUI->Enable(m_able); if(m_col==RGB(255,0,0)) pCmdUI->SetCheck(true); else
pCmdUI->SetCheck(false);
// TODO: Add your command update UI handler code here }
void CE09620129__View::OnUpdateColb(CCmdUI* pCmdUI) { pCmdUI->Enable(m_able); //\"红色\"菜单项变为可用 if(m_col==RGB(0,0,255))
pCmdUI->SetCheck(true); //\"红色\"菜单项加标记 else
pCmdUI->SetCheck(false);
// TODO: Add your command update UI handler code here }
void CE09620129__View::OnUpdateWidsp2(CCmdUI* pCmdUI) { pCmdUI->Enable(m_able); if(m_wid==2)
pCmdUI->SetCheck(true); else
pCmdUI->SetCheck(false);
// TODO: Add your command update UI handler code here
}
四、运行结果及分析
五、实验心得
1.在创建菜单项的过程中,可以直接在菜单项名字后面输入(&快捷键)便可以创建出快捷键。
2.头文件放的位置应该得当,不然系统会无法读取。 3.在绘图函数构建完成之后,不要忘了在OnDraw函数中使用这些函数,不然坐标、曲线等将不会显示。
4.建立向导的时候要注意选择正确的类名。
因篇幅问题不能全部显示,请点此查看更多更全内容