// DrawGraphView.cpp : implementation of the CDrawGraphView class
//
#include "stdafx.h"
#include "DrawGraph.h"
#include "DrawGraphDoc.h"
#include "DrawGraphView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDrawGraphView
IMPLEMENT_DYNCREATE(CDrawGraphView, CView)
BEGIN_MESSAGE_MAP(CDrawGraphView, CView)
//{{AFX_MSG_MAP(CDrawGraphView)
// 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
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDrawGraphView construction/destruction
CDrawGraphView::CDrawGraphView()
{
// TODO: add construction code here
}
CDrawGraphView::~CDrawGraphView()
{
}
BOOL CDrawGraphView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CDrawGraphView drawing
void CDrawGraphView::OnDraw(CDC* pDC)
{
CDrawGraphDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CRect crect;
CPen *pOldPen;
CPen *pMyPen = new CPen();
int MaxX;
int MaxY;
GetClientRect(&crect); //得到窗口区域
MaxX=crect.Width ();
MaxY=crect.Height ();
//创建新画笔
pMyPen->CreatePen (PS_SOLID,1,RGB(0,0,0));
pOldPen = pDC->SelectObject (pMyPen);
pDC->MoveTo (0,MaxY/2); //设置绘线点为水平
pDC->LineTo (MaxX,MaxY/2);//画一条水平线
pDC->MoveTo (MaxX/2,0);
pDC->LineTo (MaxX/2,MaxY);//画一条中垂线
CBrush *pOldBrush; //保存系统原有画刷
CBrush brushGreen(RGB(0,255,0)); //创建一个绿色画刷
pOldBrush=pDC->SelectObject (&brushGreen);//选择绿色画刷
crect.SetRect (0,0,MaxX/2,MaxY/2); //设置绘图区
crect.DeflateRect (30,30); //将绘图区缩小30个像素
pDC->Rectangle (crect); //画矩形
pDC->ExtTextOut (MaxX/4,MaxY/4,ETO_CLIPPED,NULL,"Green Rectangle",NULL);//输出说明性文字
CBrush brushRed(RGB(255,0,0)); //创建一个红色画刷
pOldBrush=pDC->SelectObject (&brushRed);//选择红色画刷
crect.SetRect (MaxX/2,0,MaxX,MaxY/2); //设置绘图区
crect.DeflateRect (30,30); //将绘图区缩小30个像素
pDC->Ellipse (crect); //画椭圆
pDC->ExtTextOut (MaxX*3/4,MaxY/4,ETO_CLIPPED,NULL,"Red ellipse",NULL);//输出说明性文字
CBrush brushYello(RGB(255,255,0)); //创建一个黄色画刷
pOldBrush=pDC->SelectObject (&brushYello);//选择黄色画刷
crect.SetRect (0,MaxY/2,MaxX/2,MaxY); //设置绘图区
crect.DeflateRect (30,30); //将绘图区缩小30个像素
pDC->RoundRect (crect,CPoint(50,50));//绘制圆角矩形
pDC->ExtTextOut (MaxX/4,MaxY*3/4,ETO_CLIPPED,NULL,"Yello RoundRect",NULL);//输出说明性文字
CBrush brushOrchid(RGB(0,0,255)); //创建一个兰色画刷
CPoint pts[4];
pOldBrush=pDC->SelectObject (&brushOrchid);//选择兰色画刷
crect.SetRect (MaxX/2,MaxY/2,MaxX,MaxY); //设置绘图区
crect.DeflateRect (30,30); //将绘图区缩小30个像素
pts[0].x = crect.left +crect.Width ()/2;//设置多边形的四个点
pts[0].y = crect.top ;
pts[1].x = crect.right ;
pts[1].y = crect.top + crect.Height ()/2;
pts[2].x = pts[0].x ;
pts[2].y = crect.bottom ;
pts[3].x = crect.left ;
pts[3].y = pts[1].y ;
pDC->Polygon (pts,4); //绘制多边形
pDC->ExtTextOut (MaxX*3/4,MaxY*3/4,ETO_CLIPPED,NULL,"Orchid Polygon",NULL);//输出说明性文字
//恢复系统原有的画刷
pDC->SelectObject (pOldBrush);
}
/////////////////////////////////////////////////////////////////////////////
// CDrawGraphView diagnostics
#ifdef _DEBUG
void CDrawGraphView::AssertValid() const
{
CView::AssertValid();
}
void CDrawGraphView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CDrawGraphDoc* CDrawGraphView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawGraphDoc)));
return (CDrawGraphDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDrawGraphView message handlers