2017-10-09 233 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()填補進去。