当前位置:首页 > GDI+ for VCL基础
还可以通过指定的矩形、起始颜色、结束颜色和角度,建立TGpLinearGradientBrush对象,将上面的代码中TLinea
rGradientMode构造方法的参数lmBackwardDiagonal替换为90,渐变方向是从上到下进行的,相当于TLinearGradientMode成员lmVertical,效果截图见图三。
利用上面建立的渐变刷填充的图案都是重复的,要改变这种情况,必须使渐变刷的指定矩形等于我们要绘的图形矩形,下
面代码没有重复填充,而且起始色和结束色都使用kcRed,但是起始色的为半透明,所以渐变填充为半透明红色到不透明红色的效果,见图四:
// Delphi 2007 var
brush: TGpLinearGradientBrush; begin
brush := TGpLinearGradientBrush.Create(GpRect(0, 0, 128, 128), ARGB(32, kcRed), kcRed);
// 假定TGpGraphics对象g已经建立 g.FillRectangle(brush, 0, 0, 128, 128); brush.Free; end;
// C++ Builder 2007
TGpLinearGradientBrush *brush =
new TGpLinearGradientBrush(TGpRect(0, 0, 128, 128), TGpColor(32, kcRed), kcRed);
// 假定TGpGraphics对象g已经建立 g->FillRectangle(brush, 0, 0, 128, 128); delete brush;
图一 图二 图三 图四
二、多色渐变
使用TGpLinearGradientBrush的SetInterpolationColors 方法可以实现多色渐变, SetInterpolationColors方法
定义如下:
procedure SetInterpolationColors(const presetColors: array of TARGB;
const blendPositions: array of Single);
其中,presetColors为沿渐变的相应位置处使用的颜色的颜色数组;blendPositions为沿渐变线的位置,这些位置是介
于 0 到 1 之间的值,指定沿定位相应颜色的渐变线的距离的比率,其中,最左边的值必须为0,最右边的值必须为1,否则,会出现InvalidParameter的错误。
调用该方法后,将在沿渐变线的每一位置创建具有一种颜色的多色渐变。设置该属性将使该 TGpLinearGradientBrush
对象的以前所有颜色、位置和过渡设置无效。当然,尺寸和方向设置还是有效的。下面的代码建立了一个45度角的5色渐变刷,填充效果见图五:
// Delphi 2007 var
brush: TGpLinearGradientBrush; begin
brush := TGpLinearGradientBrush.Create(GpRect(0, 0, 128, 128), 0, 0, 45); brush.SetInterpolationColors([kcGreen, kcYellow, kcRed, kcBlue, kcOrange], [0, 0.25, 0.5, 0.75, 1]); // 假定TGpGraphics对象g已经建立 g.FillRectangle(brush, 0, 0, 128, 128); brush.Free; end;
// C++ Builder 2007
TGpColor colors[] = {kcGreen, kcYellow, kcRed, kcBlue, kcOrange}; float ps[] = {0, 0.25, 0.5, 0.75, 1}; TGpLinearGradientBrush *brush =
new TGpLinearGradientBrush(TGpRect(0, 0, 128, 128), 0, 0, 45); brush->SetInterpolationColors(colors, ps, 5); // 假定TGpGraphics对象g已经建立
g->FillRectangle(brush, 0, 0, 128, 128); delete brush;
例子中的渐变线的位置是均匀分配的(0, 0.25, 0.5, 0.75, 1),改变各个位置的值,可增减任意2色之间的过渡宽度。
图五 图六 图七
三、其它设置
1、重新设置起始色和结束色。下面的C++代码演示了颜色设置:
TGpLinearGradientBrush *brush =
new TGpLinearGradientBrush(TGpRect(0, 0, 128, 128), kcYellow, kcRed); // 假定TGpGraphics对象g已经建立
g->FillRectangle(brush, 0, 0, 128, 128); // 黄色到红色的渐变 brush->SetLinearColors(kcWhite, kcBlack);// 重新设置渐变色 g->TranslateTransform(128, 0);
g->FillRectangle(brush, 0, 0, 128, 128); // 白色到黑色的渐变 delete brush;
2、设置双色渐变的混合因子。TGpLinearGradientBrush的SetBlend方法可以按指定的位置设置渐变色的混合因子,
该方法定义如下:
// 设置 Blend,它指定为渐变定义自定义过渡的位置和因子。对双色渐变有效
// blendFactors:用于渐变的混合因子数组,混合因子表示相应位置结束色占起始色的比率。 // blendPositions:渐变的混合位置的数组,这些位置是介于 0 到 1 之间的值,
// 最左边必须为0,最右边必须为1
procedure SetBlend(const blendFactors, blendPositions: array of Single);
下面的代码演示混合效果,效果图见图六:
// Delphi 2007 var
brush: TGpLinearGradientBrush; begin
brush := TGpLinearGradientBrush.Create(GpRect(0, 0, 128, 128), kcYellow, kcRed); // 假定TGpGraphics对象g已经建立 g.FillRectangle(brush, 0, 0, 128, 128); brush.SetBlend([0, 0.3, 1], [0, 0.5, 1]); g.TranslateTransform(128, 0);
g.FillRectangle(brush, 0, 0, 128, 128); brush.Free; end;
// C++ Builder 2007 float fs[] = {0, 0.3, 1}; float ps[] = {0, 0.5, 1};
TGpLinearGradientBrush *brush =
new TGpLinearGradientBrush(TGpRect(0, 0, 128, 128), kcYellow, kcRed); // 假定TGpGraphics对象g已经建立 g->FillRectangle(brush, 0, 0, 128, 128); brush->SetBlend(fs, ps, 3); g->TranslateTransform(128, 0);
g->FillRectangle(brush, 0, 0, 128, 128); delete brush;
效果图左边是正常渐变填充,右边则是设置中间位置(0.5)的混合因子0.3(正常情况应该为0.5),所以右边图的中间的黄色比
例比正常渐变大。
3、设置钟型曲线渐变。
共分享92篇相关文档