📄 drawview.cpp
字号:
// DrawView.cpp : implementation file
//
#include "stdafx.h"
#include "DrawView.h"
#include "WB.h"
#include "DrawFrame.h"
#include "MemDC.h"
#include "Select.h"
#include "Line.h"
#include "WBPen.h"
#include "Rectangle.h"
#include "RoundRect.h"
#include "Circle.h"
#include "Text.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define FIRST_SCROLL_BUTTON 1000
#define LAST_SCROLL_BUTTON 1016
#define SELECT_DRAW( cls , empty )\
{\
DRAW d;\
memset( &d , 0 , sizeof( DRAW ) );\
d.pen.lopnWidth = CPoint( 1 , 1 );\
if( draw )\
{\
d.page = draw->GetProp().page;\
d.pen = draw->GetProp().pen;\
d.brush = draw->GetProp().brush;\
delete draw ;\
}\
draw = new cls(*this);\
d.tag = draw->GetProp().tag;\
memcpy( &draw->GetProp() , &d , sizeof( DRAW ) );\
draw->GetProp().brush.lbStyle = empty ? BS_NULL : BS_SOLID;\
draw->GetProp().pen.lopnWidth = d.pen.lopnWidth;\
draw->GetProp().page = this->page;\
this->Invalidate();\
TRACE("select %s object\n" , #cls );\
}
#define DRAW_EVENT( event , flag , pt )\
{\
if( draw )\
{\
pt.Offset( GetScrollPosition( ) );\
draw->##event(flag,pt);\
TRACE( "event:%s(%d,%d)\n" , #event , pt.x , pt.y );\
}\
}
#define SHOW_POS( pt )\
{\
CString s;\
CPoint p = pt;\
p.Offset( GetScrollPosition( ) );\
s.Format( "x : %d , y : %d" , p.x , p.y );\
GetFrame().GetBar().GetStatic().SetWindowText( s );\
GetFrame().GetBar().GetToolBarCtrl().EnableButton( ID_SAVE , ! GetDrawMap().empty() );\
}
/////////////////////////////////////////////////////////////////////////////
// CDrawView
CDrawView::CDrawView( CDrawFrame * pFrame )
{
this->send_threadid = 0;
this->capture = false;
this->page = 1;
this->username = "";
this->OnWB = NULL;
this->pContext = NULL;
this->Create( NULL , NULL , AFX_WS_DEFAULT_VIEW | WS_CLIPCHILDREN , CRect( 0 , 0 , 0 , 0 ) , pFrame , AFX_IDW_PANE_FIRST , NULL );
this->draw = NULL;
this->m_bDraw = FALSE;
this->OnTool( ID_DRAW_LINE );
}
CDrawView::~CDrawView()
{
if( this->draw ) delete this->draw;
DRAW_MAP::iterator itr;
while( ! this->GetDrawMap().empty( ) )
{
itr = this->GetDrawMap().begin();
delete itr->second;
this->GetDrawMap().erase( itr );
}
}
BEGIN_MESSAGE_MAP(CDrawView, CScrollView)
//{{AFX_MSG_MAP(CDrawView)
ON_WM_CREATE()
ON_WM_CONTEXTMENU()
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_COMMAND(ID_COLOR, OnColor)
ON_WM_ERASEBKGND()
ON_WM_DESTROY()
ON_COMMAND(ID_OPEN, OnOpen)
ON_COMMAND(ID_SAVE, OnSave)
ON_WM_SETCURSOR()
//}}AFX_MSG_MAP
ON_COMMAND_RANGE( ID_DRAW_LINE , ID_DRAW_DELETE , OnTool )
ON_COMMAND_RANGE( ID_LINE_1_PIXEL , ID_LINE_4_PIXEL , OnLine )
ON_MESSAGE( WM_PARSE_DATA , OnParse )
ON_COMMAND_RANGE( FIRST_SCROLL_BUTTON , LAST_SCROLL_BUTTON , OnScrollButton)
ON_NOTIFY_REFLECT_EX( NM_COOLSB_CUSTOMDRAW , OnScrollNotify)
ON_CBN_SELCHANGE( ID_LINETYPE , OnLine )
ON_CBN_SELCHANGE( ID_COLOR , OnColor )
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDrawView drawing
void CDrawView::OnDraw(CDC* pDC)
{ //双缓冲绘图
CMemDC dc( pDC );
//显示列表中存在的图元
for( DRAW_MAP::iterator itr = this->GetDrawMap().begin( ); itr != this->GetDrawMap().end( ); itr ++ )
if( itr->second->GetProp().page == this->page )
itr->second->OnDraw( &dc );
//显示正在画的图元
if( this->draw->GetProp().page == this->page )
this->draw->OnDraw( &dc );
}
//更新所有背景
BOOL CDrawView::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
//创建窗口
int CDrawView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CScrollView::OnCreate(lpCreateStruct) == -1)
return -1;
//滚动条设置
InitializeCoolSB( this->GetSafeHwnd() );
CoolSB_SetStyle(m_hWnd, SB_BOTH, CSBS_NORMAL );
SCROLLBUT btn;
btn.fMask = SBBF_ID | SBBF_TYPE | SBBF_SIZE | SBBF_PLACEMENT;
btn.uCmdId = FIRST_SCROLL_BUTTON;
btn.nSize = -1;
btn.uPlacement = SBBP_LEFT;
btn.uButType = SBBT_PUSHBUTTON | SBBM_LEFTARROW;
CoolSB_InsertButton( this->GetSafeHwnd() , SB_HORZ, (UINT)-1, &btn);
btn.fMask = SBBF_ID | SBBF_TYPE | SBBF_SIZE | SBBF_PLACEMENT;
btn.uCmdId = FIRST_SCROLL_BUTTON + 1;
btn.nSize = -1;
btn.uPlacement = SBBP_LEFT;
btn.uButType = SBBT_PUSHBUTTON | SBBM_RIGHTARROW;
CoolSB_InsertButton( this->GetSafeHwnd() , SB_HORZ, (UINT)-1, &btn);
btn.fMask = SBBF_ID | SBBF_TYPE | SBBF_SIZE | SBBF_PLACEMENT;
btn.uCmdId = FIRST_SCROLL_BUTTON + 2;
btn.nSize = 120;
btn.uPlacement = SBBP_LEFT;
btn.uButType = SBBT_OWNERDRAW | SBBM_RESIZABLE;
btn.nMinSize = 0;
btn.nMaxSize = (UINT)-1;
CoolSB_InsertButton( this->GetSafeHwnd() , SB_HORZ, (UINT)-1, &btn);
btn.fMask = SBBF_ID | SBBF_TYPE | SBBF_SIZE | SBBF_PLACEMENT | SBBF_CURSOR;
btn.uCmdId = FIRST_SCROLL_BUTTON + 3;
btn.nSize = 7;
btn.uPlacement = SBBP_LEFT;
btn.uButType = SBBT_FIXED;
btn.hCurs = ::AfxGetApp()->LoadStandardCursor( IDC_SIZEWE );
CoolSB_InsertButton(this->GetSafeHwnd(), SB_HORZ, (UINT)-1, &btn);
//设定工作区大小
this->SetScrollSizes( MM_TEXT , CSize( 2034 , 2034 ) );
//初始化菜单
this->menu.LoadMenu( IDR_TOOL_MENU );
//创建发送线程
::CreateThread( NULL , 0 , CDrawView::OnSend , this , 0 , &this->send_threadid );
return 0;
}
//取得主框架框窗口
CDrawFrame & CDrawView::GetFrame( void )
{
return *( CDrawFrame * )this->GetParent();
}
//菜单弹出函数
void CDrawView::OnContextMenu(CWnd* pWnd, CPoint point)
{
if( this->menu.GetSafeHmenu() )
this->menu.GetSubMenu( 0 )->TrackPopupMenu( TPM_RIGHTBUTTON | TPM_LEFTALIGN , point.x , point.y , this );
}
//鼠标按下
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
CScrollView::OnLButtonDown(nFlags, point);
//如果draw不是CText类的实例
if( ! IsDescendant( this->draw , CText ) )
{ //那么捕获鼠标事件
this->SetCapture();
//是否捕获标志
this->capture = this->GetCapture() == this;
}
this->m_bDraw = true;
DRAW_EVENT( OnLButtonDown , nFlags , point );
}
void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
{
CScrollView::OnMouseMove(nFlags, point);
SHOW_POS( point );
if( this->m_bDraw || IsDescendant( this->draw , CSelect ) )
DRAW_EVENT( OnMouseMove , nFlags , point );
}
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
CScrollView::OnLButtonUp(nFlags, point);
//鼠标等待
CWaitCursor cursor;
if( this->capture )
::ReleaseCapture();
if( this->m_bDraw )
{
DRAW_EVENT( OnLButtonUp , nFlags , point );
this->m_bDraw = FALSE;
}
else
return;
//组合数据包,往外发送
if( this->Assemble( this->draw ) )
{
this->GetDrawMap()[ this->draw->GetId() ] = this->draw;
//克隆一个图元信息
this->draw = this->draw->Clone();
}
this->Invalidate();
}
void CDrawView::OnOpen()
{
CFileDialog dlg( TRUE , "wb" , NULL , OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT , "WB - WB file|*.wb||" );
if( dlg.DoModal() == IDOK )
{
CFile file;
if( file.Open( dlg.GetPathName() , CFile::modeRead ) )
{
int size;
char * buffer;
CString id;
CDrawBase * d;
DRAW draw;
while( file.GetPosition() < file.GetLength() )
{ //读取大小
file.Read( &size , sizeof( int ) );
//申请内存
buffer = new char[ size ];
//读取内存
file.Read( buffer , size );
//解析数据
id = buffer;
memcpy( &draw , buffer + id.GetLength() + 1 , sizeof( DRAW ) );
switch( draw.tag )
{
case CDrawBase::LINE : d = new CLine( * this ); break;
case CDrawBase::PEN : d = new CWBPen( * this ); break;
case CDrawBase::RECT : d = new CRectangle( * this ); break;
case CDrawBase::ROUNDRECT : d = new CRoundRect( * this ); break;
case CDrawBase::CIRCLE : d = new CCircle( * this ); break;
case CDrawBase::TEXT : d = new CText( * this ); break;
}
if( d )
{
d->Decode( buffer , size );
//清除id
d->SetId( NULL );
this->GetDrawMap()[ d->GetId() ] = d;
//组合数据往外发送
this->Assemble( d );
}
delete []buffer;
}
file.Close();
//刷新
this->Invalidate();
}
}
}
void CDrawView::OnSave()
{
CFileDialog dlg( FALSE , "wb" , NULL , OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT , "WB - WB file|*.wb|BMP - Windows bitmap|*.bmp||" );
if( dlg.DoModal() == IDOK )
{
CFile file;
if( file.Open( dlg.GetPathName() , CFile::modeCreate | CFile::modeWrite ) )
{
if( ! dlg.GetFileExt( ).CompareNoCase( "wb" ) )
{//大小
int size;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -