Diagonal brush style gives me black area

岁酱吖の 提交于 2019-12-12 17:17:09

问题


I want to draw a diagonal cross into a Canvas with this code:

InFlateRect(r, -1, -1);
Canvas.Brush.Color := clYellow;
Canvas.Brush.Style := bsFDiagonal;
Canvas.Pen.Color := clRed;
//Pen.Style := psClear;
Canvas.Rectangle(r);

But the result is a black box.

If I remove the style changing, a normal solid yellow area I got.

Why is the rectangle black with this code?

Thanks for every suggestion


Sorry for missing info, I extend it: This procedure is using TMetaFileCanvas to draw. On normal Form I can draw any brush style, like the TShape...


回答1:


procedure TForm4.FormPaint(Sender: TObject);
var
  R: TRect;
begin
  R := ClientRect;
  InflateRect(R, -10, -10);
  Canvas.Brush.Color := clYellow;
  Canvas.Brush.Style := bsFDiagonal;
  Canvas.Pen.Color := clRed;
  Canvas.Rectangle(R);
end;

produces the result

(Don't forget to Invalidate in the form's OnResize.)

Notice that the area is filled with diagonal yellow lines. This is indeed what the bsFDiagonal brush style does.

From the wording of your question ("I want to draw a diagonal cross [...]"), I suspect you actually want something else, namely, a big cross inside the rectangle. This you cannot achieve using the standard brushes at all. Rather, you have to draw it manually:

procedure TForm4.FormPaint(Sender: TObject);
var
  R: TRect;
begin
  R := ClientRect;
  InflateRect(R, -10, -10);
  Canvas.Brush.Color := clWhite;
  Canvas.Brush.Style := bsSolid;
  Canvas.Pen.Color := clRed;
  Canvas.Rectangle(R);
  Canvas.MoveTo(10, 10);
  Canvas.LineTo(R.Right, R.Bottom);
  Canvas.MoveTo(10, R.Bottom);
  Canvas.LineTo(R.Right, 10);
end;



来源:https://stackoverflow.com/questions/12709166/diagonal-brush-style-gives-me-black-area

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!