- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Canvas 来绘制类似对话框的表单。我可以在其中放置圆角边框和圆角矩形作为标题/标题。我只想用画笔填充标题。
但是,我正在努力填补这个头衔。当使用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;
最佳答案
当您准备绘制圆角矩形时,请在绘制之前定义Brush.Color
以获取您想要填充矩形的颜色。
Documentation对于 Delphi 7 来说:
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()
之前,您需要定义 Pen
和 Brush
的颜色
代码的最后一个 block 应该符合
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;
还有示例图像:
关于Delphi 7 - 如何用画笔填充圆角矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46651543/
我是一名优秀的程序员,十分优秀!