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

📄 graphview.cpp

📁 一系列的c++例子 一步一步由浅入深 有 聊天室
💻 CPP
字号:
// GraphView.cpp : implementation of the CGraphView class
//

#include "stdafx.h"
#include "Graph.h"

#include "math.h"
#include "GraphDoc.h"
#include "GraphView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CGraphView

IMPLEMENT_DYNCREATE(CGraphView, CView)

BEGIN_MESSAGE_MAP(CGraphView, CView)
	//{{AFX_MSG_MAP(CGraphView)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CGraphView construction/destruction

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

}

CGraphView::~CGraphView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CGraphView drawing

void CGraphView::OnDraw(CDC* pDC)
{
	CGraphDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
    int i;
pDC->SetMapMode(MM_LOENGLISH );
pDC->SetViewportOrg(200,300);
	// TODO: add draw code for native data here
	//绘制彩色点
	pDC->TextOut(10,10,"Point");
pDC->SetPixel(180,20,RGB(0,0,0));
	pDC->SetPixel(100,20,RGB(255,255,255));
	pDC->SetPixel(110,20,RGB(255,255,0));
	pDC->SetPixel(120,20,RGB(255,0,0));
	pDC->SetPixelV(150,20,RGB(255,255,255));
	pDC->SetPixelV(160,20,RGB(255,255,0));
	pDC->SetPixelV(170,20,RGB(255,0,0));

//绘制直线
	pDC->TextOut(30,60,"Line");
	pDC->MoveTo(30,150);
	pDC->LineTo(30,40);
//绘制折线
POINT polyline[5]={{70,200},{20,170},{70,170},{30,200},{100,300}};
	pDC->Polyline(polyline,5);
	
	POINT polyPolyline[9]={{95,190},{120,185},{120,270},{145,190},{120,185},
						{90,185},{150,185},{100,230},{160,230}};
	DWORD dwPolyPoints[4]={2,3,2,2};
	pDC->PolyPolyline(polyPolyline,dwPolyPoints,4);

//绘制圆	
for(i=0;i<6;i++)
	{
		pDC->Arc(260-10*i,70-10*i,260+10*i,70+10*i,260+10*i,70,260+10*i,70);
	}
  
//绘制圆弧
	for(i=3;i<6;i++)
	{
		pDC->Arc(260-10*i,70-10*i,260+10*i,70+10*i,
			(int)260+10*i*cos(60*3.1415926/180),
			(int)70+10*i*sin(60*3.1415926/180),
			(int)260+10*i*cos(60*3.1415926/180),
			(int)70-10*i*sin(60*3.1415926/180));

		pDC->Arc(260-10*i,70-10*i,260+10*i,70+10*i,
			(int)260-10*i*cos(60*3.1415926/180),
			(int)70-10*i*sin(60*3.1415926/180),
			(int)260-10*i*cos(60*3.1415926/180),
			(int)70+10*i*sin(60*3.1415926/180));
	}

//绘制椭圆
	pDC->Ellipse(180-50,80-10,180+50,80+10);

//绘制贝济尔曲线
	POINT polyBezier[4]={{20,310},{60,240},{120,300},{160,330}};
	pDC->PolyBezier(polyBezier,4);
	//绘制矩形和圆角矩形
	pDC->Rectangle(190,270,250,310);
	pDC->RoundRect(265,270,330,310,30,20);	

	//绘制弦形和扇形
	pDC->Chord(360-70,70-70,360+70,70+70,
		(int)360+70*cos(60*3.1415926/180),
		(int)70+70*sin(60*3.1415926/180),
		(int)360+70*cos(30*3.1415926/180),
		(int)70-70*sin(30*3.1415926/180));
	pDC->Pie(480-70,70-80,480+80,70+80,
		(int)480+70*cos(60*3.1415926/180),
		(int)70+70*sin(60*3.1415926/180),
		(int)480+70*cos(30*3.1415926/180),
		(int)70-70*sin(30*3.1415926/180));

	//绘制多边形
	POINT polygon[3]={{390,160},{430,220},{350,210}};
	POINT polyPolygon[6]={{450,120},{550,320},{480,280},
						{400,300},{550,320},{480,280}};
	int	polygonPoints[2]={3,3};
	pDC->Polygon(polygon,3);
	pDC->PolyPolygon(polyPolygon,polygonPoints,2);
}


/////////////////////////////////////////////////////////////////////////////
// CGraphView printing

BOOL CGraphView::OnPreparePrinting(CPrintInfo* pInfo)
{

pInfo->SetMaxPage(3); // the document is two pages long:
// the first page is the title page
// the second is the drawing
BOOL bRet = DoPreparePrinting(pInfo); // default preparation
pInfo->m_nNumPreviewPages = 2; // Preview 2 pages at a time
// Set this value after calling DoPreparePrinting to override
// value read from .INI file
return bRet;

}

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

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

/////////////////////////////////////////////////////////////////////////////
// CGraphView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CGraphView message handlers

void CGraphView::OnPrint(CDC *pDC, CPrintInfo *pInfo)
{
// TODO: Add your specialized code here and/or call the base class
if (pInfo->m_nCurPage == 1) // page no. 1 is the title page
{
PrintTitlePage(pDC, pInfo);
return; // nothing else to print on page 1 but the page title
}
CString strHeader = GetDocument()->GetTitle();
PrintPageHeader(pDC, pInfo, strHeader);
// PrintPageHeader() subtracts out from the pInfo->m_rectDraw the
// amount of the page used for the header.
pDC->SetWindowOrg(pInfo->m_rectDraw.left,-pInfo->m_rectDraw.top);
// Now print the rest of the page
OnDraw(pDC); 

}

void CGraphView::PrintTitlePage(CDC *pDC, CPrintInfo *pInfo)
{
// Prepare a font size for displaying the file name
LOGFONT logFont;
memset(&logFont, 0, sizeof(LOGFONT));
logFont.lfHeight = 75; // 3/4th inch high in MM_LOENGLISH
// (1/100th inch)
CFont font;
CFont* pOldFont = NULL;
if (font.CreateFontIndirect(&logFont))
pOldFont = pDC->SelectObject(&font);
// Get the file name, to be displayed on title page
CString strPageTitle = GetDocument()->GetTitle();
// Display the file name 1 inch below top of the page,
// centered horizontally
pDC->SetTextAlign(TA_CENTER);
pDC->TextOut(pInfo->m_rectDraw.right/2, -100, strPageTitle);
if (pOldFont != NULL)
pDC->SelectObject(pOldFont);

}

void CGraphView::PrintPageHeader(CDC *pDC, CPrintInfo *pInfo, CString &strHeader)
{
// Print a page header consisting of the name of
// the document and a horizontal line
pDC->SetTextAlign(TA_LEFT);
pDC->TextOut(0,-25, strHeader); // 1/4 inch down
// Draw a line across the page, below the header
TEXTMETRIC textMetric;
pDC->GetTextMetrics(&textMetric);
int y = -35 - textMetric.tmHeight; // line 1/10th inch below text
pDC->MoveTo(0, y); // from left margin
pDC->LineTo(pInfo->m_rectDraw.right, y); // to right margin
// Subtract out from the drawing rectange the space used by the header.
y -= 25; // space 1/4 inch below (top of) line
pInfo->m_rectDraw.top += y;

}

⌨️ 快捷键说明

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