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

Delphi 7 - How to fill a rounded rectangle with brush?

我正在尝试使用 Canvas 绘制 Dialog-like 表格。我可以在其中放置圆角边框和圆角矩形作为 header/title。我只想用画笔填充标题。

但是,我正在努力填补这个头衔。使用 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;

要填充非矩形形状,您可以创建所需形状的 HRGN,例如使用 Win32 CreateRoundRectRgn() function, and then fill the Canvas using that HRGN with the Win32 FillRgn() 函数。

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

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

Delphi 7 的 Documentation 说:

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 的颜色

你的代码的最后一块应该符合

  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;

和示例图片: