www.pudn.com > VCYXSJRM.rar > canvasFrame.cpp
// canvasFrame.cpp : implementation file
//
#include "stdafx.h"
#include "canvasr.h"
#include "canvasFrame.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// canvasFrame
IMPLEMENT_DYNCREATE(canvasFrame, CFrameWnd)
CPen newPen,*oldPen;
canvasFrame::canvasFrame()
{
RECT rect;
Create(NULL,"绘图窗口");
CClientDC dc(this);
int width = dc.GetDeviceCaps(HORZRES);
int height = dc.GetDeviceCaps(VERTRES);
GetWindowRect( &rect );
width = ( width - ( rect.right - rect.left ))/2 ;
height = (height - (rect.bottom - rect.top ))/2 ;
MoveWindow( width , height , (rect.right - rect.left ) , (rect.bottom - rect.top ) ,true);
newPen.CreatePen(PS_DASH,5,RGB(0,0,255));
}
canvasFrame::~canvasFrame()
{
}
BEGIN_MESSAGE_MAP(canvasFrame, CFrameWnd)
//{{AFX_MSG_MAP(canvasFrame)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// canvasFrame message handlers
void canvasFrame::OnPaint()
{
CPaintDC dc(this); //建立响应WM_PAINT消息的DC
oldPen=dc.SelectObject(&newPen); //选择新画笔
int i,j=11,ptype,x=100,y=10;
dc.MoveTo(x,y); //设定线段起点为(x,y)
for(i=0;i<=19;i++)
{
ptype = i % 4; //判断点的类型
switch(ptype)
{
case 0: //右上角的点
j=j-2;
x = x + 40 * j;
y = y;
break;
case 1: //右下角的点
x = x;
y = y + 40 * j;
break;
case 2: //左下角的点
x = x - (j - 1) * 40;
y = y;
break;
case 3: //左上角的点
x = x;
y = y - (j -1 ) * 40;
break;
}
dc.LineTo(x,y); //绘制直线至(x,y),(x,y)为下一线段的起点
}
dc.SelectObject(oldPen); //重设回旧画笔
}