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

📄 dcutils.cpp

📁 《深入剖析visual c++编程技术vcCode(人民邮电)》的源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// DCUtils.cpp: implementation of the CDCUtils class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "dcutils.h"

#include <math.h>

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

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

const double PI = (22.00 / 7.00);

CBitmap	CDCUtils::m_bmpDesktop;

///////////////////////////////////////////////////////////////////////////////////////////////////////////Function Header
CDCUtils::CDCUtils()
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
{
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////Function Header
CDCUtils::~CDCUtils()
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
{
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////Function Header
CDC* CDCUtils::CreateBitmappedDC( CDC * pDC, CBitmap* pBitmap, CSize* pSize, BITMAP* pBmp )
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Requester MUST clean up the pointer returned!
{
	int nWidth, nHeight;

	CDC* pReturnDC = new CDC();

	CRect Client;	pDC->GetClipBox( &Client );

	if ( pSize == NULL )
	{
		nWidth	= Client.Width();
		nHeight	= Client.Height();
	}
	else
	{
		nWidth	= pSize->cx;
		nHeight	= pSize->cy;
	}
		
	pReturnDC->CreateCompatibleDC( pDC );

	if (pBitmap)
	{
		pBitmap->CreateCompatibleBitmap(pDC, nWidth, nHeight);
	}
	else
	{
		static CBitmap bmpImage;
		bmpImage.DeleteObject();
		pBitmap = &bmpImage;
		pBitmap->CreateCompatibleBitmap(pDC, nWidth, nHeight);
	}

	pReturnDC->SelectObject(pBitmap);

	if ( pBmp )
	    pBitmap->GetBitmap(pBmp);

	return pReturnDC;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////Function Header
void CDCUtils::PaintHatch(CDC* pDC, CRect area, COLORREF crFG, COLORREF crBG)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
{
	pDC->FillSolidRect(&area,crBG);

	pDC->SetBkColor( crBG );

	CBrush bgBrush;

	bgBrush.CreateHatchBrush( HS_CROSS, crFG );

	pDC->FillRect(&area,&bgBrush);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////Function Header
void CDCUtils::Draw3DRect(CDC* pDC, CRect& area, bool bDeflate, COLORREF crFill, COLORREF crHiLite, COLORREF crLoLite)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
{
	if ( crHiLite == -1 )
		crHiLite = ::GetSysColor(COLOR_BTNHILIGHT);

	if ( crLoLite == -1 )
		crLoLite = ::GetSysColor(COLOR_BTNSHADOW);

	pDC->Draw3dRect( &area, crHiLite, crLoLite );

	if ( crFill != -1 )
	{
		CRect FillRect( area );
		FillRect.DeflateRect(1,1);
		pDC->FillSolidRect( &FillRect, crFill );
	}

	//
	// This is useful if a 3d-rect is drawn and then filled. The filling normally erases the 3d-rect
	//
	if ( bDeflate )
		area.DeflateRect(1,1);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////Function Header
void CDCUtils::DrawText(	CDC *pDC, 
							CPoint Location, 
							CString strText, 
							CString strFontName,
							int nFontWidth, 
							int nFontHeight, 
							COLORREF crFontColour, 
							long FontWeight)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
{
	pDC->SetBkMode(TRANSPARENT);

	int nCharCount = strText.GetLength();

	//
	// Set the font:
	//
	CFont font;
	LOGFONT lf;										//Stores this fonts LogFont for quick retrieval
	lf.lfHeight			= nFontHeight;
	lf.lfWidth			= nFontWidth;
	lf.lfEscapement		= 0;
	lf.lfOrientation	= 0;
	lf.lfWeight			= FontWeight;				//FW_NORMAL;
	lf.lfItalic			= 0;
	lf.lfUnderline		= 0;
	lf.lfStrikeOut		= 0;
	lf.lfCharSet		= ANSI_CHARSET;
	lf.lfOutPrecision	= OUT_DEFAULT_PRECIS;
	lf.lfClipPrecision	= CLIP_DEFAULT_PRECIS;
	lf.lfQuality		= ANTIALIASED_QUALITY;
	lf.lfPitchAndFamily	= VARIABLE_PITCH | FF_ROMAN;
	
	strcpy( lf.lfFaceName, LPCTSTR(strFontName) );
	
	font.CreateFontIndirect(&lf);

	CFont* pOldFont = pDC->SelectObject(&font);

	CSize size;

	GetTextExtentPoint32(	pDC->m_hDC,				// handle to device context
							(LPCTSTR)strText,		// pointer to text string
							strText.GetLength(),	// number of characters in string
							&size);					// pointer to structure for string size

	CRect TextRect(Location, size);

	pDC->SetTextColor( crFontColour );

	pDC->DrawText(strText, &TextRect, DT_LEFT);		//  << The Text DrawText Command >>

	// Reset the Device Context :

	pDC->SelectObject(pOldFont);
}

/////////////////////////////////////////////////////////////////////////////////////////////// Function Header
void CDCUtils::PaintRect(CDC* pDC, int x, int y, int w, int h, COLORREF color)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
{
	CBrush brush(color);
	CBrush* pOldBrush = pDC->SelectObject(&brush);
	pDC->PatBlt(x, y, w, h, PATCOPY);
	pDC->SelectObject(pOldBrush);
	brush.DeleteObject();
}


/////////////////////////////////////////////////////////////////////////////////////////////// Function Header
void CDCUtils::RadialGradient(CDC *pDC, CRect &rect, COLORREF clrFrom, COLORREF clrTo)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
{
    int nR1 = GetRValue(clrFrom);
    int nG1 = GetGValue(clrFrom);
    int nB1 = GetBValue(clrFrom);
    int nR2 = GetRValue(clrTo);
    int nG2 = GetGValue(clrTo);
    int nB2 = GetBValue(clrTo);
    int nGradient;
    int cx = rect.Width()/ 2;
    int cy = rect.Height() / 2;
    int nRadius = (cx < cy) ? cx : cy;

    for (int x = 0; x < rect.Width(); x++) 
	{
        for (int y = 0; y < rect.Height(); y++) 
		{
            nGradient = ((nRadius - (int)sqrt(pow(x - cx, 2) + pow(y - cy, 2))) * 100) / nRadius;

            if (nGradient < 0 )
                nGradient = 0;

            int nR = nR1 + ((nR2 - nR1) * nGradient / 100);
            int nG = nG1 + ((nG2 - nG1) * nGradient / 100);
            int nB = nB1 + ((nB2 - nB1) * nGradient / 100);

            pDC->SetPixel(x+rect.left, y+rect.top, nR + (nG << 8) + (nB << 16));
        }
    }
}

/////////////////////////////////////////////////////////////////////////////////////////////// Function Header
void CDCUtils::LinearGradient(CDC *pDC, CRect &rect, COLORREF clrFrom, COLORREF clrTo, BOOL bHorizontal)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
{
	const int NCOLORSHADES = 255;

	int cxCap = rect.right - rect.left;
	int cyCap = rect.bottom - rect.top;

	// Get the intensity values for the ending color
	int r1 = GetRValue(clrTo); // red
	int g1 = GetGValue(clrTo); // green
	int b1 = GetBValue(clrTo); // blue
	
	// Get the intensity values for the begining color
	int r2 = GetRValue(clrFrom); // red
	int g2 = GetGValue(clrFrom); // green
	int b2 = GetBValue(clrFrom); // blue

	int r, g, b;

    if(bHorizontal) //paint horizontal rect;
    {
	    int x = cxCap;	
	    int w = x;							// width of area to shade
	    int xDelta= __max(w/NCOLORSHADES,1);	// width of one shade band


	    while (x >= xDelta) {
		    x -= xDelta;
		    if (r1 > r2)
			    r = r1 - (r1-r2)*(w-x)/w;
		    else
			    r = r1 + (r2-r1)*(w-x)/w;

		    if (g1 > g2)
			    g = g1 - (g1-g2)*(w-x)/w;
		    else
			    g = g1 + (g2-g1)*(w-x)/w;

		    if (b1 > b2)
			    b = b1 - (b1-b2)*(w-x)/w;
		    else
			    b = b1 + (b2-b1)*(w-x)/w;

		    PaintRect(pDC, rect.left+x, rect.top, xDelta, cyCap, RGB(r, g, b));
	    }
    }
    else    //paint vertical rect;
    {
	    int y = cyCap;	
	    int w = y;							// height of area to shade
	    int yDelta= max(w/NCOLORSHADES,1);	// height of one shade band


	    //while (y >= yDelta) {
        while (y > 0) {
		    y -= yDelta;
		    if (r1 > r2)
			    r = r1 - (r1-r2)*(w-y)/w;
		    else
			    r = r1 + (r2-r1)*(w-y)/w;

		    if (g1 > g2)
			    g = g1 - (g1-g2)*(w-y)/w;
		    else
			    g = g1 + (g2-g1)*(w-y)/w;

		    if (b1 > b2)
			    b = b1 - (b1-b2)*(w-y)/w;
		    else
			    b = b1 + (b2-b1)*(w-y)/w;
		    
		    PaintRect(pDC, rect.left, rect.top+y, cxCap, yDelta, RGB(r, g, b));
	    }
    }

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////Function Header
void CDCUtils::DrawElectricity(CDC *pDC, CRect rect)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
{
	static int on1,on2,oon1,oon2;

	CRgn ClipRegion;

	ClipRegion.CreateRectRgnIndirect( &rect );

	pDC->SelectClipRgn( &ClipRegion );

//	pDC->FillSolidRect(&rect,0x00000000);
	
	int nPivot = rect.Width();

	int n1,n2;
	n1 = rand()*nPivot/RAND_MAX-nPivot/2;
	n2 = rand()*nPivot/RAND_MAX-nPivot/2;

	//
	// Add extra random element: (2/10 chance of making some wild stuff on one of the curves)
	//

	int nRandElement = rand()*10/RAND_MAX;

	if(nRandElement < 2)
		n1 = rand()*(nPivot*2)/RAND_MAX-nPivot;
	else if(rand() < RAND_MAX/10)
		n2 = rand()*(nPivot*2)/RAND_MAX-nPivot;

	CPoint TopRight		( rect.right,	rect.top	);
	CPoint TopLeft		( rect.left,	rect.top	);
	CPoint BottomLeft	( rect.left,	rect.bottom );
	CPoint BottomRight	( rect.right,	rect.bottom );
	
	int	nMidHorizontal	= rect.left + ((rect.right-rect.left)/2);
	int	nMidVertical	= rect.top + ((rect.bottom-rect.top)/2);

	//
	// Top Right -> Bottom Left
	//
	CPoint points1[4] = {
		TopRight,
		CPoint( nMidHorizontal+n1,	nMidVertical	),
		CPoint( nMidHorizontal,		nMidVertical-n1	),
		BottomLeft
	};

	//
	//	Top Left -> Bottom Right
	//
	CPoint points2[4] = {
		TopLeft,
		CPoint( nMidHorizontal-n2,nMidVertical ),
		CPoint( nMidHorizontal,nMidVertical-n2 ),
		BottomRight
	};

	//
	//	Top Right -> BottomLeft
	//
	CPoint opoints1[4] = {
		TopRight,
		CPoint( nMidHorizontal+on1,nMidVertical ),
		CPoint( nMidHorizontal,nMidVertical-on1 ),
		BottomLeft
	};

	//
	//	Top Left -> Bottom Right
	//
	CPoint opoints2[4] = {
		TopLeft,
		CPoint( nMidHorizontal-on2,nMidVertical ),
		CPoint( nMidHorizontal,nMidVertical-on2 ),
		BottomRight
	};

	//
	//	Top Right -> BottomLeft
	//

⌨️ 快捷键说明

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