Draw.cpp
上传用户:popouu88
上传日期:2013-02-11
资源大小:2894k
文件大小:5k
源码类别:

IP电话/视频会议

开发平台:

Visual C++

  1. // Draw.cpp: implementation of the CDraw class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Draw.h"
  6. #ifdef _DEBUG
  7. #undef THIS_FILE
  8. static char THIS_FILE[]=__FILE__;
  9. #define new DEBUG_NEW
  10. #endif
  11. //////////////////////////////////////////////////////////////////////
  12. // Construction/Destruction
  13. //////////////////////////////////////////////////////////////////////
  14. CDraw::CDraw( CDrawView * view )
  15. {
  16. this->userName = "";
  17. this->drawId = ( int )this;
  18. this->type = 0;
  19. this->view = view;
  20. this->dcSize = this->view->GetTotalSize( );
  21. this->start = CPoint( 0 , 0 );
  22. this->end = CPoint( 0 , 0 );
  23. }
  24. CDraw::~CDraw()
  25. {
  26. this->type = 0;
  27. }
  28. void CDraw::SetColor( COLORREF color )
  29. {
  30. int width = this->GetWidth( );
  31. if( this->pen.GetSafeHandle( ) )
  32. this->pen.DeleteObject( );
  33. this->pen.CreatePen( PS_SOLID , width , color );
  34. this->SetEmpty( this->IsEmpty( ) );
  35. }
  36. COLORREF CDraw::GetColor( void )
  37. {
  38. LOGPEN logPen;
  39. logPen.lopnColor = RGB( 0 , 0 , 0 );
  40. if( this->pen.GetSafeHandle( ) )
  41. this->pen.GetLogPen( &logPen );
  42. return logPen.lopnColor;
  43. }
  44. //设定画笔宽度
  45. void CDraw::SetWidth( int width )
  46. {
  47. COLORREF color = this->GetColor( );
  48. if( this->pen.GetSafeHandle( ) )
  49. this->pen.DeleteObject( );
  50. this->pen.CreatePen( PS_SOLID , width , color );
  51. }
  52. //取得画笔宽度
  53. int CDraw::GetWidth( void )
  54. {
  55. LOGPEN logPen;
  56. logPen.lopnWidth.x = 1;
  57. if( this->pen.GetSafeHandle( ) )
  58. this->pen.GetLogPen( &logPen );
  59. return logPen.lopnWidth.x;
  60. }
  61. //设定画刷是否为空,缺省情况为空
  62. void CDraw::SetEmpty( bool m_bEmpty )
  63. {
  64. LOGBRUSH lb;
  65. lb.lbColor = this->GetColor( );
  66. lb.lbHatch = 0;
  67. lb.lbStyle = m_bEmpty ? BS_HOLLOW : BS_SOLID;
  68. if( this->brush.GetSafeHandle( ) )
  69. this->brush.DeleteObject( );
  70. this->brush.CreateBrushIndirect( &lb );
  71. }
  72. //判断画刷是否为空 
  73. bool CDraw::IsEmpty( void )
  74. {
  75. LOGBRUSH lb;
  76. lb.lbStyle = BS_HOLLOW;
  77. if( this->brush.GetSafeHandle( ) )
  78. this->brush.GetLogBrush( &lb );
  79. return lb.lbStyle == BS_HOLLOW;
  80. }
  81. CDC * CDraw::Draw( CDC * pDC )
  82. {
  83. if( pDC == NULL )
  84. pDC = this->view->GetMemDC( );
  85. if( ! this->rgn.GetSafeHandle( ) )
  86. {
  87. if( this->pen.GetSafeHandle( ) )
  88. pDC->SelectObject( &this->pen );
  89. if( this->brush.GetSafeHandle( ) )
  90. pDC->SelectObject( &this->brush );
  91. }
  92. else
  93. {
  94. CBrush b( this->GetColor( ) );
  95. pDC->FillRgn( &this->rgn , &b );
  96. pDC = NULL;
  97. }
  98. return pDC;
  99. }
  100. void CDraw::LButtonDown( UINT nFlags , CPoint point )
  101. {
  102. if( nFlags & MK_LBUTTON )
  103. this->start = this->end = point;
  104. }
  105. void CDraw::LButtonUp( UINT nFlags , CPoint point )
  106. {   //重画
  107. CDC * pDC = this->Draw( );
  108. this->view->Invalidate( );
  109. if( this->rgn.GetSafeHandle( ) )
  110. this->rgn.DeleteObject( );
  111. CWaitCursor cursor;
  112. CDC memDC;
  113. memDC.CreateCompatibleDC( pDC );
  114. CBitmap bmp;
  115. bmp.CreateCompatibleBitmap( pDC , this->dcSize.cx , this->dcSize.cy );
  116. memDC.SelectObject( &bmp );  
  117.     //填充白色
  118. memDC.FillSolidRect( 0 , 0 , this->dcSize.cx , this->dcSize.cy , memDC.GetBkColor( ) );
  119.     //单独在背景图上画图元
  120. this->Draw( &memDC );
  121. //图像大小
  122. int width = this->GetWidth( );
  123. //计算出矩形框区域
  124. CRect rc( this->start , this->end );
  125. rc.NormalizeRect( );
  126. rc.InflateRect( width + 2 , width + 2 );
  127.     //线条颜色
  128. int color = this->GetColor( );
  129.     //判断是否空心
  130. bool m_bEmpty = this->IsEmpty( );
  131.  
  132. if( m_bEmpty )
  133. //如果是空心的,那么创建一个空的区域 
  134. this->rgn.CreateRectRgn( 0 , 0 , 0 , 0 );
  135. else
  136.         //否则创建一个实心区域
  137. this->rgn.CreateRectRgn( rc.left , rc.top , rc.right , rc.bottom );
  138. CRgn r;
  139. //循环把不是图元的区域去掉 
  140. for( int h = rc.top ; h < rc.bottom; h ++ )
  141. for( int w = rc.left; w < rc.right ; w ++ )
  142. {
  143. if( m_bEmpty )
  144. {
  145. if( memDC.GetPixel( w , h ) == color )
  146. {
  147. r.CreateRectRgn( w , h , w + 1 , h + 1 );
  148. this->rgn.CombineRgn( &this->rgn , &r , RGN_OR );
  149. r.DeleteObject( );
  150. }
  151. }
  152. else
  153. {
  154. if( memDC.GetPixel( w , h ) != color )
  155. {
  156. r.CreateRectRgn( w , h , w + 1 , h + 1 );
  157. this->rgn.CombineRgn( &this->rgn , &r , RGN_DIFF );
  158. r.DeleteObject( );
  159. }
  160. }
  161. }
  162. }
  163. void CDraw::MouseMove( UINT nFlags , CPoint point )
  164. {
  165. if( nFlags & MK_LBUTTON )
  166. {
  167. CDC * pDC = this->view->GetMemDC( );
  168.         //删掉画过的图像
  169. int rop = pDC->SetROP2( R2_NOTXORPEN );
  170. this->Draw( pDC );
  171. this->end = point;
  172.         //重画 
  173. this->Draw( pDC );
  174.         //恢复
  175. pDC->SetROP2( rop );
  176.         //刷新
  177. this->view->Invalidate( );
  178. }
  179. }