⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exp32_1view.cpp

📁 mfc例题与练习第二版 例题与练习第二版
💻 CPP
字号:
// Exp32_1View.cpp : implementation of the CExp32_1View class
//

#include "stdafx.h"
#include "Exp32_1.h"

#include "Exp32_1Doc.h"
#include "Exp32_1View.h"
#include "GraphDlg.h"
#include <fstream.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CExp32_1View

IMPLEMENT_DYNCREATE(CExp32_1View, CView)

BEGIN_MESSAGE_MAP(CExp32_1View, CView)
	//{{AFX_MSG_MAP(CExp32_1View)
	ON_COMMAND(ID_GRAPH, OnGraph)
	ON_COMMAND(ID_SAVEFILE, OnSaveFile)
	ON_COMMAND(ID_READ_FILE, OnReadFile)
	ON_COMMAND(ID_SAVE_FILE, OnSaveFile)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CExp32_1View construction/destruction

CExp32_1View::CExp32_1View()
{
	// TODO: add construction code here

}

CExp32_1View::~CExp32_1View()
{
}

BOOL CExp32_1View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CExp32_1View drawing

void CExp32_1View::OnDraw(CDC* pDC)
{
	CExp32_1Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	CPen pen(PS_SOLID,0,m_color);				//构造当前颜色的画笔
	CPen *oldPen;
	oldPen = pDC->SelectObject(&pen);			//为CDC选中当前画笔
	if(m_type == 0){							//画直线
		pDC->MoveTo(m_start);
		pDC->LineTo(m_end);
	}
	else if(m_type ==1){						//画圆
		CRect rect(m_start.x - m_radius,m_start.y -m_radius,
			m_start.x + m_radius,m_start.y + m_radius);
		pDC->Ellipse(&rect);
	}
	pDC->SelectObject(oldPen);

}

/////////////////////////////////////////////////////////////////////////////
// CExp32_1View printing

BOOL CExp32_1View::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CExp32_1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CExp32_1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CExp32_1View diagnostics

#ifdef _DEBUG
void CExp32_1View::AssertValid() const
{
	CView::AssertValid();
}

void CExp32_1View::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CExp32_1Doc* CExp32_1View::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CExp32_1Doc)));
	return (CExp32_1Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CExp32_1View message handlers

void CExp32_1View::OnGraph() 
{
	// TODO: Add your command handler code here
	CGraphDlg dlg;
	if((dlg.DoModal())==IDOK){
		m_color = RGB(dlg.m_intRed, dlg.m_intGreen, dlg.m_intBlue);
		m_start.x = dlg.m_intX1 ;
		m_start.y = dlg.m_intY1;
		m_type = dlg.m_intType ; 			//确定类型
		if(m_type ==0){   				//直线
			m_end.x = dlg.m_intX2 ;
			m_end.y = dlg.m_intY2;
		}
		else m_radius = dlg.m_intX2;		//圆
	}
	Invalidate();             				//使视图重画,调用OnDraw函数

}

void CExp32_1View::OnSaveFile() 
{
	// TODO: Add your command handler code here
	fstream out;
	out.open("graph.txt",ios::out);
	out<<m_type<<' '<<m_color<<' '<<m_start.x<<' '<<m_start.y<<' '
		<<m_end.x<<' '<<m_end.y<<' '<<m_radius<<endl;
 	return;

}

void CExp32_1View::OnReadFile() 
{
	// TODO: Add your command handler code here
	fstream infile;
	infile.open("graph.txt",ios::in);
	infile>>m_type>>m_color>>m_start.x>>m_start.y
		>>m_end.x>>m_end.y>>m_radius;
	Invalidate();  // 数据成员被修改,使视图无效(通知更新视图)
 	return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -