📄 bcgptoolbarimages.cpp
字号:
//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a part of the BCGControlBar Library
// Copyright (C) 1998-2000 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions
// of the accompanying license agreement.
//*******************************************************************************
// BCGPToolBarImages.cpp: implementation of the CBCGPToolBarImages class.
//
//////////////////////////////////////////////////////////////////////
#include "Stdafx.h"
#include "BCGGlobals.h"
#include "BCGPToolBarImages.h"
#include "BCGPToolBar.h"
#include "BCGPLocalResource.h"
#include "bcgprores.h"
#include "BCGPVisualManager.h"
#include "BCGPDrawManager.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
static BOOL WriteDIB( LPCTSTR szFile, HANDLE hDIB);
static HANDLE DDBToDIB (HBITMAP bitmap, DWORD dwCompression);
BOOL CBCGPToolBarImages::m_bDisableTrueColorAlpha = TRUE;
// globals for fast drawing (shared globals)
static HDC hDCGlyphs = NULL;
static HDC hDCMono = NULL;
/*
DIBs use RGBQUAD format:
0xbb 0xgg 0xrr 0x00
Reasonably efficient code to convert a COLORREF into an
RGBQUAD is byte-order-dependent, so we need different
code depending on the byte order we're targeting.
*/
#define RGB_TO_RGBQUAD(r,g,b) (RGB(b,g,r))
#define CLR_TO_RGBQUAD(clr) (RGB(GetBValue(clr), GetGValue(clr), GetRValue(clr)))
#define RGBQUAD_TO_CLR(clr) (RGB(GetBValue(clr), GetGValue(clr), GetRValue(clr)))
// Raster Ops
#define ROP_DSPDxax 0x00E20746L
#define ROP_PSDPxax 0x00B8074AL
/////////////////////////////////////////////////////////////////////////////
// Init / Term
void BCGPToolBarImagesCleanUp ()
{
if (hDCMono != NULL)
{
::DeleteDC (hDCMono);
hDCMono = NULL;
}
if (hDCGlyphs != NULL)
{
::DeleteDC (hDCGlyphs);
hDCGlyphs = NULL;
}
}
// a special struct that will cleanup automatically
struct _AFX_TOOLBAR_TERM
{
~_AFX_TOOLBAR_TERM()
{
BCGPToolBarImagesCleanUp ();
}
};
static const _AFX_TOOLBAR_TERM toolbarTerm;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CBCGPToolBarImages::CBCGPToolBarImages()
{
m_bModified = FALSE;
m_bReadOnly = FALSE;
m_bIsTemporary = FALSE;
m_iCount = 0;
m_hbmImageWell = NULL;
m_hbmImageLight = NULL;
m_bUserImagesList = FALSE;
// initialize the toolbar drawing engine
static BOOL bInitialized;
if (!bInitialized)
{
hDCGlyphs = CreateCompatibleDC(NULL);
// Mono DC and Bitmap for disabled image
hDCMono = ::CreateCompatibleDC(NULL);
if (hDCGlyphs == NULL || hDCMono == NULL)
AfxThrowResourceException();
bInitialized = TRUE;
}
m_clrTransparent = (COLORREF) -1;
// UISG standard sizes
m_sizeImage = CSize (16, 15);
m_sizeImageDest = CSize (0, 0);
m_bStretch = FALSE;
m_pBmpOriginal = NULL;
m_bFadeInactive = FALSE;
m_nBitsPerPixel = 0;
OnSysColorChange ();
}
//*******************************************************************************
CBCGPToolBarImages::~CBCGPToolBarImages()
{
ASSERT (m_dcMem.GetSafeHdc () == NULL);
ASSERT (m_bmpMem.GetSafeHandle () == NULL);
ASSERT (m_pBmpOriginal == NULL);
if (!m_bIsTemporary)
{
AfxDeleteObject((HGDIOBJ*)&m_hbmImageWell);
AfxDeleteObject((HGDIOBJ*)&m_hbmImageLight);
}
}
//*******************************************************************************
BOOL CBCGPToolBarImages::Load (UINT uiResID, HINSTANCE hinstRes, BOOL bAdd)
{
if (m_bIsTemporary)
{
ASSERT (FALSE);
return FALSE;
}
LPCTSTR lpszResourceName = MAKEINTRESOURCE (uiResID);
ASSERT(lpszResourceName != NULL);
if (!bAdd)
{
AfxDeleteObject((HGDIOBJ*)&m_hbmImageWell); // get rid of old one
m_lstOrigResIds.RemoveAll ();
m_lstOrigResInstances.RemoveAll ();
m_mapOrigResOffsets.RemoveAll ();
}
else if (m_lstOrigResIds.Find (uiResID) != NULL) // Already loaded, do nothing
{
return TRUE;
}
if (hinstRes == NULL)
{
hinstRes = AfxFindResourceHandle (lpszResourceName, RT_BITMAP);
}
HBITMAP hbmp = (HBITMAP) ::LoadImage (
hinstRes,
lpszResourceName,
IMAGE_BITMAP,
0, 0,
LR_CREATEDIBSECTION | LR_LOADMAP3DCOLORS);
if (hbmp == NULL)
{
TRACE(_T("Can't load bitmap: %x. GetLastError() = %x\n"),
uiResID,
GetLastError ());
return FALSE;
}
BITMAP bmp;
if (::GetObject (hbmp, sizeof (BITMAP), &bmp) == 0)
{
ASSERT (FALSE);
::DeleteObject (hbmp);
return FALSE;
}
if (bAdd)
{
m_mapOrigResOffsets.SetAt (uiResID, m_iCount);
AddImage (hbmp);
m_lstOrigResIds.AddTail (uiResID);
m_lstOrigResInstances.AddTail (hinstRes);
::DeleteObject (hbmp);
}
else
{
m_hbmImageWell = hbmp;
}
m_nBitsPerPixel = bmp.bmBitsPixel;
if (m_nBitsPerPixel > 8)
{
//------------------------------------------------
// LR_LOADMAP3DCOLORS don't support > 8bpp images,
// we should convert it now:
//------------------------------------------------
MapTo3dColors (FALSE);
}
UpdateCount ();
UpdateLightImage ();
return TRUE;
}
//*******************************************************************************
BOOL CBCGPToolBarImages::Load (LPCTSTR lpszBmpFileName)
{
if (m_bIsTemporary)
{
ASSERT (FALSE);
return FALSE;
}
ASSERT (lpszBmpFileName != NULL);
AfxDeleteObject((HGDIOBJ*)&m_hbmImageWell); // get rid of old one
CString strPath = lpszBmpFileName;
//-----------------------------------------------------------------------
// If the image path is not defined, try to open it in the EXE directory:
//-----------------------------------------------------------------------
if (strPath.Find (_T("\\")) == -1 &&
strPath.Find (_T("/")) == -1 &&
strPath.Find (_T(":")) == -1)
{
TCHAR lpszFilePath [_MAX_PATH];
if (::GetModuleFileName (NULL, lpszFilePath, _MAX_PATH) > 0)
{
TCHAR path_buffer[_MAX_PATH];
TCHAR drive[_MAX_DRIVE];
TCHAR dir[_MAX_DIR];
TCHAR fname[_MAX_FNAME];
TCHAR ext[_MAX_EXT];
_tsplitpath (lpszFilePath, drive, dir, NULL, NULL);
_tsplitpath (lpszBmpFileName, NULL, NULL, fname, ext);
_tmakepath (path_buffer, drive, dir, fname, ext);
strPath = path_buffer;
}
}
//--------------------------------
// Load images from the disk file:
//--------------------------------
m_hbmImageWell = (HBITMAP) ::LoadImage (
AfxGetInstanceHandle (),
strPath,
IMAGE_BITMAP,
0, 0,
LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_LOADMAP3DCOLORS);
if (m_hbmImageWell == NULL)
{
TRACE(_T("Can't load bitmap: %s. GetLastError() = %x\r\n"),
lpszBmpFileName,
GetLastError ());
return FALSE;
}
BITMAP bmp;
if (::GetObject (m_hbmImageWell, sizeof (BITMAP), &bmp) == 0)
{
ASSERT (FALSE);
::DeleteObject (m_hbmImageWell);
m_hbmImageWell = NULL;
return FALSE;
}
m_bUserImagesList = TRUE;
m_strUDLPath = strPath;
if (::GetFileAttributes (strPath) & FILE_ATTRIBUTE_READONLY)
{
m_bReadOnly = TRUE;
}
m_nBitsPerPixel = bmp.bmBitsPixel;
if (m_nBitsPerPixel > 8)
{
//------------------------------------------------
// LR_LOADMAP3DCOLORS don't support > 8bpp images,
// we should convert it now:
//------------------------------------------------
MapTo3dColors (FALSE);
}
UpdateCount ();
UpdateLightImage ();
return TRUE;
}
//*******************************************************************************
BOOL CBCGPToolBarImages::PrepareDrawImage (CBCGPDrawState& ds,
CSize sizeImageDest,
BOOL bFadeInactive)
{
if (m_hbmImageWell == NULL)
{
return FALSE;
}
m_bStretch = FALSE;
if (m_hbmImageLight == NULL ||
m_nBitsPerPixel > 4 || m_nBitsPerPixel == 0)
{
// Down't fade 256+ or unknown bitmaps
bFadeInactive = FALSE;
}
m_bFadeInactive = bFadeInactive;
ASSERT(m_hbmImageWell != NULL);
ASSERT (m_dcMem.GetSafeHdc () == NULL);
ASSERT (m_bmpMem.GetSafeHandle () == NULL);
ASSERT (m_pBmpOriginal == NULL);
// We need to kick-start the bitmap selection process.
ds.hbmOldGlyphs = (HBITMAP)SelectObject (hDCGlyphs,
bFadeInactive ? m_hbmImageLight : m_hbmImageWell);
ds.hbmMono = CreateBitmap (m_sizeImage.cx + 2, m_sizeImage.cy + 2,
1, 1, NULL);
ds.hbmMonoOld = (HBITMAP)SelectObject(hDCMono, ds.hbmMono);
if (ds.hbmOldGlyphs == NULL || ds.hbmMono == NULL || ds.hbmMonoOld == NULL)
{
TRACE0("Error: can't draw toolbar.\r\n");
AfxDeleteObject((HGDIOBJ*)&ds.hbmMono);
return FALSE;
}
if (sizeImageDest.cx <= 0 || sizeImageDest.cy <= 0)
{
m_sizeImageDest = m_sizeImage;
}
else
{
m_sizeImageDest = sizeImageDest;
}
if (m_sizeImageDest != m_sizeImage || m_clrTransparent != (COLORREF) -1)
{
CWindowDC dc (NULL);
m_bStretch = (m_sizeImageDest != m_sizeImage);
m_dcMem.CreateCompatibleDC (NULL); // Assume display!
m_bmpMem.CreateCompatibleBitmap (&dc, m_sizeImage.cx + 2, m_sizeImage.cy + 2);
m_pBmpOriginal = m_dcMem.SelectObject (&m_bmpMem);
ASSERT (m_pBmpOriginal != NULL);
}
return TRUE;
}
//*******************************************************************************
void CBCGPToolBarImages::EndDrawImage (CBCGPDrawState& ds)
{
SelectObject(hDCMono, ds.hbmMonoOld);
AfxDeleteObject((HGDIOBJ*)&ds.hbmMono);
SelectObject(hDCGlyphs, ds.hbmOldGlyphs);
m_sizeImageDest = CSize (0, 0);
if (m_bStretch || m_clrTransparent != (COLORREF) -1)
{
ASSERT (m_pBmpOriginal != NULL);
m_dcMem.SelectObject (m_pBmpOriginal);
m_pBmpOriginal = NULL;
::DeleteObject (m_bmpMem.Detach ());
::DeleteDC (m_dcMem.Detach ());
}
m_bFadeInactive = FALSE;
}
//*******************************************************************************
void CBCGPToolBarImages::CreateMask(int iImage, BOOL bHilite, BOOL bHiliteShadow)
{
// initalize whole area with 0's
PatBlt(hDCMono, 0, 0, m_sizeImage.cx + 2, m_sizeImage.cy + 2, WHITENESS);
// create mask based on color bitmap
// convert this to 1's
SetBkColor (hDCGlyphs,
m_clrTransparent != -1 ? m_clrTransparent : globalData.clrBtnFace);
::BitBlt(hDCMono, 0, 0, m_sizeImage.cx, m_sizeImage.cy,
hDCGlyphs, iImage * m_sizeImage.cx, 0, SRCCOPY);
if (bHilite)
{
// convert this to 1's
SetBkColor(hDCGlyphs, globalData.clrBtnHilite);
// OR in the new 1's
::BitBlt(hDCMono, 0, 0, m_sizeImage.cx, m_sizeImage.cy,
hDCGlyphs, iImage * m_sizeImage.cx, 0, SRCPAINT);
if (bHiliteShadow)
{
::BitBlt(hDCMono, 1, 1, m_sizeImage.cx + 1, m_sizeImage.cy + 1,
hDCMono, 0, 0, SRCAND);
}
}
}
//********************************************************************************
BOOL CBCGPToolBarImages::Draw (CDC* pDCDest,
int xDest, int yDest,
int iImage,
BOOL bHilite,
BOOL bDisabled,
BOOL bIndeterminate,
BOOL bShadow,
BOOL bInactive)
{
if (iImage < 0 || iImage >= m_iCount)
{
return FALSE;
}
if (bShadow && globalData.m_nBitsPerPixel <= 8)
{
return TRUE;
}
if (m_bStretch)
{
bHilite = FALSE;
bIndeterminate = FALSE;
}
HBITMAP hBmpOriginal = NULL;
if (!bInactive && m_bFadeInactive)
{
hBmpOriginal = (HBITMAP) SelectObject (hDCGlyphs, m_hbmImageWell);
}
BOOL bIsTransparent = (m_clrTransparent != (COLORREF) -1);
CDC* pDC = m_bStretch || bIsTransparent ? &m_dcMem : pDCDest;
ASSERT_VALID(pDC);
int x = m_bStretch || bIsTransparent ? 0 : xDest;
int y = m_bStretch || bIsTransparent ? 0 : yDest;
if (m_bStretch || bIsTransparent)
{
CRect rectImage (CPoint (0, 0), m_sizeImage);
if (bIsTransparent && m_clrTransparent != globalData.clrBtnFace)
{
CBrush brBackgr (m_clrTransparent);
pDC->FillRect (rectImage, &brBackgr);
}
else
{
pDC->FillRect (rectImage, &globalData.brBtnFace);
}
}
BOOL bDisabledTrueColor = FALSE;
if (bDisabled && m_nBitsPerPixel == 24)
{
bDisabled = FALSE;
bDisabledTrueColor = TRUE;
}
if (!bHilite && !bDisabled && !bShadow)
{
//----------------------
// normal image version:
//----------------------
::BitBlt(pDC->m_hDC, x, y,
m_sizeImage.cx, m_sizeImage.cy,
hDCGlyphs, iImage * m_sizeImage.cx, 0, SRCCOPY);
if (bDisabledTrueColor)
{
CBCGPDrawManager dm (*pDC);
dm.GrayRect (CRect (0, 0, m_sizeImage.cx + 2, m_sizeImage.cy + 2), -1, m_clrTransparent);
}
}
else
{
if (bDisabled || bIndeterminate || bShadow)
{
// disabled or indeterminate version
CreateMask(iImage, TRUE, FALSE);
pDC->SetTextColor(bShadow ? m_clrImageShadow : 0L); // 0's in mono -> 0 (for ROP)
pDC->SetBkColor((COLORREF)0x00FFFFFFL); // 1's in mono -> 1
if (bDisabled && CBCGPVisualManager::GetInstance ()->IsEmbossDisabledImage ())
{
// disabled - draw the hilighted shadow
HGDIOBJ hbrOld = pDC->SelectObject (globalData.hbrBtnHilite);
if (hbrOld != NULL)
{
// draw hilight color where we have 0's in the mask
::BitBlt(pDC->m_hDC, x + 1, y + 1,
m_sizeImage.cx + 2, m_sizeImage.cy + 2,
hDCMono, 0, 0, ROP_PSDPxax);
pDC->SelectObject(hbrOld);
}
}
//BLOCK: always draw the shadow
{
HGDIOBJ hbrOld = pDC->SelectObject(globalData.hbrBtnShadow);
if (hbrOld != NULL)
{
// draw the shadow color where we have 0's in the mask
::BitBlt(pDC->m_hDC,
x, y,
m_sizeImage.cx + 2, m_sizeImage.cy + 2,
hDCMono, 0, 0, ROP_PSDPxax);
pDC->SelectObject(hbrOld);
}
}
}
// if it is checked do the dither brush avoiding the glyph
if (bHilite || bIndeterminate)
{
CBrush* pBrOld = pDC->SelectObject(&globalData.brLight);
if (pBrOld != NULL)
{
CreateMask(iImage, !bIndeterminate, bDisabled);
pDC->SetTextColor(0L); // 0 -> 0
pDC->SetBkColor((COLORREF)0x00FFFFFFL); // 1 -> 1
// only draw the dither brush where the mask is 1's
::BitBlt(pDC->m_hDC, x, y,
m_sizeImage.cx, m_sizeImage.cy,
hDCMono, 0, 0, ROP_DSPDxax);
pDC->SelectObject(pBrOld);
}
}
}
if (m_bStretch)
{
TransparentBlt (pDCDest->GetSafeHdc (), xDest, yDest,
m_sizeImage.cx, m_sizeImage.cy,
pDC, 0, 0,
bIsTransparent ? m_clrTransparent : globalData.clrBtnFace,
m_sizeImageDest.cx, m_sizeImageDest.cy);
}
else if (bIsTransparent)
{
TransparentBlt (pDCDest->GetSafeHdc (), xDest, yDest,
m_sizeImage.cx, m_sizeImage.cy,
pDC, 0, 0, m_clrTransparent);
}
if (hBmpOriginal != NULL)
{
SelectObject (hDCGlyphs, hBmpOriginal);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -