📄 ex_gdiview.cpp
字号:
// Ex_GDIView.cpp : implementation of the CEx_GDIView class
//
#include "stdafx.h"
#include "Ex_GDI.h"
#include "Ex_GDIDoc.h"
#include "Ex_GDIView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CEx_GDIView
IMPLEMENT_DYNCREATE(CEx_GDIView, CView)
BEGIN_MESSAGE_MAP(CEx_GDIView, CView)
//{{AFX_MSG_MAP(CEx_GDIView)
// 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()
/////////////////////////////////////////////////////////////////////////////
// CEx_GDIView construction/destruction
CEx_GDIView::CEx_GDIView()
{
// TODO: add construction code here
}
CEx_GDIView::~CEx_GDIView()
{
}
BOOL CEx_GDIView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CEx_GDIView drawing
void CEx_GDIView::OnDraw(CDC* pDC)
{
CEx_GDIDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
int data[20] = {19,21,32,40,41,39,42,35,
33,23,21,20,24,11,9,19,22,32,40,42};
CRect rc;
GetClientRect(rc);// 获得客户区的大小
rc.DeflateRect(50,50); // 将矩形大小沿x和y方向各减小50
int gridXnums = 10, gridYnums = 8;
int dx = rc.Width()/gridXnums;
int dy = rc.Height()/gridYnums;
CRect gridRect(rc.left,rc.top,rc.left+dx*gridXnums,rc.top+dy*gridYnums); // 调整矩形大小
CPen gridPen(0,0,RGB(0,100,200));
CPen* oldPen = pDC->SelectObject(&gridPen);
for (int i=0; i<=gridXnums; i++) // 绘制垂直线
{ pDC->MoveTo(gridRect.left+i*dx,gridRect.bottom);
pDC->LineTo(gridRect.left+i*dx,gridRect.top);
}
for (int j=0; j<=gridYnums; j++) // 绘制水平线
{ pDC->MoveTo(gridRect.left,gridRect.top+j*dy);
pDC->LineTo(gridRect.right,gridRect.top+j*dy);
}
pDC->SelectObject(oldPen); // 恢复原来画笔
gridPen.Detach(); // 将画笔对象与其构造的内容分离,以便能再次构造画笔
gridPen.CreatePen(0,0,RGB(0,0,200)); // 重新创建画笔
pDC->SelectObject(&gridPen);
CBrush gridBrush(RGB(255,0,0)); // 创建画刷
CBrush* oldBrush = pDC->SelectObject(&gridBrush);
POINT ptRect[4] = {{-3,-3},{-3,3},{3,3},{3,-3}}, ptDraw[4];
int deta;
POINT pt[256];
int nCount = 20;
deta = gridRect.Width()/nCount;
for (i=0; i<nCount; i++)
{ pt[i].x = gridRect.left+i*deta;
pt[i].y = gridRect.bottom-(int)(data[i]/60.0*gridRect.Height());
for (j=0; j<4; j++)
{
ptDraw[j].x = ptRect[j].x+pt[i].x;
ptDraw[j].y = ptRect[j].y+pt[i].y;
}
pDC->Polygon(ptDraw,4); // 绘制小方块
}
pDC->Polyline(pt,nCount);
// 恢复原来绘图属性
pDC->SelectObject(oldPen);
pDC->SelectObject(oldBrush);
}
/////////////////////////////////////////////////////////////////////////////
// CEx_GDIView printing
BOOL CEx_GDIView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CEx_GDIView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CEx_GDIView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CEx_GDIView diagnostics
#ifdef _DEBUG
void CEx_GDIView::AssertValid() const
{
CView::AssertValid();
}
void CEx_GDIView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CEx_GDIDoc* CEx_GDIView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEx_GDIDoc)));
return (CEx_GDIDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CEx_GDIView message handlers
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -