绘制角度可变的颜色渐变效果
gdi+ 是gdi(windows 早期版本提供的图形设备接口)的后续版本,是microsoft windows xp作系统即后续版本的图形显示技术。它已经集成到了.net开发环境中,所以不管你的os是什么版本,只要安装了.net框架,就有了gdi+(注意:是.net框架,而不是.net开发环境,所以win98中也可以使用gdi+)。
现在,言归正传。
在头文件中加入下面的代码:
#include <gdiplus.h>
using namespace gdiplus;
#pragma comment(lib,"gdiplus.lib")
注意:在使用gdi+函数时必须进行gdi+的初始化,使用完毕要销毁gdi+!
初始化:
gdiplusstartupinput gdiplusstartupinput;
ulong_ptr gdiplustoken;
gdiplusstartup(&gdiplustoken, &gdiplusstartupinput, null);
销毁:
ulong_ptr gdiplustoken = null;
gdiplusshutdown(gdiplustoken);
下面以给一个ctestdlg的对话框绘制背景为例子,用gdi+实现角度可变的颜色渐变效果。用到的变量:
irotation:整型,渐变色的角度
color1、color2、color3:rgb颜色值
两种颜色的比较简单,直接用gdi+提供的lineargradientbrush刷子就行了:
bool ctestdlg::onerasebkgnd(cdc* pdc)
{
cdialog::onerasebkgnd(pdc);
// 取得第一种颜色的r,g,b值
int r1 = getrvalue(color1);
int g1 = getgvalue(color1);
int b1 = getbvalue(color1);
// 取得第二种颜色的r,g,b值
int r2 = getrvalue(color2);
int g2 = getgvalue(color2);
int b2 = getbvalue(color2);
// 得到绘制区域
crect rect;
getclientrect(&rect);
// gdi+对象
gdiplus::graphics graphics(pdc->getsafehdc());
// 刷子
gdiplus::lineargradientbrush lingrbrush(gdiplus::rect(0, 0, rect.width(), rect.height()), // 绘制区域
gdiplus::color(255, r1, g1, b1), // 第一种颜色
gdiplus::color(255, r2, g2, b2), // 第二种颜色
(gdiplus::real)(90 - irotation)); // 渐变色的角度
graphics.fillrectangle(&lingrbrush, gdiplus::rect(0, 0, rect.width(), rect.height()));
return true;
}
Tag: gdi ,颜色渐变
文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。