到目前为止,Graphics32 中没有直接的模式支持,但是有几十种方法可以创建您想要使用的模式。
这是使用示例多边形填充的一种解决方案:
首先,您需要为阴影图案编写一个采样器类。有几种方法可以构建这样的采样器。下面你可以找到一个非常简单的:
type
THatchedPatternSampler = class(TCustomSampler)
public
function GetSampleInt(X, Y: Integer): TColor32; override;
end;
function THatchedPatternSampler.GetSampleInt(X, Y: Integer): TColor32;
begin
Result := 0;
if ((X - Y) mod 8 = 0) or ((X + Y) mod 8 = 0) then
Result := clRed32
end;
您只需在此处重写一个方法(GetSampleInt),所有其他方法都可以从祖先类中使用。
现在它变得有点复杂了。为了使用示例,您必须在 TSamplerFiller 上使用它,如下所示:
Sampler := THatchedPatternSampler.Create;
Filler := TSamplerFiller.Create(Sampler);
一旦你有了这个填充物,你就可以在 PolygonFS 甚至 PolylineFS 中使用它。
最后代码可能如下所示:
var
Polygon: TArrayOfFloatPoint;
Sampler: THatchedPatternSampler;
Filler: TSamplerFiller;
begin
Polygon := Ellipse(128, 128, 120, 100);
Sampler := THatchedPatternSampler.Create;
try
Filler := TSamplerFiller.Create(Sampler);
try
PolygonFS(PaintBox32.Buffer, Polygon, Filler);
finally
Filler.Free;
end;
finally
Sampler.Free;
end;
PolylineFS(PaintBox32.Buffer, Polygon, clRed32, True, 1);
end;
这将在位图的中心(这里:TPaintBox32 实例的缓冲区)绘制一个相当大的椭圆,并用阴影采样器代码填充它。最后使用 PolylineFS 函数绘制一个实心轮廓。
从性能的角度来看,这不是最快的方法,因为 GetSampleInt 是按像素调用的。但是,最容易理解发生了什么。
要获得更快的替代方案,您应该直接使用填充剂。您可以像这样直接从 TCustomPolygonFiller 派生:
type
THatchedPatternFiller = class(TCustomPolygonFiller)
private
procedure FillLine(Dst: PColor32; DstX, DstY, Length: Integer; AlphaValues: PColor32);
protected
function GetFillLine: TFillLineEvent; override;
end;
GetFillLine 方法变得如此简单:
function THatchedPatternFiller.GetFillLine: TFillLineEvent;
begin
Result := FillLine;
end;
但是,FillLine 方法会稍微复杂一些,如下所示:
procedure THatchedPatternFiller.FillLine(Dst: PColor32; DstX, DstY,
Length: Integer; AlphaValues: PColor32);
var
X: Integer;
begin
for X := DstX to DstX + Length do
begin
if ((X - DstY) mod 8 = 0) or ((X + DstY) mod 8 = 0) then
Dst^ :=clRed32
else
Dst^ := 0;
Inc(Dst);
end;
end;
由于 DstY 保持不变,您还可以重构代码以提高性能。或者您可以使用汇编程序 (SSE) 加速代码,但我想这对于这样一个简单的函数来说太过分了。