我正在尝试使用 TImage 组件的 OnMouseDown 事件在 Delphi 11 Firemonkey 中的 TImage Canvas 上绘制一个圆圈。 我编写了以下代码:
procedure TForm1.FormCreate(Sender: TObject);
begin
// sets the size of the TBitmap
Image1.Bitmap.SetSize(Round(Image1.Width), Round(Image1.Height));
Image1.Bitmap.Clear(TAlphaColors.White);
end;
procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
var
MyRect: TRectF;
Point:TPointF;
begin
// sets the circumscribed rectangle of the ellipse
Caption:=X.ToString +',' +Y.ToString;
Point:=TPointF.Create(x,y);
//Point:=ClientToScreen(Point);
Point:=Image1.AbsoluteToLocal(Point);
MyRect := TRectF.Create(Point,5,5);
// draws the ellipse on the canvas
Image1.Bitmap.Canvas.BeginScene;
Image1.Bitmap.Canvas.DrawEllipse(MyRect, 20);
Image1.Bitmap.Canvas.EndScene;
end;
当我运行应用程序并单击 Timage 时,圆圈按预期绘制,但不是在准确的位置,它与鼠标指针有一定的偏移。 我怎样才能纠正这个位移?
最佳答案
OnMouseDown
事件提供的 X、Y 坐标表示为相对于 TImage
工作区的局部坐标,而不表示为全局绝对坐标。因此,您根本不应该尝试将 Point
从绝对坐标转换为局部坐标,它已经在局部坐标中。
您需要删除对 Image1.AbsoluteToLocal()
的调用,并仅使用按原样提供的坐标,例如:
procedure TForm1.Image1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Single);
var
MyRect: TRectF;
Point: TPointF;
begin
// sets the circumscribed rectangle of the ellipse
Caption := X.ToString + ',' + Y.ToString;
Point := TPointF.Create(X, Y);
MyRect := TRectF.Create(Point, 5, 5);
// draws the ellipse on the canvas
Image1.Bitmap.Canvas.BeginScene;
Image1.Bitmap.Canvas.DrawEllipse(MyRect, 20);
Image1.Bitmap.Canvas.EndScene;
end;
关于delphi - 使用 Delphi Firemonekey 在 TImage 的 Canvas 上绘制一个圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72218195/