`
soboer
  • 浏览: 1318662 次
文章分类
社区版块
存档分类
最新评论

在VC中用GDI+绘制角度可变的颜色渐变效果

 
阅读更多

GDI+ 是GDI(Windows 早期版本提供的图形设备接口)的后续版本,是Microsoft Windows XP作系统即后续版本的图形显示技术。它已经集成到了.Net开发环境中,所以不管你的OS是什么版本,只要安装了.Net框架,就有了GDI+(注意:是.Net框架,而不是.Net开发环境,所以Win98中也可以使用GDI+)。
现在,言归正传。
在头文件中加入下面的代码:

#include<GdiPlus.h>
usingnamespaceGdiplus;
#pragmacomment(lib,"gdiplus.lib")

注意:在使用GDI+函数时必须进行GDI+的初始化,使用完毕要销毁GDI+!
初始化:

GdiplusStartupInputgdiplusStartupInput;
ULONG_PTRgdiplusToken;
GdiplusStartup(
&gdiplusToken,&gdiplusStartupInput,NULL);

销毁:

ULONG_PTRgdiplusToken=NULL;
GdiplusShutdown(gdiplusToken);

下面以给一个CTestDlg的对话框绘制背景为例子,用GDI+实现角度可变的颜色渐变效果。用到的变量:
iRotation:整型,渐变色的角度
Color1、Color2、Color3:RGB颜色值
两种颜色的比较简单,直接用GDI+提供的LinearGradientBrush刷子就行了:

BOOLCTestDlg::OnEraseBkgnd(CDC*pDC)
{
CDialog::OnEraseBkgnd(pDC);

//取得第一种颜色的R,G,B值
intr1=GetRValue(Color1);
intg1=GetGValue(Color1);
intb1=GetBValue(Color1);

//取得第二种颜色的R,G,B值
intr2=GetRValue(Color2);
intg2=GetGValue(Color2);
intb2=GetBValue(Color2);

//得到绘制区域
CRectrect;
GetClientRect(
&rect);

//GDI+对象
Gdiplus::Graphicsgraphics(pDC->GetSafeHdc());

//刷子
Gdiplus::LinearGradientBrushlinGrBrush(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()));

returnTRUE;
}

三种颜色比较复杂,也是用GDI+提供的LinearGradientBrush刷子,不过需要计算绘制区域的对角线长度,并按照对角线平分为三等分。

具体的看以下代码:

BOOLCTestDlg::OnEraseBkgnd(CDC*pDC)
{
CDialog::OnEraseBkgnd(pDC);

//取得第一种颜色的R,G,B值
intr1=GetRValue(Color1);
intg1=GetGValue(Color1);
intb1=GetBValue(Color1);

//取得第二种颜色的R,G,B值
intr2=GetRValue(Color2);
intg2=GetGValue(Color2);
intb2=GetBValue(Color2);

//取得第三种颜色的R,G,B值
intr3=GetRValue(Color3);
intg3=GetGValue(Color3);
intb3=GetBValue(Color3);

//得到绘制区域
CRectrect;
GetClientRect(
&rect);

//计算对角线长度
intiHeight=rect.Height();
intiWidth=rect.Width();
doubledwDiagonal=sqrt((double)(iWidth*iWidth+iHeight*iHeight));

//三块绘制区域
RectrectDraw(0,0,(INT)dwDiagonal,(INT)dwDiagonal);
RectrectDraw1(
0,0,(INT)dwDiagonal,((INT)dwDiagonal)/2);
RectrectDraw2(
0,((INT)dwDiagonal)/2,(INT)dwDiagonal,((INT)dwDiagonal)/2);

//GDI+对象
Graphicsgraphics(pDC->GetSafeHdc());
Gdiplus::Bitmapbmp(rectDraw.Width,rectDraw.Height);
GraphicsgrTmp(
&bmp);

//用刷子填充区域
Gdiplus::LinearGradientBrushlinGrBrush(rectDraw1,Color(r1,g1,b1),Color(r2,g2,b2),90);
grTmp.FillRectangle(
&linGrBrush,rectDraw1);
Gdiplus::LinearGradientBrushlinGrBrush1(rectDraw2,Color(r2,g2,b2),Color(r3,g3,b3),
90);
grTmp.FillRectangle(
&linGrBrush1,rectDraw2);

//计算
dwDiagonal*=0.5;
doubledwAngle=iRotation*3.1415926/180.0;
doubledwCosAngle=cos(dwAngle);
doubledwSinAngle=sin(dwAngle);
doubledwBeta=atan2((double)iHeight,(double)iWidth);
doubledwDistance=dwDiagonal*sin(fabs(dwAngle)+dwBeta);
doublexc=0.5*iWidth-dwDistance*dwSinAngle;
doubleyc=0.5*iHeight-dwDistance*dwCosAngle;
doublexc1=0.5*iWidth+dwDistance*dwSinAngle;
doubleyc1=0.5*iHeight+dwDistance*dwCosAngle;
doubledx=dwDiagonal*dwCosAngle;
doubledy=-dwDiagonal*dwSinAngle;

//绘制
PointptDestinationPoints[3];
ptDestinationPoints[
0].X=(INT)(xc-dx);
ptDestinationPoints[
0].Y=(INT)(yc-dy);
ptDestinationPoints[
1].X=(INT)(xc+dx);
ptDestinationPoints[
1].Y=(INT)(yc+dy);
ptDestinationPoints[
2].X=(INT)(xc1-dx);
ptDestinationPoints[
2].Y=(INT)(yc1-dy);
graphics.DrawImage(
&bmp,ptDestinationPoints,3);

returnTRUE;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics