📄 wbpen.cpp
字号:
// WBPen.cpp: implementation of the CWBPen class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "WBPen.h"
#include "DrawView.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CWBPen::CWBPen( CDrawView & view ) : CDrawBase( view )
{
this->draw.tag = PEN;
memset( &this->orgin , 0 , sizeof( POINT ) );
}
CWBPen::~CWBPen()
{
}
CDrawBase * CWBPen::Clone( void )
{
CWBPen * obj = new CWBPen( this->view );
memcpy( &obj->draw , &this->draw , sizeof( DRAW ) );
return obj;
}
HCURSOR CWBPen::GetCursor( void )
{
static HCURSOR hCursor = ::AfxGetApp()->LoadCursor( IDC_PEN );
return hCursor;
}
bool CWBPen::Select( CRect & rc )
{
rc.NormalizeRect();
CRgn r0 , r1;
r0.CreateRectRgn( rc.left , rc.top , rc.right , rc.bottom );
r1.CreateRectRgn( 0 , 0 , 0 , 0 );
POINT start , end;
for( LIST_POINT::iterator itr = this->list_point.begin(); itr != this->list_point.end(); itr ++ )
{
if( itr == this->list_point.begin() )
start = * itr;
else
{
end = * itr;
POINT pt[] =
{
{ start.x , start.y } ,
{ end.x , end.y } ,
{ end.x + this->draw.pen.lopnWidth.x , end.y + ( this->draw.start.y == this->draw.end.y ? 1 : 0 ) } ,
{ start.x + this->draw.pen.lopnWidth.x , start.y + ( this->draw.start.y == this->draw.end.y ? 1 : 0 ) }
};
start = end;
CRgn r;
r.CreatePolygonRgn( pt , sizeof( pt ) / sizeof( POINT ) , WINDING );
r1.CombineRgn( &r1 , &r , RGN_OR );
}
}
return this->draw_select = r0.CombineRgn( &r0 , &r1 , RGN_AND ) != NULLREGION;
}
void CWBPen::OnDraw( CDC * pDC )
{
CDrawBase::OnDraw( pDC );
POINT offset;
//计算偏移量
offset.x = this->draw.start.x - this->orgin.x;
offset.y = this->draw.start.y - this->orgin.y;
//保存原点
this->orgin = this->draw.start;
//把所有的点连线
for( LIST_POINT::iterator itr = this->list_point.begin(); itr != this->list_point.end(); itr ++ )
{ //偏移坐标
itr->x += offset.x; itr->y += offset.y;
//定位到起点
if( itr == this->list_point.begin() )
pDC->MoveTo( * itr );
//连线
else
pDC->LineTo( * itr );
}
}
char * CWBPen::Encode( int & size , bool erase )
{
char * buf = CDrawBase::Encode( size , erase );
if( buf && ! erase )
{ //点数大小
int s = this->list_point.size() * sizeof( POINT );
//申请内存
char * buffer = new char[ size + s ];
char * now = buffer;
memcpy( now , buf , size ); now += size;
size += s;
delete []buf;
//保存所有的点
for( LIST_POINT::iterator itr = this->list_point.begin(); itr != this->list_point.end(); itr ++ )
{
memcpy( now , &( * itr ), sizeof( POINT ) ); now += sizeof( POINT );
}
buf = buffer;
}
return buf;
}
bool CWBPen::Decode( char * buffer , int size )
{
if( CDrawBase::Decode( buffer , size ) )
{
size -= this->draw_id.GetLength() + 1 + sizeof( DRAW );
buffer += this->draw_id.GetLength() + 1 + sizeof( DRAW );
while( size > 0 )
{
POINT point;
memcpy( &point , buffer , sizeof( POINT ) );
size -= sizeof( POINT ); buffer += sizeof( POINT );
this->list_point.push_back( point );
//计算包含画笔笔画的最大矩形
if( this->draw.start.x > point.x ) this->draw.start.x = point.x;
if( this->draw.start.y > point.y ) this->draw.start.y = point.y;
if( this->draw.end.x < point.x ) this->draw.end.x = point.x;
if( this->draw.end.y < point.y ) this->draw.end.y = point.y;
}//保留原点
this->orgin = this->draw.start;
return true;
}
return false;
}
void CWBPen::OnLButtonDown( UINT nFlags, CPoint point )
{
CDrawBase::OnLButtonDown( nFlags , point );
if( nFlags & MK_LBUTTON )
this->list_point.push_back( this->draw.start );
}
void CWBPen::OnMouseMove( UINT nFlags, CPoint point )
{
if( nFlags & MK_LBUTTON )
{ //计算包含画笔笔画的最大矩形
if( this->draw.start.x > point.x ) this->draw.start.x = point.x;
if( this->draw.start.y > point.y ) this->draw.start.y = point.y;
if( this->draw.end.x < point.x ) this->draw.end.x = point.x;
if( this->draw.end.y < point.y ) this->draw.end.y = point.y;
//且规定原点在左上角
this->orgin = this->draw.start;
this->list_point.push_back( point );
this->view.Invalidate();
}
}
void CWBPen::OnLButtonUp( UINT nFlags, CPoint point )
{
this->orgin = this->draw.start;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -