我有一个窗口,在其中显示图片。我希望用户能够调整此窗口的大小,但是 保持其与图像的宽高比相同,这样窗口上就不会出现大的空白区域。
我在 OnResize 事件中尝试过类似的操作:
DragWidth := Width;
DragHeight := Height;
//Calculate corresponding size with aspect ratio
//...
//Calculated values are now in CalcWidth and CalcHeight
Width := CalcWidth;
Height := CalcHeight;
问题是,在原始调整大小和计算结果之间拖动调整大小期间,窗口会闪烁,因为 OnResize 事件是在调整大小已经完成(并绘制一次)之后调用的。
您知道平滑纵横比调整大小的解决方案吗?
感谢您的帮助。
最佳答案
为 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;
您当然可能想要更改计算 NewHeight 和 NewWidth 的方法。当我在空白表单上尝试时,以下方法直观地感觉“正确”:
NewHeight:=round(0.5*(NewHeight+AspectRatio*NewWidth));
NewWidth:=round(NewHeight/AspectRatio);
关于delphi - 调整表单大小,同时保持纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6265746/