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

Fel*_*ais 2 delphi canvas rounded-corners

我正在尝试使用Canvas绘制类似Dialog的表单.我可以在其中添加圆形边框和圆角矩形作为标题/标题.我想用画笔填充标题.

在这里看表格

但是,我正在努力填补这个头衔.使用时,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;
Run Code Online (Sandbox Code Playgroud)

Tom*_*erg 8

准备绘制圆角矩形Brush.Color时,在绘制之前定义为要填充矩形的颜色.

Delphi 7 的文档说:

矩形
绘制的点处,其左上角(X1,Y1),并在点其右下角(X2,Y2)在画布上的矩形.使用矩形使用Pen绘制一个框并使用Brush填充它.

RoundRect
在画布上绘制一个带圆角的矩形.

来自Delphi XE7 doc:

使用RoundRect使用Pen绘制圆角矩形并使用Brush填充它.

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

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

  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;
Run Code Online (Sandbox Code Playgroud)

还有一个示例图片:

在此输入图像描述