如何在帆布上一起画两个梯度? [英] How to paint 2 gradients together on a Canvas?

查看:147
本文介绍了如何在帆布上一起画两个梯度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请查看我使用Paint程序进行的示例渐变图像:

Please look at this sample gradient image I did with a Paint program:

它包含2个垂直渐变。

从顶部到中间的渐变是白色混合到浅色的橙色。

The first gradient from the top to the middle is a blend of white, to a lightly colored orange.

从底部到中间的第二个渐变也是白色,但是要有一个稍暗的橙色。

The second gradient from the bottom to the middle is also a blend of white, but to a slightly darker orange.

关键是有2个渐变被使用,有4种颜色,2个白色和2个橙色变化。

The point is there are 2 gradients been used, with 4 colors, 2 whites and 2 orange variations.

我想在帆布上做,但不知道如何。颜色可以是任何东西,上面只是一个例子。

I would like to do this on a Canvas but don't know how to. The colors could be anything at all, above was just an example.

我该怎么做?

推荐答案

Delphi 2005及以上版本:



使用单位的GraphUtil.GradientFillCanvasrel =nofollow noreferrer> GradientFillCanvas

Delphi 2005 and up:

Use GradientFillCanvas from the GraphUtil unit:

procedure TForm1.FormPaint(Sender: TObject);
var
  R: TRect;
begin
  SetRect(R, 0, 0, ClientWidth, ClientHeight div 2);
  GradientFillCanvas(Canvas, clWhite, $00056AFF, R, gdVertical);
  SetRect(R, 0, ClientHeight div 2, ClientWidth, ClientHeight); 
  GradientFillCanvas(Canvas, $000055FF, clWhite, R, gdVertical);
end;






早期的Delphi版本:



使用 GradientFill Msimg32.dll 。将以下代码添加到全局实用程序单元中:


Earlier Delphi versions:

Use GradientFill from Msimg32.dll. Add the following code to a global utilities unit:

type
  PTriVertex = ^TTriVertex;
  TTriVertex = record
    X, Y: DWORD;
    Red, Green, Blue, Alpha: WORD;
  end;

function GradientFill(DC: HDC; Vertex: PTriVertex; NumVertex: ULONG;
  Mesh: Pointer; NumMesh, Mode: ULONG): BOOL; stdcall; overload;
  external msimg32 name 'GradientFill';

function GradientFill(DC: HDC; const ARect: TRect; StartColor,
  EndColor: TColor; Vertical: Boolean): Boolean; overload;
const
  Modes: array[Boolean] of ULONG = (GRADIENT_FILL_RECT_H, GRADIENT_FILL_RECT_V);
var
  Vertices: array[0..1] of TTriVertex;
  GRect: TGradientRect;
begin
  Vertices[0].X := ARect.Left;
  Vertices[0].Y := ARect.Top;
  Vertices[0].Red := GetRValue(ColorToRGB(StartColor)) shl 8;
  Vertices[0].Green := GetGValue(ColorToRGB(StartColor)) shl 8;
  Vertices[0].Blue := GetBValue(ColorToRGB(StartColor)) shl 8;
  Vertices[0].Alpha := 0;
  Vertices[1].X := ARect.Right;
  Vertices[1].Y := ARect.Bottom;
  Vertices[1].Red := GetRValue(ColorToRGB(EndColor)) shl 8;
  Vertices[1].Green := GetGValue(ColorToRGB(EndColor)) shl 8;
  Vertices[1].Blue := GetBValue(ColorToRGB(EndColor)) shl 8;
  Vertices[1].Alpha := 0;
  GRect.UpperLeft := 0;
  GRect.LowerRight := 1;
  Result := GradientFill(DC, @Vertices, 2, @GRect, 1, Modes[Vertical]);
end;

现在,绘画代码变为:

procedure TForm1.FormPaint(Sender: TObject);
var
  R: TRect;
begin
  SetRect(R, 0, 0, ClientWidth, ClientHeight div 2);
  GradientFill(Canvas.Handle, R, clWhite, $00056AFF, True);
  SetRect(R, 0, ClientHeight div 2, ClientWidth, ClientHeight); 
  GradientFill(Canvas.Handle, R, $000055FF, clWhite, True);
end;

procedure TForm1.FormResize(Sender: TObject);
begin
  Invalidate;
end;

这篇关于如何在帆布上一起画两个梯度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆