如何在Firemonkey中绘制虚线? [英] How to draw a dotted line in Firemonkey?

查看:73
本文介绍了如何在Firemonkey中绘制虚线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Firemonkey项目的TPaintbox画布上绘制一个虚线网格,结果应该完全像这样:

I want to draw a dotted grid on a TPaintbox canvas in a Firemonkey project, the outcome should be exactly like this:

首先,我想先绘制垂直虚线,然后再绘制水平虚线,因此考虑到这一点,我尝试绘制一条直线首先尝试使外观正确,这是我尝试的方法:

To start with I thought I would draw the vertical dotted lines and then the horizontal dotted lines, so with that in mind I attempted to draw a single line first in attempt to get the appearance just right, this is what I tried:

Canvas.Stroke.Color := TAlphaColorRec.Black;
Canvas.Stroke.Dash := TStrokeDash.Dot;
Canvas.Stroke.Kind := TBrushKind.Solid;
Canvas.Stroke.Thickness := 1;
Canvas.DrawLine(PointF(0, 0), PointF(0, 150), 1);

结果不是我希望的,实际上是 TLine 形状可以按照我的意愿做点虚线:

The result is not what I had hoped, in fact the TLine shape can do a dotted line how I want it to:

但是我需要自己在画布上绘制此图,而不要使用其他控件。对于记录 TLine ,只需将Stroke.Dash属性更改为Dot。

But I need to do this drawing myself on the canvas and not use additional controls. For the record TLine simply needs the Stroke.Dash property changing to Dot.

因此,在Firemonkey项目我如何绘制与 TLine 相同的虚线,以便可以像第一个示例图像一样绘制网格?

So, using the canvas in a Firemonkey project how may I draw a dotted line the same as TLine does so that I can draw a grid like the first sample image?

推荐答案

无需深入研究它为什么起作用,您就可以实现1像素的虚线(例如在 TLine 中)向坐标添加(或减去)线宽的一半。追溯 TLine 的工作时,我有了主意。在渲染过程中,它使用

Without going into the 'why it works' you can achieve 1 pixel dotted line (like in TLine) by adding (or subtracting) half the line width to the coordinates. I got the idea when tracing through what TLine does; during rendering it uses

InflateRect(Result, -(FStroke.Thickness / 2), -(FStroke.Thickness / 2));

将0.5修改应用于线坐标,将得到相同的结果。

Applying the 0.5 modification to the line coordinates, gives the same result.

procedure TForm24.PaintBox1Paint(Sender: TObject; Canvas: TCanvas);
var
  a: integer;
begin
  Canvas.BeginScene;
  try
    Canvas.Stroke.Dash := TStrokeDash.Dot;

    for a := 0 to 10 do
      Canvas.DrawLine(PointF(a * 20 + 0.5, 0.5), PointF(a * 20 + 0.5, 200.5), 1);

    for a := 0 to 10 do
      Canvas.DrawLine(PointF(0.5, a * 20 + 0.5), PointF(200.5, a * 20 + 0.5), 1);

    Canvas.DrawLine(PointF(0.5, 0.5), PointF(200.5, 200.5), 1);
    Canvas.DrawEllipse(RectF(5.0, 5.0, 195.5, 195.5), 1);
  finally
    Canvas.EndScene;
  end;
end;

会产生以下结果:


放大镜视图为500%

编辑2018.02.23

另一种值得测试的方法是更改​​表格 质量属性为高性能

An alternative way, worth to test, is to change the forms Quality property to HighPerformance

这篇关于如何在Firemonkey中绘制虚线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆