A Journey 2 Eternity

Archive for September 9th, 2008

void CEMCHotButton::DisabledBlt (HDC hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, HBITMAP hBitmap, int nXSrc, int nYSrc)
{
	ASSERT (hdcDest && hBitmap);
	ASSERT (nWidth > 0 && nHeight > 0);

	// Create a generic DC for all BitBlts
	HDC hDC = CreateCompatibleDC (hdcDest);
	ASSERT (hDC);

	if (hDC) {
		// Create a DC for the monochrome DIB section
		HDC bwDC = CreateCompatibleDC(hDC);
		ASSERT (bwDC);

		if (bwDC) {
			// Create the monochrome DIB section with a black and white palette
			struct
			{
				BITMAPINFOHEADER bmiHeader;
				RGBQUAD bmiColors[2];
			} RGBBWBITMAPINFO = 
			{
				{ // a BITMAPINFOHEADER
					sizeof(BITMAPINFOHEADER), // biSize
					nWidth, // biWidth;
					nHeight, // biHeight;
					1, // biPlanes;
					1, // biBitCount
					BI_RGB, // biCompression;
					0, // biSizeImage;
					0, // biXPelsPerMeter;
					0, // biYPelsPerMeter;
					0, // biClrUsed;
					0 // biClrImportant;
				},
				{
					{ 0×00, 0×00, 0×00, 0×00 },
					{ 0xFF, 0xFF, 0xFF, 0×00 }
				}
			};

			void* pbitsBW;
			HBITMAP hBitmapBW = CreateDIBSection (bwDC, (LPBITMAPINFO) &RGBBWBITMAPINFO, DIB_RGB_COLORS, &pbitsBW, NULL, 0);
			ASSERT (hBitmapBW);

			if (hBitmapBW) {
				// Attach the monochrome DIB section and the bitmap to the DCs
				SelectObject (bwDC, hBitmapBW);
				SelectObject (hDC, hBitmap);

				// BitBlt the bitmap into the monochrome DIB section
				BitBlt (bwDC, 0, 0, nWidth, nHeight, hDC, nXSrc, nYSrc, SRCCOPY);

				// Paint the destination rectangle in gray
				FillRect (hdcDest, CRect (nXDest, nYDest, nXDest + nWidth, nYDest + nHeight), GetSysColorBrush (COLOR_3DFACE));

				// BitBlt the black bits in the monochrome bitmap into COLOR_3DHILIGHT bits in the destination DC
				// The magic ROP comes from the Charles Petzold’s book

				HBRUSH hb = CreateSolidBrush (GetSysColor (COLOR_3DHILIGHT));
				HBRUSH oldBrush = (HBRUSH) SelectObject (hdcDest, hb);
				BitBlt (hdcDest, nXDest + 1, nYDest + 1, nWidth, nHeight, bwDC, 0, 0, 0xB8074A);

				// BitBlt the black bits in the monochrome bitmap into COLOR_3DSHADOW bits in the destination DC
				hb = CreateSolidBrush (GetSysColor (COLOR_3DSHADOW));
				DeleteObject (SelectObject(hdcDest, hb));

				BitBlt (hdcDest, nXDest, nYDest, nWidth, nHeight, bwDC, 0, 0, 0xB8074A);

				DeleteObject (SelectObject (hdcDest, oldBrush));
				DeleteObject (hBitmapBW);
			}

			VERIFY (DeleteDC (bwDC));
		}

		VERIFY (DeleteDC(hDC));
	}
}
void CEMCHotButton::DrawGradientRect (CDC *pDC, CRect r, COLORREF cLeft, COLORREF cRight, BOOL bVertical)
{
	CRect stepR; // rectangle for color’s band
	COLORREF color; // color for the bands
	float fStep;
	
	if(bVertical) {
		fStep = ((float)r.Height())/255.0f;
	} else {
		fStep = ((float)r.Width())/255.0f; // width of color’s band
	}

	for (int iOnBand = 0; iOnBand < 255; iOnBand++) {
		// set current band
		if(bVertical) {
			SetRect(&stepR, r.left, r.top+(int)(iOnBand * fStep), r.right, r.top+(int)((iOnBand+1)* fStep));
		} else {
			SetRect(&stepR, r.left+(int)(iOnBand * fStep), r.top, r.left+(int)((iOnBand+1)* fStep), r.bottom);
		}

		// set current color
		color = RGB((GetRValue(cRight)-GetRValue(cLeft))*((float)iOnBand)/255.0f+GetRValue(cLeft),
						(GetGValue(cRight)-GetGValue(cLeft))*((float)iOnBand)/255.0f+GetGValue(cLeft),
						(GetBValue(cRight)-GetBValue(cLeft))*((float)iOnBand)/255.0f+GetBValue(cLeft));

		// fill current band
		pDC->FillSolidRect(stepR,color);
	}
}
Tags:
HBITMAP CEMCHotButton::ConvertIconToBitmap(HICON hIcon)
{
	CClientDC clientDC(this);
	CDC dc;
	dc.CreateCompatibleDC(&clientDC);

	CRect rectButton;
	GetClientRect (&rectButton);
	CSize sizeButton = rectButton.Size();

	// bitmap size:
	int cx = 0, cy = 0;
	if (sizeButton.cy <=16) {
		// bitmap size:
		cx = 12;
		cy = 12;
	} else if (sizeButton.cy > 16) {
		// bitmap size:
		cx = 16;
		cy = 16;
	}

	CBitmap bmp;
	bmp.CreateCompatibleBitmap(&clientDC, cx, cy);
	CBitmap* pOldBmp = (CBitmap*)dc.SelectObject(&bmp);

	::DrawIconEx( dc.GetSafeHdc(), 0, 0, hIcon, cx, cy, 0, (HBRUSH)RGB(192, 192, 192), DI_NORMAL);
	dc.SelectObject( pOldBmp );
	dc.DeleteDC();

	// return the image
	return ((HBITMAP)::CopyImage((HANDLE)((HBITMAP)bmp), IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE | LR_CREATEDIBSECTION));
}

Pages

Categories

September 2008
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

Blog Stats

  • 32,541 hits