📄 drawdemoview.cpp
字号:
// DrawDemoView.cpp : implementation of the CDrawDemoView class
//
#include "stdafx.h"
#include "DrawDemo.h"
#include "DrawDemoDoc.h"
#include "DrawDemoView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDrawDemoView
IMPLEMENT_DYNCREATE(CDrawDemoView, CView)
BEGIN_MESSAGE_MAP(CDrawDemoView, CView)
//{{AFX_MSG_MAP(CDrawDemoView)
// 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()
/////////////////////////////////////////////////////////////////////////////
// CDrawDemoView construction/destruction
CDrawDemoView::CDrawDemoView()
{
// TODO: add construction code here
}
CDrawDemoView::~CDrawDemoView()
{
}
BOOL CDrawDemoView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CDrawDemoView drawing
void CDrawDemoView::OnDraw(CDC* pDC)
{
CDrawDemoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//DisRGB();
CLineDemo();
CRect rect;
GetClientRect(&rect);
DrawTree(rect.Width ()/2,rect.bottom , 80, 1.67 ,10);
}
/////////////////////////////////////////////////////////////////////////////
// CDrawDemoView diagnostics
#ifdef _DEBUG
void CDrawDemoView::AssertValid() const
{
CView::AssertValid();
}
void CDrawDemoView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CDrawDemoDoc* CDrawDemoView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawDemoDoc)));
return (CDrawDemoDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDrawDemoView message handlers
void CDrawDemoView::DisRGB()
{
CDC *pDC = new CClientDC(this);
CBrush RedBrush;
RedBrush.CreateSolidBrush (RGB(255,0,0));
CBrush *pOldBrush = pDC ->SelectObject (&RedBrush);
CRect rect;
GetClientRect(&rect);
pDC->Rectangle (rect.left ,rect.top ,rect.right ,rect.Height ()/3 );
Sleep(300);
CBrush GreenBrush;
GreenBrush.CreateSolidBrush (RGB(0,255,0));
pOldBrush = pDC ->SelectObject (&GreenBrush);
pDC->Rectangle (rect.left ,rect.Height()/3+1,rect.right ,rect.Height ()*2/3 );
Sleep(300);
CBrush BlueBrush;
BlueBrush.CreateSolidBrush (RGB(0,0,255));
pOldBrush = pDC ->SelectObject (&BlueBrush);
pDC->Rectangle (rect.left ,rect.Height()*2/3+1,rect.right ,rect.bottom );
pDC->SelectObject (pOldBrush);
delete pDC;
}
/****************************************************************************
* 名称:random()
* 功能:取随机数。
* 入口参数:seed 随机数范围(0 -- seed-1)
* 出口参数:返回值即是取得的随机数
****************************************************************************/
DWORD random(DWORD seed)
{ DWORD temp;
temp = rand();
temp = temp % seed;
return(temp);
}
/****************************************************************************
* 名称:CLineDemo()
* 功能:彩色线演示程序。以中心点为准,不断的画各种随机角度、随机长度、随机颜色画直线。
* 入口参数:无
* 出口参数:无
****************************************************************************/
void CDrawDemoView::CLineDemo()
{
POINT p0,p1;
DWORD no;
CPaintDC dc(this);
CRect rect;
CPen *pOldPen;
CPen *pNewPen = new CPen();
CBrush *pOldBrush;
CBrush *pNewBrush = new CBrush();
GetClientRect(&rect);
const CENTER_X=rect.Width ()/2;
const CENTER_Y=rect.Height ()/2;
for(no=0; no<400; no++)
{ /* 取第一个点坐标 */
p0.x = random(rect.Width ());
p0.y = random(rect.Height ());
//color = random(15)+1; // 不使用黑色(背景色)
/* 计算出中心对称点 */
p1.x = 2*CENTER_X - p0.x;
p1.y = 2*CENTER_Y - p0.y;
pNewPen->CreatePen (PS_SOLID,1,RGB((BYTE)random(255),(BYTE)random(255),(BYTE)random(255)));
pOldPen=dc.SelectObject (pNewPen);
dc.MoveTo (p0);
dc.LineTo (p1);
Sleep(50);
}
dc.FillSolidRect(rect,RGB(255,255,255));
//彩色矩形演示程序。取得随机起点及长、宽,然后画填充矩形
for(no=0; no<100; no++)
{ /* 取第一个点坐标 */
p0.x = random(rect.Width ());
p0.y = random(rect.Height ());
//color = random(15)+1; // 不使用黑色(背景色)
/* 取第二点坐标 */
p1.x = random(rect.Width ());
p1.y = random(rect.Height ());
pNewBrush->CreateSolidBrush(RGB((BYTE)random(255),(BYTE)random(255),(BYTE)random(255)));
pOldBrush=dc.SelectObject (pNewBrush);
dc.Rectangle(p0.x,p0.y,p1.x,p1.y);
Sleep(50);
}
dc.FillSolidRect(rect,RGB(255,255,255));
//彩色画填充圆演示程序。
for(no=0; no<200; no++)
{ /* 取第一个点坐标 */
p0.x = random(rect.Width ());
p0.y = random(rect.Height ());
//color = random(15)+1; // 不使用黑色(背景色)
/* 取第二点坐标 */
p1.x = random(rect.Width ());
p1.y = random(rect.Height ());
dc.SelectStockObject (NULL_BRUSH);
//pNewBrush->CreateSolidBrush(RGB((BYTE)random(255),(BYTE)random(255),(BYTE)random(255)));
pNewPen->CreatePen (PS_SOLID,1,RGB((BYTE)random(255),(BYTE)random(255),(BYTE)random(255)));
//pOldBrush=dc.SelectObject (pNewBrush);
pOldPen=dc.SelectObject (pNewPen);
//dc.Rectangle(p0.x,p0.y,p1.x,p1.y);
dc.Ellipse (p0.x,p0.y,p1.x,p1.y);
Sleep(50);
}
dc.FillSolidRect(rect,RGB(255,255,255));
}
/****************************************************************************
* 名称:DrawTree()
* 功能:使用图形分形学来实现一棵树的绘画。
* 入口参数:xStart,yStart 树枝/树干的起始点位置
* length 树枝/树干的长度
* angle 树枝/树干的倾斜角度(弧度)
* num 递归层次数
* 出口参数:无
****************************************************************************/
void CDrawDemoView::DrawTree(int xStart, int yStart, double length, double angle, int num)
{
CPaintDC dc(this);
CPen *pOldPen;
CPen *pNewPen = new CPen();
int xEnd, yEnd;
if(num == 0) return; // 本子树已画完
xEnd = xStart + (int)(length * cos(angle));
yEnd = yStart - (int)(length * sin(angle)); // 由于y方向相反,所以要相减
// 画本子树的树干
pNewPen->CreatePen (PS_SOLID,1,RGB((BYTE)random(255),(BYTE)random(255),(BYTE)random(255)));
pOldPen=dc.SelectObject (pNewPen);
dc.MoveTo (xStart, yStart);
dc.LineTo (xEnd, yEnd);
// 画本子树的左叉树枝
DrawTree( xEnd, yEnd,
length * 0.6,
angle + 0.624,
num - 1 );
// 画本子树的中叉树枝
DrawTree( xEnd, yEnd,
length * 0.85,
angle + 0.08,
num - 1 );
// 画本子树的右叉树枝
DrawTree( xEnd, yEnd,
length * 0.65,
angle - 0.6,
num - 1 );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -