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

📄 dibapi.cpp.svn-base

📁 股票软件源码
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
 * Parameter:
 *
 * LPSTR lpbi       - pointer to packed-DIB memory block
 *
 * Return Value:
 *
 * WORD             - number of colors in the color table
 *
 * Description:
 *
 * This function calculates the number of colors in the DIB's color table
 * by finding the bits per pixel for the DIB (whether Win3.0 or other-style
 * DIB). If bits per pixel is 1: colors=2, if 4: colors=16, if 8: colors=256,
 * if 24, no colors in color table.
 *
 ************************************************************************/


WORD DIBNumColors(LPSTR lpbi)
{
	WORD wBitCount;  // DIB bit count

	/*  If this is a Windows-style DIB, the number of colors in the
	 *  color table can be less than the number of bits per pixel
	 *  allows for (i.e. lpbi->biClrUsed can be set to some value).
	 *  If this is the case, return the appropriate value.
	 */

	if (IS_WIN30_DIB(lpbi))
	{
		DWORD dwClrUsed;

		dwClrUsed = ((LPBITMAPINFOHEADER)lpbi)->biClrUsed;
		if (dwClrUsed != 0)
			return (WORD)dwClrUsed;
	}

	/*  Calculate the number of colors in the color table based on
	 *  the number of bits per pixel for the DIB.
	 */
	if (IS_WIN30_DIB(lpbi))
		wBitCount = ((LPBITMAPINFOHEADER)lpbi)->biBitCount;
	else
		wBitCount = ((LPBITMAPCOREHEADER)lpbi)->bcBitCount;

	/* return number of colors based on bits per pixel */
	switch (wBitCount)
	{
		case 1:
			return 2;

		case 4:
			return 16;

		case 8:
			return 256;

		default:
			return 0;
	}
}


//////////////////////////////////////////////////////////////////////////
//// Clipboard support

//---------------------------------------------------------------------
//
// Function:   CopyHandle (from SDK DibView sample clipbrd.c)
//
// Purpose:    Makes a copy of the given global memory block.  Returns
//             a handle to the new memory block (NULL on error).
//
//             Routine stolen verbatim out of ShowDIB.
//
// Parms:      h == Handle to global memory to duplicate.
//
// Returns:    Handle to new global memory block.
//
//---------------------------------------------------------------------

HGLOBAL CopyHandle (HGLOBAL h)
{
	if (h == NULL)
		return NULL;

	DWORD dwLen = ::GlobalSize((HGLOBAL) h);
	HGLOBAL hCopy = ::GlobalAlloc(GHND, dwLen);

	if (hCopy != NULL)
	{
		LPVOID lpCopy = ::GlobalLock((HGLOBAL) hCopy);
		LPVOID lp     = ::GlobalLock((HGLOBAL) h);
		memcpy(lpCopy, lp, (int)dwLen);
		::GlobalUnlock(hCopy);
		::GlobalUnlock(h);
	}

	return hCopy;
}

void DrawTransparentBitmap(HDC hdc,	HBITMAP hBitmap, int xStart, int yStart, 
							int xBitmap, int yBitmap, int xWidth, int yHeight,
							COLORREF cTransparentColor)
{              
   COLORREF   cColor;
   HBITMAP    bmAndBack, bmAndObject, bmAndMem, bmSave;
   HBITMAP    bmBackOld, bmObjectOld, bmMemOld, bmSaveOld, bmTempOld;
   HDC        hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave;
   POINT      ptSize;

   if(NULL == hdc || NULL == hBitmap)
		return;
   hdcTemp = CreateCompatibleDC(hdc);
   bmTempOld = (HBITMAP)SelectObject(hdcTemp, hBitmap);   // Select the bitmap

   ptSize.x = xWidth;            // Get width of bitmap
   ptSize.y = yHeight;           // Get height of bitmap
   DPtoLP(hdcTemp, &ptSize, 1);      // Convert from device
                                     // to logical points
   hdcBack   = CreateCompatibleDC(hdc);
   hdcObject = CreateCompatibleDC(hdc);
   hdcMem    = CreateCompatibleDC(hdc);
   hdcSave   = CreateCompatibleDC(hdc);

   // Create a bitmap for each DC. DCs are required for a number of
   // GDI functions.

   // Monochrome DC

   bmAndBack   = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);

   // Monochrome DC
   bmAndObject = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);

   bmAndMem    = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
   bmSave      = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);

   // Each DC must select a bitmap object to store pixel data.
   bmBackOld   = (HBITMAP)SelectObject(hdcBack, bmAndBack);
   bmObjectOld = (HBITMAP)SelectObject(hdcObject, bmAndObject);
   bmMemOld    = (HBITMAP)SelectObject(hdcMem, bmAndMem);
   bmSaveOld   = (HBITMAP)SelectObject(hdcSave, bmSave);

   // Set proper mapping mode.
   SetMapMode(hdcTemp, GetMapMode(hdc));


   // Save the bitmap sent here, because it will be overwritten.
   BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, xBitmap, yBitmap, SRCCOPY);

   // Set the background color of the source DC to the color.
   // contained in the parts of the bitmap that should be transparent
   cColor = SetBkColor(hdcTemp, cTransparentColor);

   // Create the object mask for the bitmap by performing a BitBlt()
   // from the source bitmap to a monochrome bitmap.
   BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, xBitmap, yBitmap,
          SRCCOPY);

   // Set the background color of the source DC back to the original
   // color.
   SetBkColor(hdcTemp, cColor);


   // Create the inverse of the object mask.
   BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0,
          NOTSRCCOPY);

   // Copy the background of the main DC to the destination.
   BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdc, xStart, yStart,
          SRCCOPY);

   // Mask out the places where the bitmap will be placed.
   BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);

   // Mask out the transparent colored pixels on the bitmap.
   BitBlt(hdcTemp, xBitmap, yBitmap, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);

   // XOR the bitmap with the background on the destination DC.
   BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, xBitmap, yBitmap, SRCPAINT);


   // Copy the destination to the screen.
   BitBlt(hdc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0,
          SRCCOPY);

   // Place the original bitmap back into the bitmap sent here.
   BitBlt(hdcTemp, xBitmap, yBitmap, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);

   // Delete the memory bitmaps.
   SelectObject(hdcTemp, bmTempOld);

   DeleteObject(SelectObject(hdcBack, bmBackOld));
   DeleteObject(SelectObject(hdcObject, bmObjectOld));
   DeleteObject(SelectObject(hdcMem, bmMemOld));
   DeleteObject(SelectObject(hdcSave, bmSaveOld));

   // Delete the memory DCs.
   DeleteDC(hdcMem);
   DeleteDC(hdcBack);

   DeleteDC(hdcObject);
   DeleteDC(hdcSave);
   DeleteDC(hdcTemp);
}

void DrawBitmap(HDC hdc,	HBITMAP hBitmap, int xStart, int yStart, 
				int xBitmap,	int yBitmap, int xWidth, int yHeight,
				COLORREF cTransparentColor)
{              
   if(NULL == hdc || NULL == hBitmap)
		return;

   HDC        hdcMem = CreateCompatibleDC(hdc);
   HBITMAP hBitmapOld = (HBITMAP)SelectObject(hdcMem, hBitmap);   // Select the bitmap

   // Copy the destination to the screen.
   BitBlt(hdc, xStart, yStart, xWidth, yHeight, hdcMem, xBitmap, yBitmap,
          SRCCOPY);

   // Delete the memory DCs.
   SelectObject( hdcMem, hBitmapOld );
   DeleteDC(hdcMem);
}
/*
	Draw icon to bitmap handle, and fill bitmap use transparent color
	arguments:
		hBitmap	->	specified bitmap handle
		x		->	left of draw icon
		y		->	top of draw icon
		hicon	->	specified icon handle
		clrTransparent	->	transparent color to fill bitmap
	return:
		reserved, this version return TRUE.
	record:
		Date			Programmer		record
		Sep.17,1998		Daolin			write the function
*/
BOOL DrawIconToBitmap( HBITMAP hBitmap, int x, int y, HICON hicon , 
						 COLORREF clrTransparent )
{
	// get a device context 
	HDC		hdc	=	::GetDC( NULL );
	// create a compatible device context
	// hdcMemBmp	->	associated with the hBitmap
	// hdcMemIcon	->	associated with the icon
	HDC		hdcMemBmp	=	::CreateCompatibleDC( hdc );
	HDC		hdcMemIcon	=	::CreateCompatibleDC( hdc );

	HBITMAP	hbmp	=	::CreateCompatibleBitmap( hdc, 32, 32 );
	HBITMAP	hbmpOld	=	(HBITMAP)::SelectObject( hdcMemIcon, hbmp );
	HBRUSH	hbrTransparent	=	::CreateSolidBrush( clrTransparent );

	// fill the rectangle use transparenct color
	RECT	rect = { 0, 0, 32, 32 };
	FillRect( hdcMemIcon, &rect, hbrTransparent );

	// draw icon on hdcMemIcon
	::DrawIcon( hdcMemIcon, 0, 0, hicon );
	HBITMAP	hbmpOldB	=	(HBITMAP)::SelectObject( hdcMemBmp, hBitmap );

	/*
		Sep.23/98 16:00
		Modified by Daolin to avoid the blackness
	*/
	// set stretch mode to 
	int	nOldStretchBltMode	=	::SetStretchBltMode( hdcMemBmp, STRETCH_ANDSCANS );
	// drawing the bitmap use stretch mode
	::StretchBlt( hdcMemBmp, x, y, 16, 16, hdcMemIcon, 0, 0, 32, 32, SRCCOPY );
	::SetStretchBltMode( hdcMemBmp, nOldStretchBltMode );

	// cleanup the object
	::SelectObject( hdcMemBmp, hbmpOldB );
	::SelectObject( hdcMemIcon, hbmpOld );
	::DeleteObject( hbrTransparent );
	::DeleteObject( hbmp );
	::DeleteDC( hdcMemIcon );
	::DeleteDC( hdcMemBmp );
	::ReleaseDC( NULL, hdc );
	return	TRUE;
}

HGLOBAL LoadDIBResource( HMODULE hModule, LPCTSTR lpszRes )
{
	HRSRC		hReSrc;
	HGLOBAL		hDIB,	hRead;
	LPVOID		lpDst, lpSrc;

	hReSrc = ::FindResource( hModule, lpszRes, RT_BITMAP );
	if( hReSrc )
		hRead	=	::LoadResource( hModule, hReSrc );
	else
		return	NULL;
	if( hRead )
	{
		DWORD	dwSize	=	::SizeofResource( hModule, hReSrc );
		hDIB	=	::GlobalAlloc( GHND, dwSize );
		if( NULL == hDIB )
			return	NULL;
		lpDst	=	::GlobalLock( hDIB );
		lpSrc	=	::LockResource( hRead );
		if( NULL == lpDst || NULL == lpSrc )
		{
			::GlobalUnlock( hDIB );
			::UnlockResource( hRead );
			::GlobalFree( hDIB );
			return	NULL;
		}
		memcpy( lpDst, lpSrc, dwSize );
		::GlobalUnlock( hDIB );
		::UnlockResource( hRead );
	}
	else	
		return	NULL;
	return	hDIB;
}

HGLOBAL	LoadDIBResource( HMODULE hModule, UINT nResource )
{
	return	LoadDIBResource( hModule, MAKEINTRESOURCE( nResource) );
}

#define DIB_HEADER_MARKER   ((WORD) ('M' << 8) | 'B')
HDIB ReadDIBFile(CFile& file)
{
	BITMAPFILEHEADER bmfHeader;
	DWORD dwBitsSize;
	HDIB hDIB;
	LPSTR pDIB;

	dwBitsSize = (DWORD)file.GetLength();

	if (file.Read((LPSTR)&bmfHeader, sizeof(bmfHeader)) != sizeof(bmfHeader))
		return NULL;

	if (bmfHeader.bfType != DIB_HEADER_MARKER)
		return NULL;

	hDIB = (HDIB) ::GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, dwBitsSize);
	if (hDIB == 0)
	{
		return NULL;
	}
	pDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);

	if (file.Read(pDIB, dwBitsSize - sizeof(BITMAPFILEHEADER)) !=
		dwBitsSize - sizeof(BITMAPFILEHEADER) )
	{
		::GlobalUnlock((HGLOBAL) hDIB);
		::GlobalFree((HGLOBAL) hDIB);
		return NULL;
	}
	::GlobalUnlock((HGLOBAL) hDIB);
	return hDIB;
}

⌨️ 快捷键说明

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