FireMonkey Canvas.DrawLine on Windows XP
How can I draw a line ? This code does not display anything :
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.E开发者_Go百科ndScene;
Windows XP Service Pack 3 (tOsVersion.ToString is "Version 5.1, Build 2600, 32-bit Edition", Delphi XE2 update 1 installed)
You expect this to be easy - as I did. However it is not. These are early days for FireMonkey, and Embarcadero seem overwhelmed with the feedback.
When using the canvas directly on the TForm, you must accept that the result is volatile, i.e. it will disappear on the first repaint (resize, other windows overlapping etc.).
This works for me on several machines:
Create a new FM-HD project, add a button and a handler:
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;
Run, click button, and (hopefully): voila!
On a TImage Canvas, however, it is slightly more complicated (read:buggy ?)
Create a new project, this time two TButtons and a TImage - set (left,top) to something like (150,150) to distinguish its Canvas it from the Canvas of the TForm.
Add this code and assign to handlers (double click the form and the buttons):
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;
A final remark: once you start playing with the ScanLine property of the Bitmap, make sure that you do it OUTSIDE a BeginScend/EndScene section - and after you're done, make a "dummy" BeginScend/EndScene section to make sure that your changes are not lost :-( I might get back to this sometimes, if necessary ;o)
Good luck ! Carsten
精彩评论