3

どうすれば線を引くことができますか? このコードは何も表示しません:

var my_point_1, my_point_2: tPointF;

Canvas.Stroke.Color := claBlue;
Canvas.Stroke.Kind:= tBrushKind.bkSolid;

my_point_1.X:= 100;
my_point_1.Y:= 100;
my_point_2.X:= 120;
my_point_2.Y:= 150;

Canvas.BeginScene;
Canvas.DrawLine(my_point_1, my_point_2, 1.0);
Canvas.EndScene;

Windows XP Service Pack 3 (tOsVersion.ToString は「バージョン 5.1、ビルド 2600、32 ビット版」、Delphi XE2 update 1 インストール済み)

4

1 に答える 1

3

私がしたように、これは簡単だと思います。しかし、そうではありません。FireMonkey はまだ始まったばかりで、Embarcadero はフィードバックに圧倒されているようです。

キャンバスを TForm で直接使用する場合、結果が揮発性であることを受け入れる必要があります。つまり、最初の再描画 (サイズ変更、他のウィンドウのオーバーラップなど) で消えます。

これは、いくつかのマシンで機能します。

新しい FM-HD プロジェクトを作成し、ボタンとハンドラーを追加します。

procedure TForm1.Button1Click(Sender: TObject);
var pt0,pt1 : TPointF;
begin
  pt0.Create(0,0);
  pt1.Create(100,50);
  Canvas.BeginScene;
  Canvas.DrawLine(pt0,pt1,1);
  Canvas.EndScene;
end;

実行し、ボタンをクリックすると、(できれば) できあがり!

ただし、TImage Canvas では、もう少し複雑です (読み取り: バグ?)。

新しいプロジェクトを作成します。今回は 2 つの TButton と TImage を作成します。(left,top) を (150,150) のような値に設定して、Canvas と TForm の Canvas を区別します。

このコードを追加してハンドラーに割り当てます (フォームとボタンをダブルクリックします):

procedure TForm1.FormCreate(Sender: TObject);
begin
  // Without this, you normally get a runtime exception in the Button1 handler
  Image1.Bitmap := TBitmap.Create(150,150);
end;

procedure TForm1.Button1Click(Sender: TObject);
var pt0,pt1 : TPointF;
begin
  pt0.Create(0,100);
  pt1.Create(50,0);
  with Image1.Bitmap do begin
    Canvas.BeginScene;
    Canvas.DrawLine(pt0,pt1,1);
    BitmapChanged;  // without this, no output 
    Canvas.EndScene;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
// This demonstrates that if you try to access the Canvas of the TImage object (and NOT its bitmap)
// you are sometimes defaulted to the Canvas of the Form (on some configurations you get the line directly on the form).
var pt0,pt1 : TPointF;
begin
  pt0.Create(0,100);
  pt1.Create(50,0);
  with Image1 do begin
    Canvas.BeginScene;
    Canvas.DrawLine(pt0,pt1,1);
    Canvas.EndScene;
  end;
end;

最後の注意: Bitmap の ScanLine プロパティを使い始めたら、BeginScend/EndScene セクションの外でそれを行うようにしてください。完了したら、「ダミーの」BeginScend/EndScene セクションを作成して、変更は失われません:-(必要に応じて、時々これに戻るかもしれません;o)

幸運を !カーステン

于 2011-11-13T19:33:31.203 に答える