📄 bcgtoolbarimages.cpp
字号:
}
if (!m_bModified && strFile == m_strUDLPath)
{
return TRUE;
}
if (!WriteDIB (strFile, DDBToDIB (m_hbmImageWell, 0)))
{
return FALSE;
}
m_bModified = FALSE;
return TRUE;
}
//************************************************************************************
static BOOL WriteDIB( LPCTSTR szFile, HANDLE hDIB)
{
BITMAPFILEHEADER hdr;
LPBITMAPINFOHEADER lpbi;
if (!hDIB)
return FALSE;
CFile file;
if( !file.Open (szFile, CFile::modeWrite | CFile::modeCreate))
{
return FALSE;
}
lpbi = (LPBITMAPINFOHEADER) hDIB;
int nColors = 1 << lpbi->biBitCount;
// Fill in the fields of the file header
hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM"
hdr.bfSize = GlobalSize (hDIB) + sizeof( hdr );
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
hdr.bfOffBits = (DWORD) (sizeof( hdr ) + lpbi->biSize +
nColors * sizeof(RGBQUAD));
// Write the file header
file.Write( &hdr, sizeof(hdr) );
// Write the DIB header and the bits
file.Write( lpbi, GlobalSize(hDIB) );
return TRUE;
}
//********************************************************************************
static HANDLE DDBToDIB (HBITMAP bitmap, DWORD dwCompression)
{
BITMAP bm;
BITMAPINFOHEADER bi;
LPBITMAPINFOHEADER lpbi;
DWORD dwLen;
HANDLE hDIB;
HANDLE handle;
HDC hDC;
HPALETTE hPal;
// The function has no arg for bitfields
if( dwCompression == BI_BITFIELDS )
return NULL;
hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);
// Get bitmap information
::GetObject(bitmap, sizeof(bm),(LPSTR)&bm);
// Initialize the bitmapinfoheader
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = (WORD) (bm.bmPlanes * bm.bmBitsPixel);
bi.biCompression = dwCompression;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
// Compute the size of the infoheader and the color table
int nColors = (1 << bi.biBitCount);
if( nColors > 256 )
nColors = 0;
dwLen = bi.biSize + nColors * sizeof(RGBQUAD);
// We need a device context to get the DIB from
hDC = ::CreateCompatibleDC (NULL);
if (hDC == NULL)
{
return FALSE;
}
HBITMAP bmp = ::CreateBitmap (1, 1, 1, nColors, NULL);
if (bmp == NULL)
{
::DeleteDC(hDC);
return NULL;
}
HBITMAP hOldBitmap = (HBITMAP)::SelectObject (hDC, bmp);
hPal = SelectPalette(hDC,hPal,FALSE);
RealizePalette(hDC);
// Allocate enough memory to hold bitmapinfoheader and color table
hDIB = GlobalAlloc(GMEM_FIXED,dwLen);
if (!hDIB){
::SelectPalette(hDC,hPal,FALSE);
::SelectObject (hDC, hOldBitmap);
::DeleteObject (bmp);
::DeleteDC(hDC);
return NULL;
}
lpbi = (LPBITMAPINFOHEADER)hDIB;
*lpbi = bi;
// Call GetDIBits with a NULL lpBits param, so the device driver
// will calculate the biSizeImage field
GetDIBits(hDC, bitmap, 0L, (DWORD)bi.biHeight,
(LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);
bi = *lpbi;
// If the driver did not fill in the biSizeImage field, then compute it
// Each scan line of the image is aligned on a DWORD (32bit) boundary
if (bi.biSizeImage == 0){
bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)
* bi.biHeight;
// If a compression scheme is used the result may infact be larger
// Increase the size to account for this.
if (dwCompression != BI_RGB)
bi.biSizeImage = (bi.biSizeImage * 3) / 2;
}
// Realloc the buffer so that it can hold all the bits
dwLen += bi.biSizeImage;
handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE);
if (handle != NULL)
hDIB = handle;
else{
GlobalFree(hDIB);
// Reselect the original palette
SelectPalette(hDC,hPal,FALSE);
::SelectObject (hDC, hOldBitmap);
::DeleteObject (bmp);
::DeleteDC(hDC);
return NULL;
}
// Get the bitmap bits
lpbi = (LPBITMAPINFOHEADER)hDIB;
// FINALLY get the DIB
BOOL bGotBits = GetDIBits( hDC, bitmap,
0L, // Start scan line
(DWORD)bi.biHeight, // # of scan lines
(LPBYTE)lpbi // address for bitmap bits
+ (bi.biSize + nColors * sizeof(RGBQUAD)),
(LPBITMAPINFO)lpbi, // address of bitmapinfo
(DWORD)DIB_RGB_COLORS); // Use RGB for color table
if( !bGotBits )
{
GlobalFree(hDIB);
SelectPalette(hDC,hPal,FALSE);
::SelectObject (hDC, hOldBitmap);
::DeleteObject (bmp);
::DeleteDC(hDC);
return NULL;
}
// Convert color table to the standard 3-d colors:
DWORD* pColorTable = (DWORD*)(((LPBYTE)lpbi) + (UINT) lpbi->biSize);
for (int iColor = 0; iColor < nColors; iColor ++)
{
pColorTable[iColor] = CBCGToolBarImages::MapFromSysColor (pColorTable[iColor]);
}
// ::GlobalFree(hDIB);
SelectPalette(hDC,hPal,FALSE);
::SelectObject (hDC, hOldBitmap);
::DeleteObject (bmp);
::DeleteDC(hDC);
return hDIB;
}
/////////////////////////////////////////////////////////////////////////////
// CBCGToolBarImages diagnostics
#ifdef _DEBUG
void CBCGToolBarImages::AssertValid() const
{
CObject::AssertValid();
ASSERT(m_hbmImageWell != NULL);
}
void CBCGToolBarImages::Dump(CDumpContext& dc) const
{
CObject::Dump(dc);
dc << "\nm_bUserImagesList = " << m_bUserImagesList;
dc << "\nm_sizeImage = " << m_sizeImage;
if (m_bUserImagesList)
{
dc << "\nm_strUDLPath = " << m_strUDLPath;
}
if (dc.GetDepth() > 0)
{
}
dc << "\n";
}
#endif
BOOL CBCGToolBarImages::CopyImageToClipboard (int iImage)
{
CBCGLocalResource locaRes;
try
{
CWindowDC dc (NULL);
//----------------------
// Create a bitmap copy:
//----------------------
CDC memDCDest;
memDCDest.CreateCompatibleDC (NULL);
CBitmap bitmapCopy;
if (!bitmapCopy.CreateCompatibleBitmap (&dc, m_sizeImage.cx, m_sizeImage.cy))
{
AfxMessageBox (IDP_BCGBARRES_CANT_COPY_BITMAP);
return FALSE;
}
CBitmap* pOldBitmapDest = memDCDest.SelectObject (&bitmapCopy);
CBCGDrawState ds;
PrepareDrawImage (ds, FALSE);
Draw (&memDCDest, 0, 0, iImage);
EndDrawImage (ds);
memDCDest.SelectObject (pOldBitmapDest);
if (!AfxGetMainWnd ()->OpenClipboard ())
{
AfxMessageBox (IDP_BCGBARRES_CANT_COPY_BITMAP);
return FALSE;
}
if (!::EmptyClipboard ())
{
AfxMessageBox (IDP_BCGBARRES_CANT_COPY_BITMAP);
::CloseClipboard ();
return FALSE;
}
HANDLE hclipData = ::SetClipboardData (CF_BITMAP, bitmapCopy.Detach ());
if (hclipData == NULL)
{
AfxMessageBox (IDP_BCGBARRES_CANT_COPY_BITMAP);
TRACE (_T("CBCGToolBar::OnBcgbarresCopyImage error. Error code = %x\n"), GetLastError ());
}
::CloseClipboard ();
return TRUE;
}
catch (...)
{
AfxMessageBox (IDP_BCGBARRES_INTERLAL_ERROR);
}
return FALSE;
}
//****************************************************************************************
void CBCGToolBarImages::Clear ()
{
//----------------
// By Guy Hachlili
//----------------
m_hInstImageWell = NULL;
AfxDeleteObject((HGDIOBJ*)&m_hbmImageWell);
m_hbmImageWell = NULL;
m_lstOrigResIds.RemoveAll();
m_mapOrigResOffsets.RemoveAll ();
m_strUDLPath.Empty();
m_bUserImagesList = FALSE;
m_iCount = 0;
m_bModified = FALSE;
}
//*************************************************************************************
void CBCGToolBarImages::TransparentBlt( HDC hdcDest, int nXDest, int nYDest, int nWidth,
int nHeight, CDC* pDcSrc, int nXSrc, int nYSrc,
COLORREF colorTransparent)
{
CDC dc, memDC, maskDC;
dc.Attach( hdcDest );
maskDC.CreateCompatibleDC(&dc);
CBitmap maskBitmap;
//add these to store return of SelectObject() calls
CBitmap* pOldMemBmp = NULL;
CBitmap* pOldMaskBmp = NULL;
memDC.CreateCompatibleDC(&dc);
CBitmap bmpImage;
bmpImage.CreateCompatibleBitmap( &dc, nWidth, nHeight );
pOldMemBmp = memDC.SelectObject( &bmpImage );
memDC.BitBlt( 0,0,nWidth, nHeight, pDcSrc, nXSrc, nYSrc, SRCCOPY );
// Create monochrome bitmap for the mask
maskBitmap.CreateBitmap( nWidth, nHeight, 1, 1, NULL );
pOldMaskBmp = maskDC.SelectObject( &maskBitmap );
memDC.SetBkColor( colorTransparent );
// Create the mask from the memory DC
maskDC.BitBlt( 0, 0, nWidth, nHeight, &memDC,
0, 0, SRCCOPY );
// Set the background in memDC to black. Using SRCPAINT with black
// and any other color results in the other color, thus making
// black the transparent color
memDC.SetBkColor(RGB(0,0,0));
memDC.SetTextColor(RGB(255,255,255));
memDC.BitBlt(0, 0, nWidth, nHeight, &maskDC, 0, 0, SRCAND);
// Set the foreground to black. See comment above.
dc.SetBkColor(RGB(255,255,255));
dc.SetTextColor(RGB(0,0,0));
dc.BitBlt(nXDest, nYDest, nWidth, nHeight, &maskDC, 0, 0, SRCAND);
// Combine the foreground with the background
dc.BitBlt(nXDest, nYDest, nWidth, nHeight, &memDC,
0, 0, SRCPAINT);
if (pOldMaskBmp)
maskDC.SelectObject( pOldMaskBmp );
if (pOldMemBmp)
memDC.SelectObject( pOldMemBmp );
dc.Detach();
}
//**********************************************************************************
BOOL CBCGToolBarImages::MapTo3dColors ()
{
if (m_hbmImageWell == NULL)
{
return FALSE;
}
//-------------------------------------------------------
// Create memory source DC and select an original bitmap:
//-------------------------------------------------------
CDC memDCSrc;
memDCSrc.CreateCompatibleDC (NULL);
HBITMAP hOldBitmapSrc = NULL;
int iBitmapWidth;
int iBitmapHeight;
//-------------------------------
// Get original bitmap attrbutes:
//-------------------------------
BITMAP bmp;
if (::GetObject (m_hbmImageWell, sizeof (BITMAP), &bmp) == 0)
{
return FALSE;
}
hOldBitmapSrc = (HBITMAP) memDCSrc.SelectObject (m_hbmImageWell);
if (hOldBitmapSrc == NULL)
{
return FALSE;
}
iBitmapWidth = bmp.bmWidth;
iBitmapHeight = bmp.bmHeight;
//----------------------------------------------------------
// Create a new bitmap compatibel with the source memory DC:
// (original bitmap SHOULD BE ALREADY SELECTED!):
//----------------------------------------------------------
HBITMAP hNewBitmap = (HBITMAP) ::CreateCompatibleBitmap (memDCSrc,
iBitmapWidth,
iBitmapHeight);
if (hNewBitmap == NULL)
{
memDCSrc.SelectObject (hOldBitmapSrc);
return -1;
}
//------------------------------
// Create memory destination DC:
//------------------------------
CDC memDCDst;
memDCDst.CreateCompatibleDC (&memDCSrc);
HBITMAP hOldBitmapDst = (HBITMAP) memDCDst.SelectObject (hNewBitmap);
if (hOldBitmapDst == NULL)
{
memDCSrc.SelectObject (hOldBitmapSrc);
::DeleteObject (hNewBitmap);
return FALSE;
}
//-----------------------------
// Copy original bitmap to new:
//-----------------------------
memDCDst.BitBlt (0, 0, iBitmapWidth, iBitmapHeight,
&memDCSrc, 0, 0, SRCCOPY);
//------------------------------------
// Change a specific colors to system:
//------------------------------------
for (int x = 0; x < iBitmapWidth; x ++)
{
for (int y = 0; y < iBitmapHeight; y ++)
{
COLORREF clrOrig = ::GetPixel (memDCDst, x, y);
COLORREF clrNew = MapToSysColor (clrOrig);
if (clrOrig != clrNew)
{
::SetPixel (memDCDst, x, y, clrNew);
}
}
}
memDCDst.SelectObject (hOldBitmapDst);
memDCSrc.SelectObject (hOldBitmapSrc);
::DeleteObject (m_hbmImageWell);
m_hbmImageWell = hNewBitmap;
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -