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

3

我正在尝试使用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;

你的图片链接已经失效了。请不要将图片托管在外部网站上。StackOverflow有自己的图片托管服务。请直接上传你的图片到StackOverflow。 - Remy Lebeau
3
在准备绘制圆角矩形之前,将“Brush”定义为您想要填充它的颜色。根据文档:使用Pen绘制圆角矩形并用Brush进行填充。如果我理解您的代码,移动Brush.Color := BrushColor;这行代码到RoundRect()调用之前,并删除FillRect()调用即可。 - Tom Brunberg
汤姆是正确的。例如:Canvas.Brush.Color := clRed; Canvas.RoundRect(10, 10, 150, 50, 30, 10); 可以绘制一个填充的圆角矩形。请提供一个 [mcve],我们可以看到你做错了什么。 - Sertac Akyuz
我没有看到你之前的代码。你把我告诉你的那行代码移动到了 RoundRect(Component_Gutter ....) 的前面,并且按照我的建议删除了 FillRect() 调用吗?现在的结果是什么?它不可能和你所声称的问题相同。你打算提供一个 MCVE,还是只是等待奇迹发生? - Tom Brunberg
1
我已经撤销了你的编辑,因为它是不合适的。如果你想分享你找到的答案,请使用下面的空间适当地分享。在这里回答自己的问题是可以接受的,但只有在你适当地这样做时才可以。 - Ken White
显示剩余3条评论
2个回答

9
当您准备绘制圆角矩形时,在绘制之前定义Brush.Color为所需填充的颜色。Delphi 7的文档中写道:

Rectangle
在画布上绘制一个矩形,其左上角位于点(X1,Y1),右下角位于点(X2,Y2)。使用Rectangle使用Pen绘制框并使用Brush填充。

RoundRect
在画布上绘制带有圆角的矩形。

从Delphi XE7文档中可以看到:

使用RoundRect使用Pen绘制一个带有圆角的矩形,并使用Brush填充。

因此,在调用RoundRect()之前,您需要为PenBrush定义颜色。
您的代码的最后一块应该与此相一致。
  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;

以下是一个示例图片:

输入图像描述


此处涉及到IT技术相关内容,需要更多上下文信息才能提供准确的翻译。

1
为了填充非矩形形状,您可以创建所需形状的HRGN,例如使用Win32 CreateRoundRectRgn()函数,并使用Win32 FillRgn()函数使用该HRGN填充画布。
或者,在绘制所需区域周围的实线边框之后,使用TCanvas.FloodFill()进行填充。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接