⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cedraw.cpp

📁 matlab与vc6.0实现混合编程的源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
						Color = (unsigned short)( ( ColorR & 0xff ) << 11 | 
										  ( ColorG & 0xff ) << 5 | 
										  ( ColorB & 0xff ) );
						*(unsigned short*)pusDest = Color;
					}
					else if( m_gxdp.ffFormat & kfDirect555 )
					{
						/* n% ALPHA */
						Color = *(unsigned short*)pusDest;
						ColorR = (unsigned short)((Color >> 10) & 0x1f) - dwStep;
						ColorG = (unsigned short)((Color >> 5) & 0x3f) - dwStep;
						ColorB = (unsigned short)(Color & 0x1f) - dwStep;
						if( ColorR < 0 ) ColorR = 0;
						if( ColorG < 0 ) ColorG = 0;
						if( ColorB < 0 ) ColorB = 0;
						
						Color = (unsigned short)( ( ColorR & 0xff ) << 11 | 
										  ( ColorG & 0xff ) << 5 | 
										  ( ColorB & 0xff ) );
						*(unsigned short*)pusDest = Color;
					
					}
				}
				pusDest += m_cbxPitch;
			}
			pusLine += m_cbyPitch;
			EndDraw();
		}
	}
}

//
// Function : SetGDIObject()
// Purpose  : Select the gdi object
//
CCEGDIObject* CCEDraw::SetGDIObject( CCEGDIObject* pGDIObject )
{
	CCEGDIObject* pOldObject = NULL;
	// Cannot be null
	if( NULL == pGDIObject ) 
	{
		m_dwLastError = CEG_DRAW_OBJECT_NULL;
		return NULL;
	}
	switch ( pGDIObject->m_dwObjectType )
	{
	case CEG_GDIOBJECT_TYPE_PEN:
		// The Object type is Pen...
		pOldObject = m_pPen;
		m_pPen = (CCEPen*) pGDIObject;
		break;
	case CEG_GDIOBJECT_TYPE_BRUSH:
		// The Object type is Brush...
		pOldObject = m_pBrush;
		m_pBrush   = (CCEBrush*)pGDIObject;
		break;
	case CEG_GDIOBJECT_TYPE_FONT:
		// The Object type is Font...
		pOldObject = m_pFont;
		m_pFont   = (CCEFont*)pGDIObject;
		break;
	}
	return pOldObject;
}

//
// Function : GetGDIObject()
// Get the GDI Object
//
CCEGDIObject* CCEDraw::GetGDIObject( DWORD dwType )
{
	CCEGDIObject* pObject = NULL;
	switch ( dwType )
	{
	case CEG_GDIOBJECT_TYPE_PEN:
		// The Object type is Pen...
		pObject = m_pPen;
		break;
	case CEG_GDIOBJECT_TYPE_BRUSH:
		// The Object type is Brush...
		pObject = m_pBrush;
		break;
	case CEG_GDIOBJECT_TYPE_FONT:
		// The Object type is Font...
		pObject = m_pFont;
		break;
	default:
		pObject = NULL;
		break;
	}
	return pObject;
}

//
// Function : Polygon()
// This method draws a polygon consisting of two or more points connected by lines. 
// The system closes the polygon automatically by drawing a line from the last vertex to the first.
//
BOOL CCEDraw::DrawPolygon( LPPOINT lpPoints, int nCount )
{
	if( NULL == lpPoints ) return FALSE;
	if( IsAllPointOutside( lpPoints, nCount ) ) return TRUE;

	LPPOINT pPoint = lpPoints;
	for( int nIndex = 1 ; nIndex < nCount ; nIndex ++ )
	{
		DrawLine( pPoint->x, pPoint->y, (pPoint+1)->x, (pPoint+1)->y );
		pPoint ++;
	}
	// Draw The Last line...
	DrawLine( lpPoints->x, lpPoints->y, pPoint->x, pPoint->y );

	return TRUE;
}

//
// Function : Polyline()		
// This method draws a polygon consisting of two or more points connected by lines. 
//
BOOL CCEDraw::DrawPolyline( LPPOINT lpPoints, int nCount )
{
	if( NULL == lpPoints ) return FALSE;
	if( IsAllPointOutside( lpPoints, nCount ) ) return TRUE;

	LPPOINT pPoint = lpPoints;
	for( int nIndex = 1 ; nIndex < nCount ; nIndex ++ )
	{
		DrawLine( pPoint->x, pPoint->y, (pPoint+1)->x, (pPoint+1)->y );
		pPoint ++;
	}

	return TRUE;
}

//
// Function : FillPolygon()
// This method draws a polygon with the brush fill. 
// The system closes the polygon automatically by drawing a line from the last vertex to the first.
//
BOOL CCEDraw::FillPolygon( LPPOINT lpPoints, int nCount, int nOffsetX, int nOffsetY )
{
	if( NULL == lpPoints ) return FALSE;
	if( IsAllPointOutside( lpPoints, nCount ) ) return TRUE;
	PtListHeader Header;
	Header.Length   = nCount;
	Header.PointPtr = lpPoints;

	::FillPolygon( &Header, COMPLEX, nOffsetX, nOffsetY, this );
	return TRUE;
}

BOOL CCEDraw::FillPolygon( FLOATPOINT* lpPoints, int nCount, int nOffsetX, int nOffsetY )
{
	if( NULL == lpPoints ) return FALSE;
	// Check...
	BOOL bIn = FALSE;
	for( int nIndex = 0 ; nIndex < nCount ; nIndex ++ )
	{
		if( (lpPoints[nIndex].x+nOffsetX) < (LONG)m_gxdp.cxWidth && (lpPoints[nIndex].y + nOffsetY) < (LONG)m_gxdp.cyHeight 
			&& (lpPoints[nIndex].x+nOffsetX) >= 0 && (lpPoints[nIndex].y + nOffsetY) >= 0 ) 
		{
			bIn = TRUE;
			break;
		}
	}
	if(!bIn)return TRUE;
	// Check End.
	FloatPtListHeader Header;
	Header.Length   = nCount;
	Header.PointPtr = lpPoints;

	::FillFloatPolygon( &Header, COMPLEX, nOffsetX, nOffsetY, this );
	return TRUE;
}

//
// Function : DrawText()
// This method draws string to the screen.
//
BOOL CCEDraw::DrawText( LPPOINT lpPoint, LPCTSTR lpString, int nCount, UINT uFormat )
{
	// Create Font Bitmap
	HBITMAP     hOldBitmap;       // Handle to the old bitmap
	HDC         hWindowDC;
	HBITMAP     hBitmap;
	BITMAPINFO  BitmapInfo;
	RECT        rectText;
	SIZE        sizeText;
	unsigned short nColor = 0;
	LPBYTE      pBitmap;
	LPRECT      lpRect;
	
	// Get the windows device content...
	hWindowDC = GetDC( NULL );
	GetTextExtentPoint32( hWindowDC, lpString, nCount, &sizeText );
	rectText.left = 0;
	rectText.top  = 0;
	rectText.right = sizeText.cx;
	rectText.bottom = sizeText.cy;
	lpRect = &rectText;
	// Set the bitmap info...
	BitmapInfo.bmiHeader.biSize          = sizeof( BITMAPINFOHEADER );
	BitmapInfo.bmiHeader.biWidth         = lpRect->right - lpRect->left;
	BitmapInfo.bmiHeader.biHeight        = lpRect->bottom - lpRect->top;
	BitmapInfo.bmiHeader.biPlanes        = 1;
	BitmapInfo.bmiHeader.biBitCount      = 24;
	BitmapInfo.bmiHeader.biCompression   = BI_RGB;
	BitmapInfo.bmiHeader.biSizeImage     = 0;
	BitmapInfo.bmiHeader.biClrImportant  = 0;
	BitmapInfo.bmiHeader.biClrUsed       = 0;
	BitmapInfo.bmiHeader.biXPelsPerMeter = 0;
	BitmapInfo.bmiHeader.biYPelsPerMeter = 0;
	hBitmap = CreateDIBSection( hWindowDC, &BitmapInfo, DIB_RGB_COLORS, (void**)&pBitmap, NULL, 0 );
	::ReleaseDC( NULL, hWindowDC );

	memset( pBitmap, 0xff, BitmapInfo.bmiHeader.biHeight * BitmapInfo.bmiHeader.biWidth * 3 );
	
	hWindowDC = CreateCompatibleDC( NULL );
	if( IsPointOutside( lpPoint->x, lpPoint->y ) &&
		IsPointOutside( lpPoint->x + sizeText.cx, lpPoint->y + sizeText.cy ) &&
		IsPointOutside( lpPoint->x, lpPoint->y + sizeText.cy ) &&
		IsPointOutside( lpPoint->x + sizeText.cx, lpPoint->y ) ) return TRUE;

	hOldBitmap = (HBITMAP)::SelectObject( hWindowDC, hBitmap );

	// Draw the text to the dib section...
	::DrawText( hWindowDC, lpString, nCount, &rectText, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
	
	unsigned char* pVideoBuffer = m_pVB;
	// Copy it to the back buffer...
	// Set the font...
	SetBkMode( hWindowDC, TRANSPARENT );

	COLORREF crTransparent = GetBitmapPointColor( pBitmap );
	for( int nX = 0; nX < BitmapInfo.bmiHeader.biWidth ; nX ++ )
		for( int nY = 1; nY < BitmapInfo.bmiHeader.biHeight ; nY ++ )
		{
			if( GetBitmapPointColor( pBitmap +  ( nX ) * 3 +
											    ( BitmapInfo.bmiHeader.biHeight - nY ) * 
											     BitmapInfo.bmiHeader.biWidth * 3 
								   ) == crTransparent 
			  ) continue;
			  
			DrawPixel( nX + lpPoint->x, nY + lpPoint->y, nColor );
		}
	
	// cleanup
	::SelectObject( hWindowDC, hOldBitmap );
	DeleteObject( hBitmap );
    DeleteDC( hWindowDC );
	return TRUE;
}

inline int CCEDraw::TestPoint( unsigned int a,int k )
{
	a<<=(k-1);
	if(a&0x80)
	return (1);
	else
	return (0);
}

//
// Function : DrawText()
// This method draws string to the screen use hzk16 or 24
//
BOOL CCEDraw::DrawText( LPPOINT lpPoint, LPSTR lpString, int nCount, UINT uFormat )
{
	int X = lpPoint->x;
	int Y = lpPoint->y;
	unsigned int n=0;
	char zw[256];

	if( NULL == m_pFontFile ) 
	{
		m_dwLastError = CEG_DRAW_FONTFILE_NULL;
		return FALSE;
	}
	if(  IsPointOutside( lpPoint->x, lpPoint->y ) &&
		 IsPointOutside( lpPoint->x + nCount* 16, lpPoint->y + 16 ) &&
		 IsPointOutside( lpPoint->x + nCount* 16, lpPoint->y ) &&
		 IsPointOutside( lpPoint->x, lpPoint->y + 16 ) ) return TRUE;
	
	strcpy( zw, lpString );
    while( n != strlen( zw ) )
	{   register int i,j;
		register int xx=0;
		register int yy=0;
		char wm[32];
		long num;
		
		if((zw[n]&0x80)==0)
		{
			num=188+zw[n]-33;
			memcpy( wm, (m_pFontFile + num*32), 32 ); 
			for(i=0;i<32;i++)
			{
				for(j=1;j<=8;j++)
				   if(TestPoint(wm[i],j)==0) xx++;
				   else
				   {
					   DrawPixel((xx)+X,Y+yy,m_pPen->m_Color);xx++;
				   }
				   if( (i+1)%2==0 )
				   { 
					   xx=0;
					   yy++;
				   }
						  
			}
			X=X+16;
			n=n+1;
		}
		else
		{
		    zw[n]=zw[n]&0x7f;
			zw[n+1]=zw[n+1]&0x7f;
			zw[n]=zw[n]-0x20;
			zw[n+1]=zw[n+1]-0x20;
			num=(zw[n]-1)*94+(zw[n+1]-1);
			memcpy( wm, (m_pFontFile + num*32), 32 ); 
			for(i=0;i<32;i++)
			{
				for(j=1;j<=8;j++)
					if(TestPoint(wm[i],j)==0) xx++;
					else
					{
						DrawPixel((xx)+X,Y+yy,m_pPen->m_Color);
						xx++;
					}
					if((i+1)%2==0)
					{
						xx=0;
						yy++;
					}
			}
			X=X+16;n=n+2;
		}
	}	
   return TRUE;
}

//
// Function : DrawBitmap()
// Draw the bitmap to the screen...
//
BOOL CCEDraw::DrawBitmap( CCEBitmap* pBitmap, int nXDest, int nYDest, int nWidth, int nHeight, int nXSrc, int nYSrc, DWORD dwRop, float fAlpha )
{
	return pBitmap->BitBlt( this, nXDest, nYDest, nWidth, nHeight, nXSrc, nYSrc, dwRop, fAlpha );
}
	
//
// Function : DrawRect()
// Draw the rectangle
//
BOOL CCEDraw::DrawRect( LONG lStartX, LONG lStartY, LONG lEndX, LONG lEndY )
{
	LONG lTemp;
	if( IsPointOutside( lStartX, lStartY ) && IsPointOutside( lEndX, lEndY ) ) return TRUE;
	if( lStartX > lEndX )
	{
		lTemp = lStartX;
		lStartX = lEndX;
		lEndX = lTemp;
	}
	if( lStartY > lEndY )
	{
		lTemp = lStartY;
		lStartY = lEndY;
		lEndY = lTemp;
	}

	// Draw hor line
	DrawHLine( lStartX, lStartY, lEndX, lStartY );
	DrawHLine( lStartX, lEndY, lEndX, lEndY );
	// Draw v line
	DrawVLine( lStartX, lStartY, lStartX, lEndY );
	DrawVLine( lEndX, lStartY, lEndX, lEndY );
	
	return TRUE;
}

BOOL CCEDraw::DrawRect( RECT &rectDraw )
{
	return DrawRect( rectDraw.left, rectDraw.top , rectDraw.right, rectDraw.bottom );
}

//
// Function : FillRect()
// Draw a filled rectangle
//
BOOL CCEDraw::FillRect( LONG lStartX, LONG lStartY, LONG lEndX, LONG lEndY, FLOAT fAlpha )
{
	LONG lTemp;
	if( IsPointOutside( lStartX, lStartY ) && IsPointOutside( lEndX, lEndY ) ) return TRUE;
	if( lStartX > lEndX )
	{
		lTemp = lStartX;
		lStartX = lEndX;
		lEndX = lTemp;
	}
	if( lStartY > lEndY )
	{
		lTemp = lStartY;
		lStartY = lEndY;
		lEndY = lTemp;
	}
	if( lStartY < 0 ) lStartY = 0;
	if( lEndY >= (LONG)m_gxdp.cyHeight ) lEndY = (LONG)m_gxdp.cyHeight - 1;
	if( lStartX < 0 ) lStartX = 0;
	if( lEndX >= (LONG)m_gxdp.cxWidth ) lEndX = m_gxdp.cxWidth - 1;
	
	unsigned char* pBuffer = ( m_pVB + lStartY*m_cbyPitch + m_cbxPitch*lStartX );
	unsigned char* pTempBuffer = pBuffer;
	unsigned short ColorR, ColorG, ColorB, Color;
	// First draw if the color deepth is 8
	if( m_gxdp.cBPP == 8 ) 
	{
		for( int nY = lStartY ; nY < lEndY ; nY ++ )
		{
			pBuffer = pTempBuffer;
			for( int nX = lStartX ; nX < lEndX ; nX ++ )
			{
				*pBuffer = (BYTE)m_pBrush->m_Color;
				pBuffer += m_cbxPitch;
			}
			pTempBuffer += m_cbyPitch;
		}
	}
	else if( m_gxdp.cBPP == 16 )
	{
		if( m_gxdp.ffFormat & kfDirect565 && fAlpha != 1 )
		{
			for( int nY = lStartY ; nY < lEndY ; nY ++ )
			{
				pBuffer = pTempBuffer;
				for( int nX = lStartX ; nX < lEndX ; nX ++ )
				{
					/* n% ALPHA */
					Color  = *(unsigned short*)pBuffer;
					ColorR = (unsigned short)((Color >> 11) & 0x1f) * ( 1 - fAlpha );
					ColorG = (unsigned short)((Color >> 5) & 0x3f) * ( 1 - fAlpha );
					ColorB = (unsigned short)(Color & 0x1f) * ( 1 - fAlpha );
					Color  = m_pBrush->m_Color;
					ColorR = (unsigned short)(((Color >> 11)&0x1f) * fAlpha + ColorR);
					ColorG = (unsigned short)(((Color >> 5)&0x3f) * fAlpha + ColorG);
					ColorB = (unsigned short)((Color & 0x1f)* fAlpha + ColorB);
					Color  = (unsigned short)( ( ColorR & 0xff ) << 11 | 
											  ( ColorG & 0xff ) << 5 | 
											  ( ColorB & 0xff ) );
					*(unsigned short*)pBuffer = Color;
					pBuffer += m_cbxPitch;
				}
				pTempBuffer += m_cbyPitch;
			}
		}
		else if( m_gxdp.ffFormat & kfDirect555 && fAlpha != 1 )
		{
			for( int nY = lStartY ; nY < lEndY ; nY ++ )
			{
				pBuffer = pTempBuffer;
				for( int nX = lStartX ; nX < lEndX ; nX ++ )
				{
					/* n% ALPHA */
					Color  = *(unsigned short*)pBuffer;
					ColorR = (unsigned short)((Color >> 10) & 0x1f) * ( 1 - fAlpha );
					ColorG = (unsigned short)((Color >> 5) & 0x1f) * ( 1 - fAlpha );
					ColorB = (unsigned short)(Color & 0x1f) * ( 1 - fAlpha );
					Color  = m_pBrush->m_Color;
					ColorR = (unsigned short)(((Color >> 10)&0x1f) * fAlpha + ColorR);
					ColorG = (unsigned short)(((Color >> 5)&0x1f) * fAlpha + ColorG);
					ColorB = (unsigned short)((Color & 0x1f)* fAlpha + ColorB);
					Color  = (unsigned short)( ( ColorR & 0xff ) << 10 | 
											  ( ColorG & 0xff ) << 5 | 
											  ( ColorB & 0xff ) );
					*(unsigned short*)pBuffer = Color;
					pBuffer += m_cbxPitch;
				}
				pTempBuffer += m_cbyPitch;
			}
		}
		else
		{
			for( int nY = lStartY ; nY < lEndY ; nY ++ )
			{
				pBuffer = pTempBuffer;
				for( int nX = lStartX ; nX < lEndX ; nX ++ )
				{
					*(unsigned short*)pBuffer = m_pBrush->m_Color;
					pBuffer += m_cbxPitch;
				}
				pTempBuffer += m_cbyPitch;
			}
		}
	}
	return TRUE;	
}

BOOL CCEDraw::FillRect( RECT &rectDraw, FLOAT fAlpha )
{	
	return FillRect( rectDraw.left, rectDraw.top , rectDraw.right, rectDraw.bottom, fAlpha );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -