2017-10-09 174 views
2

我正在尝试使用Canvas绘制类似Dialog的窗体。我可以在其中放置圆角边框和圆角矩形作为标题/标题。我只想用画笔填写标题。Delphi 7 - 如何用画笔填充圆角矩形?

see form here

不过,我努力填补这个称号。当使用FillRect时,所有Form都会重新粉刷。试图在这里搜索,所以如果我错过了,只要指出我去哪里。否则,我该怎么做?使用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; 
+0

您的图片链接是brok恩。请勿在外部网站上放置图片。 StackOverflow有它自己的图像托管。请直接将您的图片上传到StackOverlow。 –

+0

@RemyLebeau编辑。 –

+3

在绘制圆角矩形的准备工作中,将“Brush”定义为您想要填充的颜色。从doc:*使用RoundRect使用Pen画出一个圆角矩形,并用Brush *填充它。如果我理解你的代码,在'RoundRect()'调用之前移动'Brush.Color:= BrushColor;'行并移除'FillRect()'调用。 –

回答

7

当您准备绘制圆角矩形,定义Brush.Color有你想要的矩形填充颜色,绘图前。

Documentation为Delphi 7表示:

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

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

德尔福XE7 DOC:

使用ROUNDRECT使用笔来绘制一个圆角矩形, 刷填充它。

所以,你需要调用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; 

和样本图像来定义颜色PenBrush

enter image description here

1

为了填充一个非矩形形状,可以创建所需的形状,如使用Win32 CreateRoundRectRgn()功能的HRGN,然后使用填充画布HRGN使用Win32 FillRgn()功能。

另外,周围绘制所需区域固体边界后,用TCanvas.FloodFill()填补进去。