gpt4 book ai didi

Delphi 7 - 如何用画笔填充圆角矩形?

转载 作者:行者123 更新时间:2023-12-02 07:28:31 24 4
gpt4 key购买 nike

我正在尝试使用 Canvas 来绘制类似对话框的表单。我可以在其中放置圆角边框和圆角矩形作为标题/标题。我只想用画笔填充标题。

see form here

但是,我正在努力填补这个头衔。当使用FillRect时,所有表单都会被重新绘制。尝试在这里搜索,所以如果我错过了,请告诉我该去哪里。不然我怎么办呢?使用Delphi 7,OnPaint事件。

procedure TCustomDialog.FormPaint(Sender: TObject);
var
Rect: TRect;
BorderColor: TColor;
BrushColor: TColor;
begin
// Rect for Form's borders;
Rect.Left := 0;
Rect.Top := 0;
Rect.Right := ClientWidth;
Rect.Bottom := ClientHeight;

BorderColor := HtmlToTColor('#ffffff');
BrushColor := HtmlToTColor('#ffffff');

// Here I set the colors of Canvas.Pen (border) and Canvas.Brush (Filling),
// similar to Bootstrap themes/classes (Default, Success, Warning, Danger);
case DialogType of
dtInformation:
begin
BorderColor := HtmlToTColor(Header_Color_Pen_Information);
BrushColor := HtmlToTColor(Header_Color_Brush_Information);
end;

dtSuccess:
begin
BorderColor := HtmlToTColor(Header_Color_Pen_Success);
BrushColor := HtmlToTColor(Header_Color_Brush_Success);
end;

dtWarning:
begin
BorderColor := HtmlToTColor(Header_Color_Pen_Warning);
BrushColor := HtmlToTColor(Header_Color_Brush_Warning);
end;

dtError:
begin
BorderColor := HtmlToTColor(Header_Color_Pen_Error);
BrushColor := HtmlToTColor(Header_Color_Brush_Error);
end;
end;

with Canvas do
begin
Pen.Color := BorderColor;
Pen.Width := Form_Pen_Width;

// Draw rounded borders for Form;
RoundRect(1, 1, Rect.Right - 1, Rect.Bottom - 1, Form_Border_Radius - 1, Form_Border_Radius - 1);

// Rect for Dialog's Header;
Rect.Left := Component_Gutter;
Rect.Top := Component_Gutter;
Rect.Right := ClientWidth - Component_Gutter;
Rect.Bottom := Form_Header_Height;

RoundRect(Component_Gutter, Component_Gutter, ClientWidth - Component_Gutter, Form_Header_Height,
Form_Border_Radius - 2, Form_Border_Radius - 2);

Brush.Color := BrushColor;
FillRect(Rect);
end;
end;

最佳答案

当您准备绘制圆角矩形时,请在绘制之前定义Brush.Color 以获取您想要填充矩形的颜色。

Documentation对于 Delphi 7 来说:

Rectangle
Draws a rectangle on the canvas with its upper left corner at the point (X1, Y1) and its lower right corner at the point (X2, Y2). Use Rectangle to draw a box using Pen and fill it using Brush.

RoundRect
Draws a rectangle with rounded corners on the canvas.

来自 Delphi XE7 文档:

Use RoundRect to draw a rounded rectangle using Pen and fill it with Brush.

因此,在调用 RoundRect() 之前,您需要定义 PenBrush 的颜色

代码的最后一个 block 应该符合

  with Canvas do
begin
Pen.Color := BorderColor;
Pen.Width := Form_Pen_Width;
Brush.Color := BrushColor; // Add this line to control which fill color the form will have

// Draw rounded borders for Form;
RoundRect(1, 1, Rect.Right - 1, Rect.Bottom - 1, Form_Border_Radius - 1, Form_Border_Radius - 1);

// Rect for Dialog's Header;
Rect.Left := Component_Gutter;
Rect.Top := Component_Gutter;
Rect.Right := ClientWidth - Component_Gutter;
Rect.Bottom := Form_Header_Height;

Brush.Color := clYellow; // This line defines the fill color of the "header"
RoundRect(Component_Gutter, Component_Gutter, ClientWidth - Component_Gutter, Form_Header_Height, Form_Border_Radius - 2, Form_Border_Radius - 2);

Brush.Color := BrushColor; // Resets the brush color to the same as the form has
// FillRect(Rect); Remove this line, as it overdraws the "header" incl. its border
end;

还有示例图像:

enter image description here

关于Delphi 7 - 如何用画笔填充圆角矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46651543/

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