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

📄 dcutils.cpp

📁 《深入剖析visual c++编程技术vcCode(人民邮电)》的源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	CPoint oopoints1[4] = {
		TopRight,
		CPoint( nMidHorizontal+oon1,nMidVertical ),
		CPoint( nMidHorizontal,nMidVertical-oon1 ),
		BottomLeft
	};

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

	CPen wpen(	PS_SOLID,1,RGB(150,220,255));
	CPen pen(	PS_SOLID,2,RGB(50,100,255));
	CPen open(	PS_SOLID,1,RGB(50,100,255));
	CPen oopen(	PS_SOLID,1,RGB(0,30,150));
	
	CPen *pOldPen = pDC->SelectObject(&oopen);
	pDC->PolyBezier(oopoints1,4);
	pDC->PolyBezier(oopoints2,4);

	pDC->SelectObject(&open);
	pDC->PolyBezier(opoints1,4);
	pDC->PolyBezier(opoints2,4);
	
	pDC->SelectObject(&pen);
	pDC->PolyBezier(points1,4);
	pDC->PolyBezier(points2,4);
	
	pDC->SelectObject(&wpen);
	pDC->PolyBezier(points1,4);
	pDC->PolyBezier(points2,4);
	
	pDC->SelectObject(pOldPen);

	oon1 = on1;
	oon2 = on2;
	on1 = n1;
	on2 = n2;

	pDC->SelectClipRgn( NULL );
	ClipRegion.DeleteObject();
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////Function Header


///////////////////////////////////////////////////////////////////////////////////////////////////////////Function Header
void CDCUtils::MakeGlass( CDC *pDC, CWnd* pWnd, CRect rect, UINT uBmpID )
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
{
	CDCUtils::PaintDesktop( pDC, pWnd, CRect(0,0,0,0), rect );

	CDCUtils::PaintBitmap( pDC, pWnd, rect, uBmpID, true, true );
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////Function Header
void CDCUtils::PaintBitmap( CDC *pDC, CWnd* pWnd, CRect rect, UINT uBmpID, bool bStretchToFit, bool bOverlay )
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//	Useful for painting to Screen Related coordinated blitting
//
{
	UINT uCopyOption = SRCCOPY;	if ( bOverlay )	uCopyOption = SRCAND;

	CSize BmpSize;
	CDC* pOverlayDC = GetDCFromBmp( pDC, uBmpID, &BmpSize );

	if ( bStretchToFit )
	{
		pDC->StretchBlt(	rect.left,
							rect.top, 
							rect.Width(), 
							rect.Height(), 
							pOverlayDC, 
							0, 
							0, 
							BmpSize.cx, 
							BmpSize.cy, 
							uCopyOption);
	}
	else
	{
	    pDC->BitBlt(	rect.left, 
						rect.top, 
						rect.Width(),
						rect.Height(), 
						pOverlayDC, 
						0,
						0,
						uCopyOption);
	}

	delete pOverlayDC;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////Function Header
double CDCUtils::DegreeToRadian( double degree )	{	return ( (PI/180) * degree );	}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////Function Header
void CDCUtils::BlitBitmap( CDC *pDC, CRect rect, UINT uResourceID, COLORREF crMask, double dDegreesRotation )
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
{
	CSize	ImageSize;
	COLORREF crPixel;

	CDC*	pBitmapDC		= GetDCFromBmp( pDC, uResourceID, &ImageSize );

	const int nWidth		= ImageSize.cx;
	const int nHeight		= ImageSize.cy;
    CPoint ptAxle			= CPoint(nWidth / 2, nHeight / 2);
    CPoint ptOffset			= CPoint((rect.Width() - nWidth) / 2, (rect.Height() - nHeight) / 2);


	if ( dDegreesRotation > 360.0 )
		dDegreesRotation = (int)dDegreesRotation % 360;

	double dRadians = DegreeToRadian ( dDegreesRotation );

    for (int x1 = 0; x1 < nWidth; x1++) 
	{
        for (int y1 = 0; y1 < nHeight; y1++) 
		{
			crPixel = pBitmapDC->GetPixel(x1, y1);

            int x2 = rect.left + ptOffset.x + (int)(ptAxle.x + ((x1 - ptAxle.x) * sin(dRadians)) + ((y1 - ptAxle.y) * cos(dRadians)));
            int y2 = rect.top + ptOffset.y + (int)(ptAxle.y + ((y1 - ptAxle.y) * sin(dRadians)) - ((x1 - ptAxle.x) * cos(dRadians)));
            
			if (crPixel != crMask) 
			{
                long lRes1 = pDC->SetPixel( x2,		y2, crPixel );
                long lRes2 = pDC->SetPixel( x2 + 1,	y2, crPixel );
            }
			else
			{
				//-------- NOTE: -----------
				//
				//	Here's a cool effect I discovered by mistake: Try it!
				//
                //long lRes2 = pOffScreenDC->SetPixel( x2+1,	y1, pDC->GetPixel(x1, y1) );
			}
        }
    }

	delete pBitmapDC;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////Function Header
void CDCUtils::TileBitmap( CDC *pDC, CRect rect, UINT uResourceID )
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
{
	CBitmap bmpTile;

	CRgn ClipRegion;

	ClipRegion.CreateRectRgnIndirect( &rect );

	pDC->SelectClipRgn( &ClipRegion );

	//    
	// Paint the window background with the bitmap.    
	//    

	CSize TileSize;

	CDC* pTileDC = GetDCFromBmp( pDC, uResourceID, &TileSize );

    bmpTile.CreateCompatibleBitmap( pTileDC, TileSize.cx, TileSize.cy );

	int nTileWidth	= TileSize.cx;
	int nTileHeight	= TileSize.cy;

	int nStartCol	= rect.left / nTileWidth;
    int nEndCol		= (rect.right + nTileWidth + 1) / nTileWidth;
    int nStartRow	= rect.top / nTileHeight;
    int nEndRow		= (rect.bottom + nTileHeight + 1) / nTileHeight;

    for (int i=nStartCol; i<nEndCol; i++) 
	{
        for (int j=nStartRow; j<nEndRow; j++) 
		{
            int x = i * nTileWidth;            
			int y = j * nTileHeight;
            pDC->BitBlt (x, y, nTileWidth, nTileHeight, pTileDC, 0, 0, SRCCOPY);        
		}    
	}

	pDC->SelectClipRgn( NULL );
	ClipRegion.DeleteObject();

	delete pTileDC;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////Function Header
void CDCUtils::DrawSineWave( CDC *pDC, CRect rect, double dwCycles, COLORREF crPen )
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//	dwCycles is the number of full waves
//
{
	ASSERT ( dwCycles > 0 );


	CPen Marker( PS_SOLID, 1, crPen );
	CPen *pOldPen = pDC->SelectObject(&Marker);

	int nWidth = rect.Height();
	double dwStep = (2*PI*dwCycles)/((double)rect.Width());
	double dwAmplitude = rect.Height()/2;
	bool bFirstDot = true;
	bool bDotsOnly = false;

    for (int x = 0; x < rect.Width(); x++) 
	{
		int nOffset = (rect.top)+rect.Height()/2;

        int yPos	= (int) (sin( x*dwStep )* (dwAmplitude)) + nOffset;
		int xPos	= x+rect.left;

		if ( bFirstDot && !bDotsOnly )
		{
			pDC->MoveTo( xPos, yPos );
			bFirstDot = false;
		}

		if ( bDotsOnly )
	        pDC->SetPixel(xPos, yPos, crPen);
		else
			pDC->LineTo( xPos, yPos );

    }

	pDC->SelectObject(pOldPen);
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////Function Header
void CDCUtils::DrawLine( CDC *pDC, CPoint From, CPoint To )
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
{
    pDC->MoveTo(From);
    pDC->LineTo(To);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////Function Header
void CDCUtils::PaintDesktop( CDC *pDC, CWnd* pWnd, CRect SourceRect, CRect DestRect )
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
{
	CRect	LastRect;
	CPoint	Point;

	//
	// If source is empty, the user wants it to be the same as the destination rect
	//
	if ( SourceRect == CRect(0,0,0,0) )
	{
		SourceRect = DestRect;
		Point = CPoint( SourceRect.left, SourceRect.top );
		ClientToScreen(pWnd->GetSafeHwnd(), &Point);
	}
	else
	{
		// If the source rect is *specified*, then it is a screen co-ord, not client:
		Point = CPoint( SourceRect.left, SourceRect.top );
	}

	CWnd* pFrameWnd = AfxGetMainWnd();
	
	ASSERT( pFrameWnd );

	pFrameWnd->GetWindowRect( LastRect );

	//
	// Get the Calling Window out the way, so that we can get a clean snapshot...
	//

	CDC* pDesktopDC = GetDCFromBmp( pDC, m_bmpDesktop );

	//
	// Blit the buffer's contents (constrained by incoming size) to the calling DC:
	//

    pDC->BitBlt(		DestRect.left, 
						DestRect.top, 
						DestRect.Width(), 
						DestRect.Height(), 
						pDesktopDC,			// From DC
						Point.x,			// xSrc
						Point.y,			// ySrc
						SRCCOPY);

	//
	//
	//
	
	//
	// Cleanup
	//

	delete pDesktopDC;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////Function Header
CDC* CDCUtils::GetDCFromBmp( CDC *pDC, CBitmap& bmpSource )
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
{
	CDC* pReturnDC = new CDC();
    pReturnDC->CreateCompatibleDC(pDC);
    pReturnDC->SelectObject(&bmpSource);

	//
	// Caller cleans up!!!
	//
	return pReturnDC;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////Function Header
CDC* CDCUtils::GetDCFromBmp( CDC *pDC, UINT uResourceID, CSize* pSize )
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
{
	CBitmap bmpImage;

	HANDLE hBitmap = ::LoadImage (	AfxGetInstanceHandle(),
									MAKEINTRESOURCE (uResourceID), 
									IMAGE_BITMAP, 
									0, 
									0, 
									LR_CREATEDIBSECTION);

	ASSERT (hBitmap); 

	bmpImage.Attach (hBitmap);

	if (pSize)
	{
		BITMAP bmpInfo;
	    bmpImage.GetBitmap(&bmpInfo);
		*pSize = CSize(bmpInfo.bmWidth, bmpInfo.bmHeight );
	}
	
	return GetDCFromBmp( pDC, bmpImage );
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////Function Header
void CDCUtils::GetDesktopImage( CBitmap* pBitmap )
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
{
	if ( pBitmap == NULL )
		pBitmap = &m_bmpDesktop;

    CDC dcDesktop;

    CSize DesktopSize( GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN) );

	//
	// Get the Desktop's Device Context
	//

    dcDesktop.m_hDC = ::GetDC(NULL);

	//
	// Initialise the bitmap according to the Desktop DC
	//

    CDC* pBitmapDC = CreateBitmappedDC(&dcDesktop, pBitmap );

    pBitmapDC->BitBlt(0, 0, DesktopSize.cx, DesktopSize.cy, &dcDesktop, 0, 0, SRCCOPY);

	//
	// Cleanup
	//

    dcDesktop.DeleteDC();

	delete pBitmapDC;
}

⌨️ 快捷键说明

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