gpt4 book ai didi

delphi - Firemonkey 使用样式书隐藏圆角溢出

转载 作者:行者123 更新时间:2023-12-03 15:08:51 28 4
gpt4 key购买 nike

在 firemonkey 中,我尝试使用圆角矩形制作进度条。最简单的情况是一个矩形(进度条)和其中的第二个矩形(到目前为止的进度)。附上一个简单的例子。

带角的进度条(油漆): Progressbar with corners (paint)

我尝试过以下操作:

  1. 让第二个矩形也有圆角。这不起作用,因为如果第二个矩形非常短或几乎在末尾,这些舍入将会改变。
  2. 使用 Clipchildren。这与在 html/css 中 overflow hidden 几乎相同,但 Delphi 在此功能中不包含圆角。
  3. 创建一个应在其中绘制图像的 TPath。我真的很想避免这种解决方案,因为它不使用样式书。我更喜欢对所有样式使用一本样式书,而不是在代码中的多个位置使用样式解决方案。

什么有效:

  • 有一种非常丑陋的方法可以完成这项工作。我现在使用这个方法,但我真的希望你能帮我找到另一种解决方案。丑陋的方法是:只需使用一个矩形。用渐变画笔填充它,将两个渐变点设置在同一位置,并使渐变本身为0度。当我必须更改进度条等的状态时,此方法的结果是很多丑陋的代码。

这是我们可以避免的事情,还是这是唯一可能的解决方案?

进度条目标(绘制): Progressbar result

提前谢谢您!

一月

最佳答案

不太明白你的意思

Use clipchildren. This is almost the same as hiding overflow in html / css, but Delphi does not include rounded corners in this function.

我通过使用一个矩形作为边框来实现这一点;其顶部是进度布局,其中包含另一个矩形。第二个矩形始终具有第一个矩形的尺寸(这意味着角看起来相同),布局的 ClipChildren 设置为 true,并且通过设置其宽度

这是我的实现方式:

type

TRoundProgressBar = class (TLayout)
strict private
FProgress: Single;
FFill: TBrush;
FStroke: TStrokeBrush;
StrokeRect, FillRect: TRectangle;
FillLayout: TLayout;
procedure SetFill(const Value: TBrush);
procedure SetStroke(const Value: TStrokeBrush);
procedure FillChanged(Sender: TObject);
procedure StrokeChanged(Sender: TObject);
procedure SetProgress(Progress: Single);
procedure UpdateWidths;
protected
procedure Resize; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Fill: TBrush read FFill write SetFill;
property Stroke: TStrokeBrush read FStroke write SetStroke;
property Progress: Single read FProgress write SetProgress;
end;

implementation

constructor TRoundProgressBar.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FFill := TBrush.Create(TBrushKind.Solid, $FFE0E0E0);
FFill.OnChanged := FillChanged;
FStroke := TStrokeBrush.Create(TBrushKind.Solid, $FF000000);
FStroke.OnChanged := StrokeChanged;

FillLayout := TLayout.Create(self);
FillLayout.Parent := self;
FillLayout.Align := TAlignLayout.Left;
FillLayout.ClipChildren := true;

FillRect := TRectangle.Create(FillLayout);
FillRect.Parent := FillLayout;
FillRect.Align := TAlignLayout.Left;
FillRect.XRadius := 15;
FillRect.YRadius := 15;

StrokeRect := TRectangle.Create(self);
StrokeRect.Parent := self;
StrokeRect.Align := TAlignLayout.Contents;
StrokeRect.XRadius := 15;
StrokeRect.YRadius := 15;
StrokeRect.Fill.Kind := TBrushKind.None;
end;

destructor TRoundProgressBar.Destroy;
begin
FFill.Free;
FStroke.Free;
inherited;
end;

procedure TRoundProgressBar.SetFill(const Value: TBrush);
begin
FFill.Assign(Value);
end;

procedure TRoundProgressBar.SetProgress(Progress: Single);
begin
FProgress := Min(Max(Progress, 0), 100);
UpdateWidths;
end;

procedure TRoundProgressBar.FillChanged(Sender: TObject);
begin
FillRect.Fill.Assign(FFill);
end;

procedure TRoundProgressBar.Resize;
begin
inherited;
UpdateWidths;
end;

procedure TRoundProgressBar.SetStroke(const Value: TStrokeBrush);
begin
FStroke.Assign(Value);
end;

procedure TRoundProgressBar.StrokeChanged(Sender: TObject);
begin
StrokeRect.Stroke.Assign(FStroke);
end;

procedure TRoundProgressBar.UpdateWidths;
begin
FillRect.Width := Width;
FillLayout.Width := Width * (FProgress / 100);
Repaint;
end;

关于delphi - Firemonkey 使用样式书隐藏圆角溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42345751/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com