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

📄 hsgraphics.cpp

📁 漂亮菜单
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// HSGraphics.cpp: implementation of the CHSGraphics class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "HSGraphics.h"
#include <math.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

///////////////////////////////////////////////////////////////////////////////////////////////
//$//								作者 : 韩 松                                 
//$//                                                                          
//$//			程序名称 :CHSMenuPro                                              
//$//			程序类型 :菜单类												
//$//			邮箱地址 :hs_china@yahoo.com								
//$//           QQ号码    :102567329
//$//																		
//$//			作者声明 :此部分代码全是作者所写,可以随便传播,但要保持  
//$//					   文件的完整性,如果您有问题或有好的建议、意见请您
//$//					   给我来信,非常感谢!							
//$//                                      
//$//																	
//////////////////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

/*
	Draw3DLine
	 画一条具有3D外观的直线.
	参数:
	hDC 
*/

void WINAPI Draw3DLine(HDC hDC, BOOL bHeave, const POINT& ptStart, const POINT& ptEnd)
{
	HPEN	hpen;
	HGDIOBJ hpenOld;

	hpen = ::CreatePen( PS_SOLID, 1, GetSysColor( bHeave ? COLOR_3DHILIGHT : COLOR_3DSHADOW  ) );
	hpenOld = ::SelectObject( hDC, hpen );
	::MoveToEx( hDC, ptStart.x, ptStart.y, NULL );
	::LineTo( hDC, ptEnd.x, ptEnd.y );
	::SelectObject( hDC, hpenOld );
	::DeleteObject( hpen );
	
	hpen = ::CreatePen( PS_SOLID, 1, GetSysColor( bHeave ? COLOR_3DSHADOW : COLOR_3DHILIGHT ) );
	hpenOld = ::SelectObject( hDC, hpen );
	::MoveToEx( hDC, ptStart.x, ptStart.y+1, NULL );
	::LineTo( hDC, ptEnd.x, ptEnd.y+1 );
	::SelectObject( hDC, hpenOld );
	::DeleteObject( hpen );
}

void WINAPI DrawRectEdge(HDC hDC, const RECT& rect, int nWidth, COLORREF clrEdge, int nStyle)
{
	HPEN	hpen	= ::CreatePen( nStyle, nWidth, clrEdge );
	HGDIOBJ	hobjOld = ::SelectObject( hDC, hpen );

	POINT	pt[3];

	pt[0].x = pt[1].x = rect.left;							//	 p1	+-----+ p2
	pt[0].y = rect.bottom;									//		|	
	pt[1].y = pt[2].y = rect.top;							//		|
	pt[2].x = rect.right;									//	 p0	+

	::Polyline( hDC, pt, 3 );

	pt[1].x = pt[2].x = rect.right;							//		      + p2
	pt[1].y = rect.bottom;									//			  |	
	pt[2].y = rect.top;										//			  |			
															//	 p0 +-----+	p1	
	::Polyline( hDC, pt, 3 );

	::SelectObject( hDC, hobjOld );
	::DeleteObject( hpen );
}

void WINAPI Draw3DRectEdge(HDC hDC, BOOL bHeave, int nLeft, int nTop, int nRight, int nBottom)
{
	HPEN	hpen;
	HGDIOBJ hpenOld;
	POINT	pt[3];

	pt[0].x = pt[1].x = nLeft;							//	 p1	+-----+ p2
	pt[0].y = nBottom;									//		|	
	pt[1].y = pt[2].y = nTop;							//		|
	pt[2].x = nRight;									//	 p0	+
		
	hpen = ::CreatePen( PS_SOLID, 1, GetSysColor( bHeave ? COLOR_3DHILIGHT : COLOR_3DSHADOW  ) );
	hpenOld = ::SelectObject( hDC, hpen );
	::Polyline( hDC, pt, 3 );
	::SelectObject( hDC, hpenOld );
	::DeleteObject( hpen );

	pt[1].x = pt[2].x = nRight;							//		      + p2
	pt[1].y = nBottom;									//			  |	
	pt[2].y = nTop;										//			  |			
														//	 p0 +-----+	p1	

	hpen = ::CreatePen( PS_SOLID, 1, GetSysColor( bHeave ? COLOR_3DSHADOW : COLOR_3DHILIGHT ) );
	hpenOld = ::SelectObject( hDC, hpen );
	::Polyline( hDC, pt, 3 );
	::SelectObject( hDC, hpenOld );
	::DeleteObject( hpen );
}

void WINAPI Draw3DRgnEdge(HDC hDC, BOOL bHeave, HRGN rgnWnd)
{
	HBRUSH		hbr;
	HRGN		rgnTemp = ::CreateRectRgn( 0, 0, 0, 0 );

	::CombineRgn( rgnTemp, rgnWnd, 0, RGN_COPY );
	::OffsetRgn ( rgnTemp, 1, 1 );
	::CombineRgn( rgnTemp, rgnWnd, rgnTemp, RGN_DIFF );
	hbr = ::CreateSolidBrush( GetSysColor( bHeave ? COLOR_3DHILIGHT : COLOR_3DSHADOW ) );
	::FillRgn( hDC, rgnTemp, hbr );
	::DeleteObject( hbr );
	
	::CombineRgn( rgnTemp, rgnWnd, 0, RGN_COPY );
	::OffsetRgn( rgnTemp, -1, -1 );
	::CombineRgn( rgnTemp, rgnWnd, rgnTemp, RGN_DIFF );
	hbr = ::CreateSolidBrush( GetSysColor( bHeave ? COLOR_3DSHADOW : COLOR_3DHILIGHT ) );
	::FillRgn( hDC, rgnTemp, hbr );
	::DeleteObject( hbr );

	::DeleteObject( rgnTemp );
}


void WINAPI DrawGradientRgn(HDC hDC, BOOL bVertical, HRGN hRgn, COLORREF clrStart, COLORREF clrEnd)
{
	RECT	rect;
	RECT	rcFill;

	::GetRgnBox ( hRgn, &rect );
	int	nWidth  = rect.right  - rect.left;
	int	nHeight = rect.bottom - rect.top; 

	rcFill.left		= 0;
	rcFill.top		= 0;
	rcFill.right	= nWidth;
	rcFill.bottom	= nHeight;

	HRGN	hrgnTmp = ::CreateRectRgn( 0, 0, 0, 0 );
	::CombineRgn( hrgnTmp, hRgn, 0, RGN_COPY );
	::OffsetRgn( hrgnTmp, -rect.left, -rect.top );

	HDC		hdcMem   = ::CreateCompatibleDC( hDC );
	HBITMAP	hbmpMem  = ::CreateCompatibleBitmap( hDC, nWidth, nHeight );
	HGDIOBJ hobjOld  = ::SelectObject( hdcMem, hbmpMem );

	DrawRect( hdcMem, 0x00FFFFFF, rcFill.left, rcFill.top, rcFill.right, rcFill.bottom );

	HGDIOBJ holdRgn = ::SelectObject( hdcMem, hrgnTmp );

	DrawGradientRect( hdcMem, bVertical, rcFill, clrStart, clrEnd );

	BitBltTran( hDC, rect.left, rect.top, nWidth, nHeight, hdcMem, 0, 0, 0x00FFFFFF );

	::SelectObject( hdcMem, holdRgn );
	::SelectObject( hdcMem, hobjOld );
	::DeleteObject( hrgnTmp );	
	::DeleteObject( hbmpMem );
	::DeleteDC( hdcMem );	
}

void WINAPI BitBltTran( HDC hDC, int nLeft, int nTop, int nWidth, int nHeight, HDC hdcSrc, int nX, int nY, COLORREF clrTran )
{
	HDC		hdcTmp  = ::CreateCompatibleDC( hDC );
	HBITMAP	bmpTmp  = ::CreateCompatibleBitmap( hDC, nWidth, nHeight );
	HGDIOBJ hbmpOld = ::SelectObject( hdcTmp, bmpTmp );

	HDC		hdcMsk  = ::CreateCompatibleDC( hDC );
	HBITMAP hbmpMsk = ::CreateBitmap( nWidth, nHeight, 1, 1, NULL );
	HGDIOBJ hMskOld = ::SelectObject( hdcMsk, hbmpMsk );

	COLORREF clrBkOld = ::SetBkColor( hdcSrc, clrTran );
	::BitBlt( hdcMsk, 0, 0, nWidth, nHeight, hdcSrc, nX, nY, SRCCOPY );
	::SetBkColor( hdcSrc, clrBkOld );

	::BitBlt( hdcTmp, 0, 0, nWidth, nHeight, hDC, nLeft, nTop, SRCCOPY );

	::BitBlt( hdcTmp, 0, 0, nWidth, nHeight, hdcSrc, nX, nY, SRCINVERT );
	::BitBlt( hdcTmp, 0, 0, nWidth, nHeight, hdcMsk, 0, 0, SRCAND );
	::BitBlt( hdcTmp, 0, 0, nWidth, nHeight, hdcSrc, nX, nY, SRCINVERT );

	::BitBlt( hDC, nLeft, nTop, nWidth, nHeight, hdcTmp, 0, 0, SRCCOPY );

	::SelectObject( hdcMsk, hMskOld );
	::DeleteObject( hbmpMsk );
	::DeleteDC( hdcMsk );

	::SelectObject( hdcTmp, hbmpOld );
	::DeleteObject( bmpTmp );
	::DeleteDC( hdcTmp );	
}

/*
void WINAPI DrawGradientRgn(HDC hDC, BOOL bVertical, HRGN hRgn, COLORREF clrStart, COLORREF clrEnd)
{
	float	fStep;
	RECT	rect;
	HBRUSH	hbrFill;
	HRGN	hRgnFill = ::CreateRectRgn( 0, 0, 0, 0 );
	HRGN	hRgnTemp = ::CreateRectRgn( 0, 0, 0, 0 );

	::GetRgnBox ( hRgn, &rect );
	::CombineRgn( hRgnTemp, hRgn, 0, RGN_COPY );
	::OffsetRgn ( hRgnTemp, -rect.left, -rect.top );

	int	nWidth  = rect.right  - rect.left;
	int	nHeight = rect.bottom - rect.top; 

	HRGN	hRgnRect = ::CreateRectRgn( 0, 0, nWidth, nHeight );

	int nR = GetRValue( clrStart );
	int nG = GetGValue( clrStart );
	int nB = GetBValue( clrStart );

	float fRStep = ( GetRValue( clrEnd ) - GetRValue( clrStart ) ) / (float)STEP;
	float fGStep = ( GetGValue( clrEnd ) - GetGValue( clrStart ) ) / (float)STEP;
	float fBStep = ( GetBValue( clrEnd ) - GetBValue( clrStart ) ) / (float)STEP;

	HDC		hdcMem   = ::CreateCompatibleDC( hDC );
	HBITMAP	hbmpMem     = ::CreateCompatibleBitmap( hDC, nWidth, nHeight );
	HGDIOBJ hobjOld = ::SelectObject( hdcMem, hbmpMem );

	hbrFill = ::CreateSolidBrush( 0x00FFFFFF );	// White
	::FillRgn( hdcMem, hRgnRect, hbrFill );
	::DeleteObject( hbrFill );

	if ( bVertical )
	{
		int nBottom, nTop = 0; 

		fStep = (float)nHeight / (float)STEP;

		for ( int nCount=0; nCount<=STEP; nCount++ )
		{
			nBottom = (int)( (nCount+1) * fStep );

			::SetRectRgn( hRgnRect, 0, nTop, nWidth, nBottom );
			::CombineRgn( hRgnFill, hRgnRect, hRgnTemp, RGN_AND );

			hbrFill = ::CreateSolidBrush( RGB( nR+fRStep*nCount, nG+fGStep*nCount, nB+fBStep*nCount ) );
			::FillRgn( hdcMem, hRgnFill, hbrFill );
			::DeleteObject( hbrFill );

			nTop = nBottom;
		}
	}
	else
	{
		int nRight, nLeft = 0;

		fStep = (float)nWidth / (float)STEP;

		for ( int nCount=0; nCount<=STEP; nCount++ )
		{
			nRight = (int)( (nCount+1) * fStep );

			::SetRectRgn( hRgnRect, nLeft, 0, nRight, nHeight );
			::CombineRgn( hRgnFill, hRgnRect, hRgnTemp, RGN_AND );

			hbrFill = ::CreateSolidBrush( RGB( nR+fRStep*nCount, nG+fGStep*nCount, nB+fBStep*nCount ) );
			::FillRgn( hdcMem, hRgnFill, hbrFill );
			::DeleteObject( hbrFill );

			nLeft = nRight;
		}
	}

	BitBltTran( hDC, rect.left, rect.top, nWidth, nHeight, hdcMem, 0, 0, 0x00FFFFFF );

	::DeleteObject( hRgnRect );
	::DeleteObject( hRgnTemp );
	::DeleteObject( hRgnFill );

	::SelectObject( hdcMem, hobjOld );
	::DeleteObject( hbmpMem );
	::DeleteDC( hdcMem );	
}
*/
void WINAPI DrawGradientRect(HDC hDC, BOOL bVertical, const RECT &rectFill, COLORREF clrStart, COLORREF clrEnd)
{
	float	fStep;
	RECT	rect;
	HBRUSH	hbrFill;

	int		nWidth  = rectFill.right  - rectFill.left;
	int		nHeight = rectFill.bottom - rectFill.top; 

	int nR = GetRValue( clrStart );
	int nG = GetGValue( clrStart );
	int nB = GetBValue( clrStart );

	float fRStep = ( GetRValue( clrEnd ) - GetRValue( clrStart ) ) / (float)STEP;
	float fGStep = ( GetGValue( clrEnd ) - GetGValue( clrStart ) ) / (float)STEP;
	float fBStep = ( GetBValue( clrEnd ) - GetBValue( clrStart ) ) / (float)STEP;

	HDC		hdcMem   = ::CreateCompatibleDC( hDC );
	HBITMAP	hbmpMem  = ::CreateCompatibleBitmap( hDC, nWidth, nHeight );
	HGDIOBJ hobjOld  = ::SelectObject( hdcMem, hbmpMem );

	rect.top = rect.left = 0;

	if ( bVertical )
	{
		rect.right	= nWidth;
		fStep		= (float)nHeight / (float)STEP;

		for ( int nCount=0; nCount<=STEP; nCount++ )
		{
			rect.bottom = (int)( (nCount+1) * fStep );
	
			hbrFill = ::CreateSolidBrush( RGB( nR+fRStep*nCount, nG+fGStep*nCount, nB+fBStep*nCount ) );
			::FillRect( hdcMem, &rect, hbrFill );
			::DeleteObject( hbrFill );

			rect.top = rect.bottom;
		}
	}
	else
	{
		rect.bottom = nHeight;
		fStep		= (float)nWidth / (float)STEP;

		for ( int nCount=0; nCount<=STEP; nCount++ )
		{
			rect.right	= (int)( (nCount+1) * fStep );

			hbrFill = ::CreateSolidBrush( RGB( nR+fRStep*nCount, nG+fGStep*nCount, nB+fBStep*nCount ) );
			::FillRect( hdcMem, &rect, hbrFill );
			::DeleteObject( hbrFill );

			rect.left = rect.right;
		}
	}
	
	::BitBlt( hDC, rectFill.left, rectFill.top, nWidth, nHeight, hdcMem, 0, 0, SRCCOPY );

	::SelectObject( hdcMem, hobjOld );
	::DeleteObject( hbmpMem );
	::DeleteDC( hdcMem );
}

void WINAPI DrawLightPointRect(HDC hDC, const RECT &rectFill, const POINT &pointLight, COLORREF clrBkgnd, COLORREF clrPoint, float fFouce)
{
	HBRUSH	hbrFill;

	int		nStep;

	int nX = pointLight.x - rectFill.left;
	int nY = pointLight.y - rectFill.top;

	int	nWidth  = rectFill.right  - rectFill.left;
	int	nHeight = rectFill.bottom - rectFill.top; 

	int nR = GetRValue( clrPoint );
	int nG = GetGValue( clrPoint );
	int nB = GetBValue( clrPoint );

	float fRStep = ( GetRValue( clrBkgnd ) - GetRValue( clrPoint ) ) / (float)STEP;
	float fGStep = ( GetGValue( clrBkgnd ) - GetGValue( clrPoint ) ) / (float)STEP;
	float fBStep = ( GetBValue( clrBkgnd ) - GetBValue( clrPoint ) ) / (float)STEP;

	HDC		hdcMem   = ::CreateCompatibleDC( hDC );
	HBITMAP	hbmpMem     = ::CreateCompatibleBitmap( hDC, nWidth, nHeight );
	HGDIOBJ hobjOld = ::SelectObject( hdcMem, hbmpMem );

	HRGN	rgnTemp;
	HRGN	rgnRect	 = ::CreateRectRgn( 0, 0, nWidth, nHeight );
	HRGN	rgnLight = ::CreateEllipticRgn( nX-1, nY-1, nX+1, nY+1 );

	hbrFill = ::CreateSolidBrush( clrBkgnd );
	::FillRgn( hdcMem, rgnRect, hbrFill );
	::DeleteObject( hbrFill );

⌨️ 快捷键说明

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