GradientCaptionBackground.cpp
上传用户:zhoushen
上传日期:2022-06-15
资源大小:84k
文件大小:3k
源码类别:
对话框与窗口
开发平台:
Visual C++
- /////////////////////////////////
- // Class for painting a windows caption background as
- // a shaded gradient.
- //
- // Used by custom caption class CCaption and its derived classes.
- //
- //
- // Author: Dave Lorde (dlorde@cix.compulink.co.uk)
- //
- // Copyright January 2000
- //
- // Portions of this file copyright Paul DiLascia
- //
- #include "stdafx.h"
- #include "GradientCaptionBackground.h"
- #include <algorithm>
- using std::_cpp_max;
- using std::_cpp_min;
- namespace
- {
- const COLORREF ColorBlack = RGB(0, 0, 0);
- const int nColorShades = 64; // this many shades in gradient
- }
- void CGradientCaptionBackground::Paint(CDC* pDC, CSize paintArea, BOOL active)
- {
- int cxCap = paintArea.cx;
- int cyCap = paintArea.cy;
- if (!active)
- {
- // Inactive caption: don't do shading, just fill w/bg color
- PaintRect(pDC, 0, 0, cxCap, cyCap, GetInactiveColor());
- }
- else
- {
- // Active caption: do shading
- //
- COLORREF clrBG = GetActiveColor(); // background color
- int r = GetRValue(clrBG); // red..
- int g = GetGValue(clrBG); // ..green
- int b = GetBValue(clrBG); // ..blue color vals
- int x = 5 * cxCap / 6; // start 5/6 of the way right
- int w = x; // width of area to shade
- int xDelta = _MAX(w / nColorShades, 1); // width of one shade band
- // Paint far right 1/6 of caption the background color
- PaintRect(pDC, x, 0, cxCap - x, cyCap, clrBG);
- // Compute new color brush for each band from x to x + xDelta.
- // Excel uses a linear algorithm from black to normal, i.e.
- //
- // color = CaptionColor * r
- //
- // where r is the ratio x/w, which ranges from 0 (x=0, left)
- // to 1 (x=w, right). This results in a mostly black title bar,
- // since we humans don't distinguish dark colors as well as light
- // ones. So instead, I use the formula
- //
- // color = CaptionColor * [1-(1-r)^2]
- //
- // which still equals black when r=0 and CaptionColor when r=1,
- // but spends more time near CaptionColor. For example, when r=0.5,
- // the multiplier is [1-(1-.5)^2] = 0.75, closer to 1 than 0.5.
- // I leave the algebra to the reader to verify that the above formula
- // is equivalent to
- //
- // color = CaptionColor - (CaptionColor*(w-x)*(w-x))/(w*w)
- //
- // The computation looks horrendous, but it's only done once each
- // time the caption changes size; thereafter BitBlt'ed to the screen.
- //
- while (x > xDelta) // paint bands right to left
- {
- x -= xDelta; // next band
- int wmx2 = (w - x) *(w - x); // w minus x squared
- int w2 = w * w; // w squared
- PaintRect(pDC, x, 0, xDelta, cyCap,
- RGB(r -(r * wmx2) / w2, g -(g * wmx2) / w2, b -(b * wmx2) / w2));
- }
- PaintRect(pDC, 0, 0, x, cyCap, ColorBlack); // whatever's left ==> black
- }
- }