在保持纵横比的同时调整表格大小

Mar*_*rks 7 forms delphi events

我有一个窗口,我在其中显示一张图片.我希望用户能够调整此窗口的大小,但保持它与图像的纵横比相同,因此窗口上不会出现大的空白区域.

我在OnResize事件中尝试的是这样的:

DragWidth := Width;
DragHeight := Height;

//Calculate corresponding size with aspect ratio
//...
//Calculated values are now in CalcWidth and CalcHeight

Width := CalcWidth;
Height := CalcHeight;
Run Code Online (Sandbox Code Playgroud)

问题是,在原始调整大小和计算值之间调整大小时窗口会闪烁,因为在调整大小已经完成(并绘制一次)之后调用OnResize事件.

你知道任何解决方案是否有平滑的宽高比调整大小?

谢谢你的帮助.

boi*_*eau 7

为OnCanResize事件添加以下处理程序似乎对我很有用:

procedure TForm1.FormCanResize(Sender: TObject; var NewWidth,
  NewHeight: Integer; var Resize: Boolean);
var
  AspectRatio:double;
begin
  AspectRatio:=Height/Width;
  NewHeight:=round(AspectRatio*NewWidth);
end;
Run Code Online (Sandbox Code Playgroud)

您当然可以想要更改计算NewHeight和NewWidth的方法.当我在空白表单上尝试时,以下方法感觉直观"正确":

  NewHeight:=round(0.5*(NewHeight+AspectRatio*NewWidth));
  NewWidth:=round(NewHeight/AspectRatio);
Run Code Online (Sandbox Code Playgroud)

  • 你可能已经弄明白了,但对于其他感兴趣的人,我建议保持ClientWidth和ClientHeight的宽高比不变,而不是我上面的方式.这当然会有点复杂. (2认同)