【问题标题】:How to draw a line in Delphi on an FMX canvas如何在 FMX 画布上用 Delphi 画一条线
【发布时间】:2017-04-02 08:08:50
【问题描述】:

这是 Delphi Berlin 10.1 Update 2

以下作品(我画了一条线):

brush := TStrokeBrush.Create(TBrushKind.Solid, TAlphaColors.Lightgray);
brush.Thickness := 2;
with Canvas do
begin
    BeginUpdate;
    DrawLine(PointF(10, 10), PointF(100, 10), 1, brush);
    EndUpdate;
end;

以下不起作用:

with Canvas do
begin
    BeginUpdate;
    Stroke.Color := TAlphaColors.Black;
    Stroke.Thickness := 2.0;
    DrawLine(PointF(10, 10), PointF(100, 10), 1);
    EndUpdate;
end;

为什么我不能使用第二个?我怎样才能让它工作,或者我应该像第一个例子一样坚持创建笔刷?

我已经包含了一个最小的应用程序:

main.pas

unit main;

interface

uses
    System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Objects;

type
    TMainForm = class(TForm)
        PaintBox: TPaintBox;
        procedure OnPaint(Sender: TObject; Canvas: TCanvas);
    private
        { Private declarations }
    public
        { Public declarations }
    end;

var
    MainForm: TMainForm;

implementation

{$R *.fmx}

procedure TMainForm.OnPaint(Sender: TObject; Canvas: TCanvas);
begin
    with Canvas do
    begin
        BeginUpdate;
        Stroke.Color := TAlphaColors.Black;
        Stroke.Thickness := 2.0;
        DrawLine(PointF(10, 10), PointF(100, 10), 1);
        EndUpdate;
    end;
end;

end.

main.fmx:

object MainForm: TMainForm
    Left = 0
    Top = 0
    Caption = 'Form1'
    ClientHeight = 480
    ClientWidth = 640
    FormFactor.Width = 320
    FormFactor.Height = 480
    FormFactor.Devices = [Desktop]
    DesignerMasterStyle = 0
    object PaintBox: TPaintBox
        Position.X = 16.000000000000000000
        Position.Y = 16.000000000000000000
        Size.Width = 609.000000000000000000
        Size.Height = 449.000000000000000000
        Size.PlatformDefault = False
        OnPaint = OnPaint
    end
end

test.dpr:

program test;

uses
   System.StartUpCopy,
   FMX.Forms,
   main in 'main.pas' {MainForm};

{$R *.res}

begin
    Application.Initialize;
    Application.CreateForm(TMainForm, MainForm);
    Application.Run;
end.

【问题讨论】:

  • 第二个示例中的代码很好。请出示minimal reproducible example。您是否尝试在 Paint 方法或 OnPaint 处理程序之外进行绘制?
  • Stroke.Kind := TBrushKind.bkSolid; 在 Android 上可能需要,因为它默认为 bkNone。在 Windows 上,默认值为 bkSolid
  • 我添加了显示问题所需的最少文件
  • @imekon 那么你的代码对我来说当然可以正常工作(10.1 Seattle,windows)——你是为 android 编译吗? IOS?麦克?
  • 你真的需要停止使用 with

标签: delphi canvas firemonkey delphi-10.1-berlin paintbox


【解决方案1】:

早期版本的 Delphi 对 Stroke.Kind 有不同的默认值,具体取决于平台。

自 Delphi 10.1 Berlin 以来,所有平台的默认值似乎都是 None。 (感谢@TomBrunberg)

要使线条出现,请设置Stroke.Kind := TBrushKind.Solid;

注意:在 Windows 8.1 上测试


我还创建了一个新的 Metropolis FMX 应用程序,其中绘图工作没有设置 Stroke.Kind。虽然无法解释。


QP 提交了一份类似的报告,RSP-16313 The Canvas.DrawLine doesn't work at Windows XP (32 bit) forms。这里问题在 Windows-XP 上出现,但在 Windows-7 上没有。

【讨论】:

  • 宾果游戏!添加 Stroke.Kind := TBrushKind.Solid 就是答案。但是为什么要添加这么奇怪的默认值呢?
  • 我不知道。也许该值根本不是持久的,但会因上下文而异。无论如何,在写作之前始终将其设置为solid,适用于所有情况。
【解决方案2】:

第一种情况很奇怪。

你应该使用笔刷。

您应该使用 BeginScene 和 EndScene 而不是 BeginUpdate 和 EndUpdate。该片段完美运行:

  Brush := TStrokeBrush.Create(TBrushKind.Solid, TAlphaColors.Black);
  Brush.Thickness := 2;
  with Canvas do
  begin
    BeginScene();
    DrawLine(PointF(10, 10), PointF(100, 10), 1, Brush);
    EndScene;
  end;

【讨论】:

  • 该问题指出使用单独的 Brush 有效,并询问为什么在没有这样的 Brush 的情况下使用 DrawLine 不起作用。不需要在 OnPaint 事件中使用 BeginScene/EndScene。
  • 这不起作用 - 即使我使用 BeginScene/EndScene: with Canvas do begin BeginScene; Stroke.Color := TAlphaColors.Black;行程.厚度:= 2.0; DrawLine(PointF(10, 10), PointF(100, 10), 1);结束场景;结束;
猜你喜欢
  • 2013-09-21
  • 2021-04-04
  • 1970-01-01
  • 2014-07-19
  • 2018-01-29
  • 1970-01-01
  • 1970-01-01
  • 2018-12-14
  • 1970-01-01
相关资源
最近更新 更多