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

时间:2017-10-09 17:03:13

标签: delphi canvas rounded-corners

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

see form here

然而,我正努力填补这个头衔。使用64.23010091217222985704 $^{o}$时,所有表格都会重新绘制。试图在这里搜索,所以如果我错过了,请指出我去哪里。否则,我该怎么办?使用Delphi 7,OnPaint事件。

FillRect

2 个答案:

答案 0 :(得分:7)

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

Delphi 7的Documentation说:

  

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

     

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

来自Delphi XE7 doc:

  

使用RoundRect使用Pen绘制圆角矩形并填充   刷。

因此,在致电Pen之前,您需要定义BrushRoundRect()的颜色

代码的最后一个块应与

一致
  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

答案 1 :(得分:1)

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

或者,在所需区域周围绘制实线边框后,使用HRGN填充它。

相关问题