Draw.cpp
资源名称:视频会议系统.rar [点击查看]
上传用户:popouu88
上传日期:2013-02-11
资源大小:2894k
文件大小:5k
源码类别:
IP电话/视频会议
开发平台:
Visual C++
- // Draw.cpp: implementation of the CDraw class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "Draw.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CDraw::CDraw( CDrawView * view )
- {
- this->userName = "";
- this->drawId = ( int )this;
- this->type = 0;
- this->view = view;
- this->dcSize = this->view->GetTotalSize( );
- this->start = CPoint( 0 , 0 );
- this->end = CPoint( 0 , 0 );
- }
- CDraw::~CDraw()
- {
- this->type = 0;
- }
- void CDraw::SetColor( COLORREF color )
- {
- int width = this->GetWidth( );
- if( this->pen.GetSafeHandle( ) )
- this->pen.DeleteObject( );
- this->pen.CreatePen( PS_SOLID , width , color );
- this->SetEmpty( this->IsEmpty( ) );
- }
- COLORREF CDraw::GetColor( void )
- {
- LOGPEN logPen;
- logPen.lopnColor = RGB( 0 , 0 , 0 );
- if( this->pen.GetSafeHandle( ) )
- this->pen.GetLogPen( &logPen );
- return logPen.lopnColor;
- }
- //设定画笔宽度
- void CDraw::SetWidth( int width )
- {
- COLORREF color = this->GetColor( );
- if( this->pen.GetSafeHandle( ) )
- this->pen.DeleteObject( );
- this->pen.CreatePen( PS_SOLID , width , color );
- }
- //取得画笔宽度
- int CDraw::GetWidth( void )
- {
- LOGPEN logPen;
- logPen.lopnWidth.x = 1;
- if( this->pen.GetSafeHandle( ) )
- this->pen.GetLogPen( &logPen );
- return logPen.lopnWidth.x;
- }
- //设定画刷是否为空,缺省情况为空
- void CDraw::SetEmpty( bool m_bEmpty )
- {
- LOGBRUSH lb;
- lb.lbColor = this->GetColor( );
- lb.lbHatch = 0;
- lb.lbStyle = m_bEmpty ? BS_HOLLOW : BS_SOLID;
- if( this->brush.GetSafeHandle( ) )
- this->brush.DeleteObject( );
- this->brush.CreateBrushIndirect( &lb );
- }
- //判断画刷是否为空
- bool CDraw::IsEmpty( void )
- {
- LOGBRUSH lb;
- lb.lbStyle = BS_HOLLOW;
- if( this->brush.GetSafeHandle( ) )
- this->brush.GetLogBrush( &lb );
- return lb.lbStyle == BS_HOLLOW;
- }
- CDC * CDraw::Draw( CDC * pDC )
- {
- if( pDC == NULL )
- pDC = this->view->GetMemDC( );
- if( ! this->rgn.GetSafeHandle( ) )
- {
- if( this->pen.GetSafeHandle( ) )
- pDC->SelectObject( &this->pen );
- if( this->brush.GetSafeHandle( ) )
- pDC->SelectObject( &this->brush );
- }
- else
- {
- CBrush b( this->GetColor( ) );
- pDC->FillRgn( &this->rgn , &b );
- pDC = NULL;
- }
- return pDC;
- }
- void CDraw::LButtonDown( UINT nFlags , CPoint point )
- {
- if( nFlags & MK_LBUTTON )
- this->start = this->end = point;
- }
- void CDraw::LButtonUp( UINT nFlags , CPoint point )
- { //重画
- CDC * pDC = this->Draw( );
- this->view->Invalidate( );
- if( this->rgn.GetSafeHandle( ) )
- this->rgn.DeleteObject( );
- CWaitCursor cursor;
- CDC memDC;
- memDC.CreateCompatibleDC( pDC );
- CBitmap bmp;
- bmp.CreateCompatibleBitmap( pDC , this->dcSize.cx , this->dcSize.cy );
- memDC.SelectObject( &bmp );
- //填充白色
- memDC.FillSolidRect( 0 , 0 , this->dcSize.cx , this->dcSize.cy , memDC.GetBkColor( ) );
- //单独在背景图上画图元
- this->Draw( &memDC );
- //图像大小
- int width = this->GetWidth( );
- //计算出矩形框区域
- CRect rc( this->start , this->end );
- rc.NormalizeRect( );
- rc.InflateRect( width + 2 , width + 2 );
- //线条颜色
- int color = this->GetColor( );
- //判断是否空心
- bool m_bEmpty = this->IsEmpty( );
- if( m_bEmpty )
- //如果是空心的,那么创建一个空的区域
- this->rgn.CreateRectRgn( 0 , 0 , 0 , 0 );
- else
- //否则创建一个实心区域
- this->rgn.CreateRectRgn( rc.left , rc.top , rc.right , rc.bottom );
- CRgn r;
- //循环把不是图元的区域去掉
- for( int h = rc.top ; h < rc.bottom; h ++ )
- for( int w = rc.left; w < rc.right ; w ++ )
- {
- if( m_bEmpty )
- {
- if( memDC.GetPixel( w , h ) == color )
- {
- r.CreateRectRgn( w , h , w + 1 , h + 1 );
- this->rgn.CombineRgn( &this->rgn , &r , RGN_OR );
- r.DeleteObject( );
- }
- }
- else
- {
- if( memDC.GetPixel( w , h ) != color )
- {
- r.CreateRectRgn( w , h , w + 1 , h + 1 );
- this->rgn.CombineRgn( &this->rgn , &r , RGN_DIFF );
- r.DeleteObject( );
- }
- }
- }
- }
- void CDraw::MouseMove( UINT nFlags , CPoint point )
- {
- if( nFlags & MK_LBUTTON )
- {
- CDC * pDC = this->view->GetMemDC( );
- //删掉画过的图像
- int rop = pDC->SetROP2( R2_NOTXORPEN );
- this->Draw( pDC );
- this->end = point;
- //重画
- this->Draw( pDC );
- //恢复
- pDC->SetROP2( rop );
- //刷新
- this->view->Invalidate( );
- }
- }