【问题标题】:Delphi 7 - How to fill a rounded rectangle with brush?Delphi 7 - 如何用画笔填充圆角矩形?
【发布时间】:2017-10-10 15:31:36
【问题描述】:

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

但是,我正在努力填补这个标题。使用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 有自己的图像托管。请将您的图片直接上传到 StackOverlow。
  • @RemyLebeau 已编辑。
  • 在准备绘制圆角矩形时,将Brush 定义为您希望填充的颜色。来自文档:使用 RoundRect 使用 Pen 绘制圆角矩形并用 Brush 填充它。如果我理解您的代码,请将Brush.Color := BrushColor; 行移到RoundRect() 调用之前并删除FillRect() 调用。
  • @TomBrunberg 感谢您的评论。我在此之前尝试过,但是所有表格都已填写。
  • 我已撤销您的编辑,因为它不合适。如果您想分享您找到的答案,请使用下面的空间正确地分享答案。回答您自己的问题是 acceptable here,但前提是您这样做得当。

标签: delphi canvas rounded-corners


【解决方案1】:

当您准备绘制圆角矩形时,在绘制之前定义Brush.Color 以具有您希望矩形填充的颜色。

Delphi 7 的 Documentation 说:

矩形
在画布上用左上角绘制一个矩形 在点 (X1, Y1) 和它的右下角在点 (X2, Y2)。使用 Rectangle 使用 Pen 绘制一个框,然后使用 Brush 填充它。

圆形矩形
在画布上绘制一个圆角矩形。

来自 Delphi XE7 文档:

使用 RoundRect 用 Pen 画一个圆角矩形并用 刷机。

因此,您需要在调用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;

还有一张示例图片:

【讨论】:

    【解决方案2】:

    要填充非矩形形状,您可以创建所需形状的 HRGN,例如使用 Win32 CreateRoundRectRgn() 函数,然后使用 HRGN 和 Win32 FillRgn() 函数填充画布.

    或者,在所需区域周围绘制实心边框后,使用TCanvas.FloodFill() 填充它。

    【讨论】:

      猜你喜欢
      • 2011-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-07
      相关资源
      最近更新 更多