//绘制颜色渐变区域的函数: DrawGradientV(垂直渐变) DrawGradientH(水平渐变)
//设置渐变参数,GRADLEVEL越小,颜色渐变越细腻,过度效果越好,但速度比较慢
#define GRADLEVEL 2
/****************************************************************************
绘制垂直颜色渐变区域
DrawGradientV( HDC hdc //绘图刷子
COLORREF co1 //顶端颜色
COLORREF co2 //低端颜色
RECT& DrawRect) //颜色渐变区域
****************************************************************************/
void DrawGradientV( HDC hdc, COLORREF co1, COLORREF co2, RECT& DrawRect )
{
int r = GetRValue( co1 );
int g = GetGValue( co1 );
int b = GetBValue( co1 );
int r2 = GetRValue( co2 );
int g2 = GetGValue( co2 );
int b2 = GetBValue( co2 );
//计算宽,高
int DrawRectWidth=DrawRect.right-DrawRect.left;
int DrawRectHeight=DrawRect.bottom-DrawRect.top;
if ( DrawRectWidth<=0)
return;
//初始化rect
RECT rect={0,0,DrawRectWidth,GRADLEVEL};
//准备GDI
HDC hMemDC=CreateCompatibleDC(hdc); //创建内存DC
HBITMAP hBitmap=::CreateCompatibleBitmap(hdc,DrawRectWidth,DrawRectHeight);//创建位图
::SelectObject(hMemDC,hBitmap); //把位图选进内存DC
HBRUSH hbr;
for(int i = DrawRectHeight; i > 0; i -= GRADLEVEL )
{
//创建刷子
hbr = CreateSolidBrush( RGB( r, g, b ) );
FillRect( hMemDC, &rect, hbr );
DeleteObject( hbr );
//改变小正方体的位置
rect.top += GRADLEVEL;
rect.bottom += GRADLEVEL;
//判断小正方体是否超界
if( rect.bottom > DrawRect.bottom )
rect.bottom = DrawRect.bottom;
//改变颜色
r += ( r2 - r + i / 2 ) / i * GRADLEVEL;
g += ( g2 - g + i / 2 ) / i * GRADLEVEL;
b += ( b2 - b + i / 2 ) / i * GRADLEVEL;
}
//内存DC映射到屏幕DC
BitBlt(hdc,DrawRect.left,DrawRect.top,DrawRectWidth,DrawRectHeight,hMemDC,0,0,SRCCOPY);
//删除
::DeleteDC(hMemDC) ;
::DeleteObject(hBitmap);
}
/****************************************************************************
绘制水平颜色渐变区域
DrawGradientV( HDC hdc //绘图刷子
COLORREF co1 //左端颜色
COLORREF co2 //右端颜色
RECT& DrawRect) //颜色渐变区域
****************************************************************************/
void DrawGradientH( HDC hdc, COLORREF co1, COLORREF co2, RECT& DrawRect )
{
int r = GetRValue( co1 );
int g = GetGValue( co1 );
int b = GetBValue( co1 );
int r2 = GetRValue( co2 );
int g2 = GetGValue( co2 );
int b2 = GetBValue( co2 );
//计算宽,高
int DrawRectWidth=DrawRect.right-DrawRect.left;
int DrawRectHeight=DrawRect.bottom-DrawRect.top;
if ( DrawRectHeight<=0)
return;
//初始化rect
RECT rect={0,0,GRADLEVEL,DrawRectHeight};
//准备GDI
HDC hMemDC=CreateCompatibleDC(hdc); //创建内存DC
HBITMAP hBitmap=::CreateCompatibleBitmap(hdc,DrawRectWidth,DrawRectHeight);//创建位图
::SelectObject(hMemDC,hBitmap); //把位图选进内存DC
HBRUSH hbr;
for(int i = DrawRectWidth; i > 0; i -= GRADLEVEL )
{
//创建刷子
hbr = CreateSolidBrush( RGB( r, g, b ) );
FillRect( hMemDC, &rect, hbr );
DeleteObject( hbr );
//改变小正方体的位置
rect.left += GRADLEVEL;
rect.right += GRADLEVEL;
//判断小正方体是否超界
if( rect.right > DrawRect.right )
rect.right = DrawRect.right;
//改变颜色
r += ( r2 - r + i / 2 ) / i * GRADLEVEL;
g += ( g2 - g + i / 2 ) / i * GRADLEVEL;
b += ( b2 - b + i / 2 ) / i * GRADLEVEL;
TRACE("i:%d r=%d,g=%d,b=%d\n",i,r,g,b);
}
//内存DC映射到屏幕DC
BitBlt(hdc,DrawRect.left,DrawRect.top,DrawRectWidth,DrawRectHeight,hMemDC,0,0,SRCCOPY);
//删除
::DeleteDC(hMemDC) ;
::DeleteObject(hBitmap);
}
void DrawShadeRect(CDC* pdc, CRect crect, COLORREF top, COLORREF bottom )
{
CRect stepR;
COLORREF color;
float fStep;
fStep = ((float)crect.Height())/255.0f;
CRect rect(crect);
for (int iOnBand = 0; iOnBand < 255; iOnBand++)
{
if ( (int)(iOnBand * fStep) == (int)((iOnBand + 1) * fStep)
&& (int)((iOnBand + 1) * fStep) == (int)((iOnBand + 2)* fStep))
{
continue;
}
stepR.SetRect(rect.left, rect.top + (int)(iOnBand * fStep),
rect.right, rect.top + (int)((iOnBand + 1)* fStep));
color = RGB((GetRValue(bottom) - GetRValue(top)) * ((float)iOnBand) / 255.0f + GetRValue(top),
(GetGValue(bottom) - GetGValue(top)) * ((float)iOnBand) / 255.0f + GetGValue(top),
(GetBValue(bottom) - GetBValue(top)) * ((float)iOnBand) / 255.0f + GetBValue(top));
pdc->FillSolidRect(stepR,color);
}
}
以上代码从网上搜集
//调用例子
void test()
{
//蓝色
COLORREF midClr = RGB(147,255,255);//RGB(187,222,222);
COLORREF bianClr = RGB(49,68,212);//RGB(80,80,180);
// //红色
// midClr = RGB(252,200,200);
// bianClr = RGB(255,80,80);
RECT rect = {180,10,240,179};
HDC hdc=::GetDC(m_hWnd);
int w = (rect.right - rect.left);
RECT rect1=rect;
rect1.right = rect1.left + w / 2;
DrawGradientH(hdc,bianClr,midClr,rect1);
RECT rect2=rect;
rect2.left = rect1.right;
DrawGradientH(hdc,midClr,bianClr,rect2);
::ReleaseDC(m_hWnd,hdc);
}