VC 绘制颜色渐变区域
更新日期:2010-12-23
测试环境:VC6.0+WinXP
图片预览:
━━━━━━━━━━━━━━━━━━━━━━━━
绘制颜色渐变区域的函数: DrawGradientV(垂直渐变) DrawGradientH(水平渐变)
//设置渐变参数,GRADLEVEL越小,颜色渐变越细腻,过度效果越好,但速度比较慢 #define GRADLEVEL 1 /**************************************************************************** 绘制垂直颜色渐变区域 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; } //内存DC映射到屏幕DC BitBlt(hdc,DrawRect.left,DrawRect.top,DrawRectWidth,DrawRectHeight,hMemDC,0,0,SRCCOPY); //删除 ::DeleteDC(hMemDC) ; ::DeleteObject(hBitmap); } |
调用示例:
void CDemoDlg::OnButton1() { HDC hdc=::GetDC(m_hWnd); RECT rect={10,10,179,179}; DrawGradientV(hdc,RGB(49,68,212),RGB(147,255,255),rect); ::ReleaseDC(m_hWnd,hdc); } void CDemoDlg::OnButton2() { HDC hdc=::GetDC(m_hWnd); RECT rect={210,10,379,179}; DrawGradientH(hdc,RGB(49,68,212),RGB(147,255,255),rect); ::ReleaseDC(m_hWnd,hdc); } |