📄 bcgpdrawmanager.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.
//*******************************************************************************
// BCGDrawManager.cpp: implementation of the CBCGPDrawManager class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <math.h>
#include "BCGCBPro.h"
#include "BCGGlobals.h"
#include "BCGPDrawManager.h"
#include "BCGPToolBarImages.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
const double PI = 3.1415926;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CBCGPDrawManager::CBCGPDrawManager(CDC& m_dc) :
m_dc (m_dc)
{
}
//*************************************************************************************
CBCGPDrawManager::~CBCGPDrawManager()
{
}
//*************************************************************************************
BOOL CBCGPDrawManager::HighlightRect (CRect rect, int nPercentage, COLORREF clrTransparent)
{
if (nPercentage == 100)
{
// Nothing to do
return TRUE;
}
if (rect.Height () <= 0 || rect.Width () <= 0)
{
return TRUE;
}
if (globalData.m_nBitsPerPixel <= 8)
{
CBCGPToolBarImages::FillDitheredRect (&m_dc, rect);
return TRUE;
}
int cx = rect.Width ();
int cy = rect.Height ();
//--------------------------------------------
// Copy screen content into the memory bitmap:
//--------------------------------------------
CDC dcMem;
if (!dcMem.CreateCompatibleDC (&m_dc))
{
ASSERT (FALSE);
return FALSE;
}
//--------------------------------------------
// Gets the whole menu and changes the shadow.
//--------------------------------------------
CBitmap bmpMem;
if (!bmpMem.CreateCompatibleBitmap (&m_dc, cx, cy))
{
ASSERT (FALSE);
return FALSE;
}
CBitmap* pOldBmp = dcMem.SelectObject(&bmpMem);
ASSERT (pOldBmp != NULL);
BITMAPINFO bi;
// Fill in the BITMAPINFOHEADER
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biWidth = cx;
bi.bmiHeader.biHeight = cy;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biCompression = BI_RGB;
bi.bmiHeader.biSizeImage = cx * cy;
bi.bmiHeader.biXPelsPerMeter = 0;
bi.bmiHeader.biYPelsPerMeter = 0;
bi.bmiHeader.biClrUsed = 0;
bi.bmiHeader.biClrImportant = 0;
COLORREF* pBits = NULL;
HBITMAP hmbpDib = CreateDIBSection (
dcMem.m_hDC, &bi, DIB_RGB_COLORS, (void **)&pBits,
NULL, NULL);
if (hmbpDib == NULL || pBits == NULL)
{
ASSERT (FALSE);
return FALSE;
}
dcMem.SelectObject (hmbpDib);
dcMem.BitBlt (0, 0, cx, cy, &m_dc, rect.left, rect.top, SRCCOPY);
if (clrTransparent != -1)
{
clrTransparent = RGB (GetBValue(clrTransparent), GetGValue(clrTransparent), GetRValue(clrTransparent));
}
for (int pixel = 0; pixel < cx * cy; pixel++, *pBits++)
{
COLORREF color = (COLORREF) *pBits;
if (color != clrTransparent)
{
if (nPercentage == -1)
{
//-------------------
// By Maarten Hoeben:
//-------------------
*pBits = RGB (
min (255, GetRValue (color) + ((GetBValue (globalData.clrBtnHilite) -
GetRValue (color)) / 2)),
min (255, GetGValue (color) + ((GetGValue (globalData.clrBtnHilite) -
GetGValue (color)) / 2)),
min (255, GetBValue(color) + ((GetRValue (globalData.clrBtnHilite) -
GetBValue (color)) / 2)));
}
else
{
*pBits = PixelAlpha (color, .01 * nPercentage, .01 * nPercentage, .01 * nPercentage);
}
}
}
//-------------------------------------------
// Copy highligted bitmap back to the screen:
//-------------------------------------------
m_dc.BitBlt (rect.left, rect.top, cx, cy, &dcMem, 0, 0, SRCCOPY);
dcMem.SelectObject (pOldBmp);
DeleteObject (hmbpDib);
return TRUE;
}
//*************************************************************************************
BOOL CBCGPDrawManager::GrayRect (CRect rect, int nPercentage, COLORREF clrTransparent)
{
if (rect.Height () <= 0 || rect.Width () <= 0)
{
return TRUE;
}
if (globalData.m_nBitsPerPixel <= 8)
{
CBCGPToolBarImages::FillDitheredRect (&m_dc, rect);
return TRUE;
}
int cx = rect.Width ();
int cy = rect.Height ();
//--------------------------------------------
// Copy screen content into the memory bitmap:
//--------------------------------------------
CDC dcMem;
if (!dcMem.CreateCompatibleDC (&m_dc))
{
ASSERT (FALSE);
return FALSE;
}
//--------------------------------------------
// Gets the whole menu and changes the shadow.
//--------------------------------------------
CBitmap bmpMem;
if (!bmpMem.CreateCompatibleBitmap (&m_dc, cx, cy))
{
ASSERT (FALSE);
return FALSE;
}
CBitmap* pOldBmp = dcMem.SelectObject(&bmpMem);
ASSERT (pOldBmp != NULL);
LPBITMAPINFO lpbi;
// Fill in the BITMAPINFOHEADER
lpbi = (LPBITMAPINFO) new BYTE[sizeof(BITMAPINFOHEADER)];
lpbi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
lpbi->bmiHeader.biWidth = cx;
lpbi->bmiHeader.biHeight = cy;
lpbi->bmiHeader.biPlanes = 1;
lpbi->bmiHeader.biBitCount = 32;
lpbi->bmiHeader.biCompression = BI_RGB;
lpbi->bmiHeader.biSizeImage = cx * cy;
lpbi->bmiHeader.biXPelsPerMeter = 0;
lpbi->bmiHeader.biYPelsPerMeter = 0;
lpbi->bmiHeader.biClrUsed = 0;
lpbi->bmiHeader.biClrImportant = 0;
COLORREF* pBits = NULL;
HBITMAP hmbpDib = CreateDIBSection (
dcMem.m_hDC, lpbi, DIB_RGB_COLORS, (void **)&pBits,
NULL, NULL);
if (hmbpDib == NULL || pBits == NULL)
{
delete lpbi;
ASSERT (FALSE);
return FALSE;
}
dcMem.SelectObject (hmbpDib);
dcMem.BitBlt (0, 0, cx, cy, &m_dc, rect.left, rect.top, SRCCOPY);
if (clrTransparent != -1)
{
clrTransparent = RGB (GetBValue(clrTransparent), GetGValue(clrTransparent), GetRValue(clrTransparent));
}
for (int pixel = 0; pixel < cx * cy; pixel++, *pBits++)
{
COLORREF color = (COLORREF) *pBits;
if (color != clrTransparent)
{
double H,S,L;
RGBtoHSL(color, &H, &S, &L);
color = HLStoRGB_ONE(H,L,0);
if (nPercentage == -1)
{
//-------------------
// By Maarten Hoeben:
//-------------------
*pBits = RGB (
min (255, GetRValue (color) + ((GetBValue (globalData.clrBtnHilite) -
GetRValue (color)) / 2)),
min (255, GetGValue (color) + ((GetGValue (globalData.clrBtnHilite) -
GetGValue (color)) / 2)),
min (255, GetBValue(color) + ((GetRValue (globalData.clrBtnHilite) -
GetBValue (color)) / 2)));
}
else
{
*pBits = PixelAlpha (color, .01 * nPercentage, .01 * nPercentage, .01 * nPercentage);
}
}
}
//-------------------------------------------
// Copy highligted bitmap back to the screen:
//-------------------------------------------
m_dc.BitBlt (rect.left, rect.top, cx, cy, &dcMem, 0, 0, SRCCOPY);
dcMem.SelectObject (pOldBmp);
DeleteObject (hmbpDib);
delete lpbi;
return TRUE;
}
//*************************************************************************************
void CBCGPDrawManager::FillGradient (CRect rect,
COLORREF colorStart, COLORREF colorFinish,
BOOL bHorz/* = TRUE*/)
// Copyright (C) 1998, 1999 by Cristi Posea
// All rights reserved
{
if (colorStart == colorFinish)
{
CBrush br (colorStart);
m_dc.FillRect (rect, &br);
return;
}
// this will make 2^6 = 64 fountain steps
int nShift = 6;
int nSteps = 1 << nShift;
for (int i = 0; i < nSteps; i++)
{
// do a little alpha blending
BYTE bR = (BYTE) ((GetRValue(colorStart) * (nSteps - i) +
GetRValue(colorFinish) * i) >> nShift);
BYTE bG = (BYTE) ((GetGValue(colorStart) * (nSteps - i) +
GetGValue(colorFinish) * i) >> nShift);
BYTE bB = (BYTE) ((GetBValue(colorStart) * (nSteps - i) +
GetBValue(colorFinish) * i) >> nShift);
CBrush br (RGB(bR, bG, bB));
// then paint with the resulting color
CRect r2 = rect;
if (bHorz)
{
r2.bottom = rect.bottom -
((i * rect.Height()) >> nShift);
r2.top = rect.bottom -
(((i + 1) * rect.Height()) >> nShift);
if (r2.Height() > 0)
m_dc.FillRect(r2, &br);
}
else
{
r2.left = rect.left +
((i * rect.Width()) >> nShift);
r2.right = rect.left +
(((i + 1) * rect.Width()) >> nShift);
if (r2.Width() > 0)
m_dc.FillRect(r2, &br);
}
}
}
//************************************************************************************
void CBCGPDrawManager::FillGradient2 (CRect rect, COLORREF colorStart, COLORREF colorFinish,
int nAngle)
{
if (colorStart == colorFinish)
{
CBrush br (colorStart);
m_dc.FillRect (rect, &br);
return;
}
//----------------------
// Process simple cases:
//----------------------
switch (nAngle)
{
case 0:
case 360:
FillGradient (rect, colorStart, colorFinish, FALSE);
return;
case 90:
FillGradient (rect, colorStart, colorFinish, TRUE);
return;
case 180:
FillGradient (rect, colorFinish, colorStart, FALSE);
return;
case 270:
FillGradient (rect, colorFinish, colorStart, TRUE);
return;
}
//--------------------------------------------
// Copy screen content into the memory bitmap:
//--------------------------------------------
CDC dcMem;
if (!dcMem.CreateCompatibleDC (&m_dc))
{
ASSERT (FALSE);
return;
}
CBitmap bmpMem;
if (!bmpMem.CreateCompatibleBitmap (&m_dc, rect.Width (), rect.Height ()))
{
ASSERT (FALSE);
return;
}
CBitmap* pOldBmp = dcMem.SelectObject (&bmpMem);
ASSERT (pOldBmp != NULL);
CPen* pOldPen = (CPen*) dcMem.SelectStockObject (NULL_PEN);
int nShift = 6;
int nSteps = 1 << nShift;
const double fAngle = PI * (nAngle + 180) / 180;
const int nOffset = (int) (cos (fAngle) * rect.Height ());
const int nTotalWidth = rect.Width () + abs (nOffset);
const int xStart = nOffset > 0 ? - nOffset : 0;
for (int i = 0; i < nSteps; i++)
{
// do a little alpha blending
BYTE bR = (BYTE) ((GetRValue(colorStart) * (nSteps - i) +
GetRValue(colorFinish) * i) >> nShift);
BYTE bG = (BYTE) ((GetGValue(colorStart) * (nSteps - i) +
GetGValue(colorFinish) * i) >> nShift);
BYTE bB = (BYTE) ((GetBValue(colorStart) * (nSteps - i) +
GetBValue(colorFinish) * i) >> nShift);
CBrush br (RGB (bR, bG, bB));
int x11 = xStart + ((i * nTotalWidth) >> nShift);
int x12 = xStart + (((i + 1) * nTotalWidth) >> nShift);
if (x11 == x12)
{
continue;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -