delphi - 使用 Delphi Firemonekey 在 TImage 的 Canvas 上绘制一个圆圈

标签 delphi firemonkey

我正在尝试使用 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 时,圆圈按预期绘制,但不是在准确的位置,它与鼠标指针有一定的偏移。 我怎样才能纠正这个位移?

enter image description here

最佳答案

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/

相关文章:

multithreading - 线程向主 UI 线程发布消息被阻止/删除

delphi - 如何注销事件日志源?

delphi - 如何为每个 TEdit 设置特定的文本?

macos - 在 OSX 上使用 FireMonkey 播放音频文件

德尔福错误: "Cannot focus a disabled or invisible window"

java - Delphi 到 Java 代码转换 : image. canvas.pixels

delphi - ElevateDB 关系模型的注意事项

c++ - InterBase ToGo 试用许可证不起作用

ios - FireMonkey,IOS FMX.Listview

ios - 如何让 TListBoxItem 的 ADetail 附件突出显示当前 TListBoxItem