function SSDrawGradient(ACanvas: TCanvas; AClipRect: TRect; FromPoint, ToPoint: TPoint; FromColor, ToColor: TColor): Boolean; type TSSGradientDirection=(gdEast, gdWest, gdNorth, gdSouth, gdOther); var buf:TBitmap; w,h,y,x,XOffset,ir,ig,ib,pw,ph:Integer; c1, c2: TColor; r1,g1,b1,r2,g2,b2,br,bg,bb,rmax,rmin,gmax,gmin,bmax,bmin: Byte; kx,ky,kx0,ky0,rx0,gx0,bx0,r0,g0,b0,drx,dry,dgx,dgy,dbx,dby,dr,dg,db: Double; P : PByteArray; function GetStep(V1, V2, V3:Integer): Double; begin if V2=V1 then Result:=0 else Result:=V3/(V2-V1); end;begin Result:=False; if (FromPoint.Y=ToPoint.Y)and(FromPoint.X=ToPoint.X) then Exit; buf:=TBitmap.Create; try //初始化缓冲区 buf.PixelFormat:=pf24bit; w:=WidthOfRect(AClipRect); buf.Width:=w; h:=HeightOfRect(AClipRect); buf.Height:=h; //为了防止运算溢出而设的检查 if (w>Screen.Width)or(h>Screen.Height) then Exit; //读取渐变起点和终点的RGB值 c1:=ColorToRGB(FromColor); c2:=ColorToRGB(ToColor); r1:=GetRValue(c1); g1:=GetGValue(c1); b1:=GetBValue(c1); r2:=GetRValue(c2); g2:=GetGValue(c2); b2:=GetBValue(c2); if r1>r2 then begin rmin:=r2; rmax:=r1 end else begin rmin:=r1; rmax:=r2 end; if g1>g2 then begin gmin:=g2; gmax:=g1 end else begin gmin:=g1; gmax:=g2 end; if b1>b2 then begin bmin:=b2; bmax:=b1 end else begin bmin:=b1; bmax:=b2 end; pw:=Abs(ToPoint.X-FromPoint.X); ph:=Abs(ToPoint.Y-FromPoint.Y); kx:=pw/Sqrt(ph*ph+pw*pw); ky:=ph/Sqrt(ph*ph+pw*pw);
//计算出RGB值相对于XY轴的线性变化系数 drx:=GetStep(AClipRect.Left, AClipRect.Right, Round((r2-r1)*kx)); dry:=GetStep(AClipRect.Top, AClipRect.Bottom, Round((r2-r1)*ky)); dgx:=GetStep(AClipRect.Left, AClipRect.Right, Round((g2-g1)*kx)); dgy:=GetStep(AClipRect.Top, AClipRect.Bottom, Round((g2-g1)*ky)); dbx:=GetStep(AClipRect.Left, AClipRect.Right, Round((b2-b1)*kx)); dby:=GetStep(AClipRect.Top, AClipRect.Bottom, Round((b2-b1)*ky)); //计算出矩形左上角的RGB值,备用 kx0:=GetStep(FromPoint.X, ToPoint.X, FromPoint.X); ky0:=GetStep(FromPoint.Y, ToPoint.Y, FromPoint.Y); r0:=r1+(kx0+ky0)*r2; g0:=g1+(kx0+ky0)*g2; b0:=b1+(kx0+ky0)*b2; //这三个变量是每个扫描线的第一个点的RGB值 rx0:=r0; gx0:=g0; bx0:=b0; for y:=0 to h-1 do begin XOffset:=0; //dr意思是Double类型的红色值,其他类推 dr:=rx0; dg:=gx0; db:=bx0; P := buf.ScanLine[y]; for x:=0 to w-1 do begin //ir的意思是整型的红色值,其他类推 //之所以要先转成整型,是因为我觉得整型的比较也许会比浮点快一点 //反正都要三次Round的,不如早做…… ir:=Round(dr); ig:=Round(dg); ib:=Round(db); //br的意思是字节型的红色值 br:=Max(Min(rmax,ir),rmin); bg:=Max(Min(gmax,ig),gmin); bb:=Max(Min(bmax,ib),bmin); //按照偏移量设置RGB值 P[XOffset]:=bb; P[XOffset+1]:=bg; P[XOffset+2]:=br; if FromPoint.X<>ToPoint.X then begin //下一个像素的RGB值分别按照一定的系数递增 dr:=dr+drx; dg:=dg+dgx; db:=db+dbx; end; //因为我定义的P是字节型的数组,所以这里递增“3”,避免使用乘法 Inc(XOffset, 3); end; if FromPoint.Y<>ToPoint.Y then begin //按照RGB在Y轴方向上的变化规律计算下一行的第一个像素RGB值 rx0:=rx0+dry; gx0:=gx0+dgy; bx0:=bx0+dby; end; end; //将缓冲区复制到目标上 BitBlt(ACanvas.Handle, AClipRect.Left, AClipRect.Top, w, h, buf.Canvas.Handle, 0, 0, SRCCOPY); Result:=True; finally buf.Free; end; end
找来的 ========= 界面色彩渐变效果是通过用渐变的画刷刷绘依次相邻的矩形块实现的。下面列举实例说明: 1.新建一个表单,假设其Width为500,设置一个按钮Button1,按此按钮将把表单置为由左向右由黄变白的渐变效果。 2.Button1按钮的代码如下: procedure TForm1.Button1Click(Sender: TObject); var i,j:Integer; Dct:TRect; begin j:=Form1.height; //获得表单高度 for i:=0 to 255 do //此处设置RGB()中一个颜色值 begin Canvas.Brush.Color:=RGB(255,255,i); //每次画矩形的画刷颜色 Dct:=Rect(i*2,0,(i+1)*2,j); //每次刷绘的矩形区域 Canvas.FillRect(Dct); //填充颜色 end; end;
你是哪里人?
FromPoint, ToPoint: TPoint; FromColor, ToColor: TColor): Boolean;
type
TSSGradientDirection=(gdEast, gdWest, gdNorth, gdSouth, gdOther);
var
buf:TBitmap;
w,h,y,x,XOffset,ir,ig,ib,pw,ph:Integer;
c1, c2: TColor;
r1,g1,b1,r2,g2,b2,br,bg,bb,rmax,rmin,gmax,gmin,bmax,bmin: Byte;
kx,ky,kx0,ky0,rx0,gx0,bx0,r0,g0,b0,drx,dry,dgx,dgy,dbx,dby,dr,dg,db: Double;
P : PByteArray; function GetStep(V1, V2, V3:Integer): Double;
begin
if V2=V1 then Result:=0
else Result:=V3/(V2-V1);
end;begin
Result:=False;
if (FromPoint.Y=ToPoint.Y)and(FromPoint.X=ToPoint.X) then Exit;
buf:=TBitmap.Create;
try
//初始化缓冲区
buf.PixelFormat:=pf24bit;
w:=WidthOfRect(AClipRect);
buf.Width:=w;
h:=HeightOfRect(AClipRect);
buf.Height:=h;
//为了防止运算溢出而设的检查
if (w>Screen.Width)or(h>Screen.Height) then Exit; //读取渐变起点和终点的RGB值
c1:=ColorToRGB(FromColor);
c2:=ColorToRGB(ToColor);
r1:=GetRValue(c1);
g1:=GetGValue(c1);
b1:=GetBValue(c1);
r2:=GetRValue(c2);
g2:=GetGValue(c2);
b2:=GetBValue(c2);
if r1>r2 then begin rmin:=r2; rmax:=r1 end
else begin rmin:=r1; rmax:=r2 end;
if g1>g2 then begin gmin:=g2; gmax:=g1 end
else begin gmin:=g1; gmax:=g2 end;
if b1>b2 then begin bmin:=b2; bmax:=b1 end
else begin bmin:=b1; bmax:=b2 end;
pw:=Abs(ToPoint.X-FromPoint.X);
ph:=Abs(ToPoint.Y-FromPoint.Y);
kx:=pw/Sqrt(ph*ph+pw*pw);
ky:=ph/Sqrt(ph*ph+pw*pw);
//计算出RGB值相对于XY轴的线性变化系数
drx:=GetStep(AClipRect.Left, AClipRect.Right, Round((r2-r1)*kx));
dry:=GetStep(AClipRect.Top, AClipRect.Bottom, Round((r2-r1)*ky));
dgx:=GetStep(AClipRect.Left, AClipRect.Right, Round((g2-g1)*kx));
dgy:=GetStep(AClipRect.Top, AClipRect.Bottom, Round((g2-g1)*ky));
dbx:=GetStep(AClipRect.Left, AClipRect.Right, Round((b2-b1)*kx));
dby:=GetStep(AClipRect.Top, AClipRect.Bottom, Round((b2-b1)*ky)); //计算出矩形左上角的RGB值,备用
kx0:=GetStep(FromPoint.X, ToPoint.X, FromPoint.X);
ky0:=GetStep(FromPoint.Y, ToPoint.Y, FromPoint.Y);
r0:=r1+(kx0+ky0)*r2;
g0:=g1+(kx0+ky0)*g2;
b0:=b1+(kx0+ky0)*b2; //这三个变量是每个扫描线的第一个点的RGB值
rx0:=r0;
gx0:=g0;
bx0:=b0;
for y:=0 to h-1 do
begin
XOffset:=0;
//dr意思是Double类型的红色值,其他类推
dr:=rx0;
dg:=gx0;
db:=bx0;
P := buf.ScanLine[y];
for x:=0 to w-1 do
begin
//ir的意思是整型的红色值,其他类推
//之所以要先转成整型,是因为我觉得整型的比较也许会比浮点快一点
//反正都要三次Round的,不如早做……
ir:=Round(dr);
ig:=Round(dg);
ib:=Round(db);
//br的意思是字节型的红色值
br:=Max(Min(rmax,ir),rmin);
bg:=Max(Min(gmax,ig),gmin);
bb:=Max(Min(bmax,ib),bmin);
//按照偏移量设置RGB值
P[XOffset]:=bb;
P[XOffset+1]:=bg;
P[XOffset+2]:=br; if FromPoint.X<>ToPoint.X then
begin
//下一个像素的RGB值分别按照一定的系数递增
dr:=dr+drx;
dg:=dg+dgx;
db:=db+dbx;
end;
//因为我定义的P是字节型的数组,所以这里递增“3”,避免使用乘法
Inc(XOffset, 3);
end;
if FromPoint.Y<>ToPoint.Y then
begin
//按照RGB在Y轴方向上的变化规律计算下一行的第一个像素RGB值
rx0:=rx0+dry;
gx0:=gx0+dgy;
bx0:=bx0+dby;
end;
end;
//将缓冲区复制到目标上
BitBlt(ACanvas.Handle, AClipRect.Left, AClipRect.Top, w, h,
buf.Canvas.Handle, 0, 0, SRCCOPY);
Result:=True;
finally
buf.Free;
end;
end
=========
界面色彩渐变效果是通过用渐变的画刷刷绘依次相邻的矩形块实现的。下面列举实例说明:
1.新建一个表单,假设其Width为500,设置一个按钮Button1,按此按钮将把表单置为由左向右由黄变白的渐变效果。
2.Button1按钮的代码如下:
procedure TForm1.Button1Click(Sender: TObject);
var i,j:Integer;
Dct:TRect;
begin
j:=Form1.height;
//获得表单高度
for i:=0 to 255 do
//此处设置RGB()中一个颜色值
begin
Canvas.Brush.Color:=RGB(255,255,i);
//每次画矩形的画刷颜色
Dct:=Rect(i*2,0,(i+1)*2,j);
//每次刷绘的矩形区域 Canvas.FillRect(Dct);
//填充颜色
end;
end;
用image填充整个窗体,所有控件都放在image上,然后在image的onpaint事件中
编写渐变效果,这样速度会提高数倍
另外,我的表单上面不放任何按钮,不用考虑覆盖的问题。