📄 drawtestview.cpp
字号:
// DrawTestView.cpp : implementation of the CDrawTestView class
//
#include "stdafx.h"
#include "DrawTest.h"
#include "DrawTestDoc.h"
#include "DrawTestView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDrawTestView
IMPLEMENT_DYNCREATE(CDrawTestView, CView)
BEGIN_MESSAGE_MAP(CDrawTestView, CView)
//{{AFX_MSG_MAP(CDrawTestView)
ON_COMMAND(ID_TEST_FONT, OnTestFont)
ON_COMMAND(ID_TEST_LINE, OnTestLine)
ON_COMMAND(ID_TEST_SHAPES, OnTestShapes)
ON_COMMAND(ID_CHANGE_CLR, OnChangeClr)
ON_COMMAND(ID_CHANGE_FONT, OnChangeFont)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CDrawTestView construction/destruction
CDrawTestView::CDrawTestView()
{
// TODO: add construction code here
//默认为画线:
m_CurTest=0;
}
CDrawTestView::~CDrawTestView()
{
}
BOOL CDrawTestView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CDrawTestView drawing
void CDrawTestView::OnDraw(CDC* pDC)
{
CDrawTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CPen pen,*open;
pen.CreatePen(PS_SOLID,3,pDoc->color);
open=pDC->SelectObject(&pen);
//画水平线:
pDC->MoveTo(5,5);
pDC->LineTo(105,5);
pDC->SetTextColor(pDoc->color);
pDC->TextOut(110,5,"X坐标");
//画垂直线:
pDC->MoveTo(5,5);
pDC->LineTo(5,105);
pDC->TextOut(5,110,"Y坐标");
pDC->SelectObject(open);
pen.DeleteObject();
switch(m_CurTest)
{
case 0:
{
DrawLine(pDC);
break;
}
case 1:
{
DrawShape(pDC);
break;
}
case 2:
{
DrawFont(pDC);
break;
}
}
}
/////////////////////////////////////////////////////////////////////////////
// CDrawTestView printing
BOOL CDrawTestView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDrawTestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CDrawTestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CDrawTestView diagnostics
#ifdef _DEBUG
void CDrawTestView::AssertValid() const
{
CView::AssertValid();
}
void CDrawTestView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CDrawTestDoc* CDrawTestView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawTestDoc)));
return (CDrawTestDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDrawTestView message handlers
void CDrawTestView::OnTestLine()
{
// TODO: Add your command handler code here
m_CurTest=0;
Invalidate();
}
void CDrawTestView::OnTestShapes()
{
// TODO: Add your command handler code here
m_CurTest=1;
Invalidate();
}
void CDrawTestView::OnTestFont()
{
// TODO: Add your command handler code here
m_CurTest=2;
Invalidate();
}
void CDrawTestView::DrawLine(CDC *pDC)
{
UINT i,pos,gap,length;
CRect rc;
GetClientRect(&rc);
pos=20;
gap=20;
length=rc.Width()-40;
CPen pen,*open;
CDrawTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
for(i=0;i<5;i++)
{
pen.CreatePen(i,1,pDoc->color);
open=pDC->SelectObject(&pen);
pDC->MoveTo(pos,pos+i*gap);
pDC->LineTo(pos+length,pos+i*gap);
pDC->SelectObject(open);
pen.DeleteObject();
}
}
void CDrawTestView::DrawShape(CDC *pDC)
{
UINT i;
UINT pos,gap,width;
CRect rc;
GetClientRect(&rc);
pos=20;
gap=35;
width=rc.Width()-40;
CDrawTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CBrush br,*obr;
for(i=0;i<6;i++)
{
br.CreateHatchBrush(i,pDoc->color);
obr=pDC->SelectObject(&br);
pDC->RoundRect(pos,pos+i*gap,pos+width,pos+i*gap+30,pos+20,pos+20);
pDC->SelectObject(obr);
br.DeleteObject();
}
br.CreateSolidBrush(pDoc->color);
obr=pDC->SelectObject(&br);
pDC->RoundRect(pos,pos+i*gap,pos+width,pos+i*gap+30,pos+20,pos+20);
pDC->SelectObject(obr);
br.DeleteObject();
}
void CDrawTestView::DrawFont(CDC *pDC)
{
CDrawTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// Create and display eight example fonts.
CRect rc;
GetClientRect(&rc);
CFont font;
font.CreateFontIndirect(&pDoc->logFont);
CFont* oldFont = pDC->SelectObject(&font);
pDC->SetTextAlign(TA_CENTER);
pDC->SetTextColor(pDoc->color);
pDC->TextOut(rc.Width()/2,rc.Height()/2, "Hellow 即 你好.....");
pDC->SelectObject(oldFont);
font.DeleteObject();
}
void CDrawTestView::OnChangeClr()
{
// TODO: Add your command handler code here
CDrawTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CColorDialog cd;
if(cd.DoModal()==IDOK)
{
pDoc->color=cd.GetColor();
Invalidate();
}
}
void CDrawTestView::OnChangeFont()
{
// TODO: Add your command handler code here
CDrawTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CFontDialog fd;
if(fd.DoModal()==IDOK)
{
m_CurTest=2;
fd.GetCurrentFont(&pDoc->logFont);
Invalidate();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -