微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

delphi – 调整表单大小并保持宽高比

类似的问题: Resize Form while keeping aspect ratio

基本上,我想要的是调整窗体大小并保持其宽高比,但我也希望调整大小以跟随光标.上面主题中的答案提供了半满意的解决方案 – 它可以工作,但调整大小比它应该慢2倍.当我开始通过X轴调整表单大小时,您可以看到光标的位置以及表单大小:

我认为,因为它调整速度慢了2倍,我应该省略代码中的0.5乘数,它会工作,但没有骰子.这是我目前正在使用的代码

type
  TfrmTable = class(TForm)
    procedure FormCanResize(Sender: TObject; var NewWidth,NewHeight: Integer; var Resize: Boolean);
    procedure FormCreate(Sender: TObject);
  private
    FAspectRatio: Double;
  public
  end;

var
  frmTable: TfrmTable;

implementation

{$R *.dfm}

procedure TfrmTable.FormCreate(Sender: TObject);
begin
  FAspectRatio := Width / Height;
end;

procedure TfrmTable.FormCanResize(Sender: TObject; var NewWidth,NewHeight: Integer; var Resize: Boolean);
begin
  NewHeight := Round(0.50 * (NewHeight + NewWidth / FAspectRatio));
  NewWidth := Round(NewHeight * FAspectRatio);
end;

我尝试了另一种方法,使用这样的方法

procedure TfrmTable.FormCanResize(Sender: TObject; var NewWidth,NewHeight: Integer; var Resize: Boolean);
begin
  if NewWidth <> Width then
    NewHeight := Round(NewWidth / FAspectRatio)
  else
    if NewHeight <> Height then
      NewWidth := Round(NewHeight * FAspectRatio);
end;

这该怎么办?好吧,我的想法是我首先检查NewWidth是否与当前宽度不同,如果是,则表示用户正在通过X轴调整表格大小.然后我应该将NewHeight设置为适当的值.否则,我检查NewHeight是否与当前高度不同,并将NewWidth值设置为其适当的值.这也会产生奇怪的结果,当我通过X轴拖动窗体时,它似乎工作,并且一旦我停止调整大小,窗体返回到其原始大小 – 我得出结论,一旦我停止调整大小(让鼠标按钮向上),FormCanResize( )使用旧的NewHeight值调用事件,然后将表单恢复为旧的大小.

解决方法

处理此问题的正确消息是WM_SIZING:

By processing this message,an application can monitor the size and
position of the drag rectangle and,if needed,change its size or
position.

procedure TForm1.WMSizing(var Message: TMessage);
begin
  case Message.wParam of
    WMSZ_LEFT,WMSZ_RIGHT,WMSZ_BottOMLEFT:
      with PRect(Message.LParam)^ do
        Bottom := Top + Round((Right-Left)/FAspectRatio);
    WMSZ_TOP,WMSZ_BottOM,WMSZ_TOPRIGHT,WMSZ_BottomrIGHT:
      with PRect(Message.LParam)^ do
        Right := Left + Round((Bottom-Top)*FAspectRatio);
    WMSZ_TOPLEFT:
      with PRect(Message.LParam)^ do
        Top := Bottom - Round((Right-Left)/FAspectRatio);
  end;
  inherited;
end;

原文地址:https://www.jb51.cc/delphi/101817.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐