delphi - Delphi 中具有给定纵横比的 DrawFocusRect

标签 delphi aspect-ratio timage mouse-coordinates

我希望能够在图像上绘制 FocusRect,以保持图像的纵横比。我的问题是,FocusRect 仅取决于鼠标的 y 坐标。我只是不知道如何让矩形取决于两个鼠标坐标...... 这是我的代码:

procedure TForm1.AuswahlRechteck; //Due to this procedure it doesn't matter in which corner the rectangle begins
begin                                                                           
  Image1.Canvas.DrawFocusRect(Rect(X0,Y0,MX,MY));
  Image1.Canvas.DrawFocusRect(Rect(X0,MY,MX,Y0));
  Image1.Canvas.DrawFocusRect(Rect(MX,MY,X0,Y0));
  Image1.Canvas.DrawFocusRect(Rect(MX,Y0,X0,MY));
end;

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  X0:=X;
  MX:=X;
  Y0:=Y;
  MY:=Y;
  AuswahlRechteck;
  InMove:=true;
end;

procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if InMove then
  begin
    AuswahlRechteck;
    MY:=Y;
    MX:=X;
    if (((MX < X0) AND (MY > Y0)) OR ((MX > X0) AND (MY < Y0))) then MX:=Round(X0-((MY-Y0)*Image1.Width/Image1.Height))
    else MX:=Round(X0+((MY-Y0)*Image1.Width/Image1.Height));    
    AuswahlRechteck;
  end;
end;

有人可以帮我吗?

亨利

最佳答案

  private
    FSelecting: Boolean;
    FSelRect: TRect;
    FSelX: Integer;
    FSelY: Integer;
  end;

uses
  Math;

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  FSelX := X;
  FSelY := Y;
  FSelecting := True;
end;

procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  Scale: Single;
  W: Integer;
  H: Integer;
begin
  if FSelecting then
  begin
    Image1.Canvas.DrawFocusRect(FSelRect);
    Scale := Image1.Width / Image1.Height;
    W := X - FSelX;
    H := Y - FSelY;
    if (W <> 0) and (H <> 0) then
      if Abs(W) / Abs(H) > Scale then
        H := Round(Abs(W) / Scale) * Sign(H)
      else
        W := Round(Abs(H) * Scale) * Sign(W);
    FSelRect := Bounds(
      Min(FSelX, FSelX + W), Min(FSelY, FSelY + H), Abs(W), Abs(H));
    Image1.Canvas.DrawFocusRect(FSelRect);
  end;
end;

procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  FSelecting := False;
end;

关于delphi - Delphi 中具有给定纵横比的 DrawFocusRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7132153/

相关文章:

delphi - 是否可以在 RAD Studio 2009 中关闭 Unicode 支持?

delphi - 如何在Delphi中引入前端或启动浏览器

swift - 当另一个节点的子节点时,Spritekit 节点 zRotation 不保持节点的纵横比

CSS强制图像调整大小并保持纵横比

delphi - 为什么这个用于淡化 PNG 图像的 D2006 代码不起作用?

delphi - 获取 Timage 内位图的坐标

delphi - delphi中inline关键字有什么用

delphi - 使用公钥和 LockBox3 进行 RSA 加密

python - 如果其中一个轴的 set_aspectratio 为 "equal",则轴的大小相同

ios - FireMonkey iOS RAD Studio XE2 - 在从 URL 加载的表单上显示图像