Delphi 在运行时更改模式窗体的位置

Delphi 在运行时更改模式窗体的位置,delphi,delphi-2007,Delphi,Delphi 2007,我的Delphi2007应用程序中有一个模式表单。我已经在表单上应用了MinHeight、MaxHeight、MinWidth和MaxWidth约束 如果屏幕分辨率低于最小/最大限制,我想根据屏幕分辨率重新调整窗体的大小和位置 procedure TfrmMyForm.FormCreate(Sender: TObject); var WorkArea: TRect; iTitleHeight: Integer; begin inherited; //---------

我的Delphi2007应用程序中有一个模式表单。我已经在表单上应用了MinHeight、MaxHeight、MinWidth和MaxWidth约束

如果屏幕分辨率低于最小/最大限制,我想根据屏幕分辨率重新调整窗体的大小和位置

procedure TfrmMyForm.FormCreate(Sender: TObject);
var      
  WorkArea: TRect;
  iTitleHeight: Integer;
begin
  inherited;
  //-------------------------------------------------------------------
  //Adjust Height/Width of the form if min/max values 
  //don't fall under the current resolution.
  iTitleHeight := GetSystemMetrics(SM_CYCAPTION); //Height of titlebar
  WorkArea := Screen.WorkAreaRect;

  if(Self.Constraints.MinWidth > WorkArea.BottomRight.X) then
  begin
    if(Self.Constraints.MaxWidth > WorkArea.BottomRight.X) then
    begin
      Self.Constraints.MinWidth := WorkArea.BottomRight.X;
      Self.Constraints.MaxWidth := WorkArea.BottomRight.X;
      Self.Position := poDesigned;
      SetBounds(0,0,WorkArea.BottomRight.X, WorkArea.BottomRight.Y - 5);
    end
    else
    begin
      Self.Constraints.MinWidth := WorkArea.BottomRight.X;
    end;
  end;
  if(Self.Constraints.MinHeight > WorkArea.BottomRight.Y) then
  begin
    if(Self.Constraints.MaxHeight > WorkArea.BottomRight.Y) then
    begin
      Self.Constraints.MinHeight := WorkArea.BottomRight.Y - iTitleHeight;
      Self.Constraints.MaxHeight := WorkArea.BottomRight.Y;
      Self.Position := poDesigned;
      SetBounds(0,0,WorkArea.BottomRight.X, WorkArea.BottomRight.Y -  5);
    end
    else
    begin
      Self.Constraints.MinHeight := WorkArea.BottomRight.Y;
    end;
  end;
//-------------------------------------------------------------------
end;
我已经在OnCreate事件处理函数中编写了以下代码来处理屏幕分辨率

procedure TfrmMyForm.FormCreate(Sender: TObject);
var      
  WorkArea: TRect;
  iTitleHeight: Integer;
begin
  inherited;
  //-------------------------------------------------------------------
  //Adjust Height/Width of the form if min/max values 
  //don't fall under the current resolution.
  iTitleHeight := GetSystemMetrics(SM_CYCAPTION); //Height of titlebar
  WorkArea := Screen.WorkAreaRect;

  if(Self.Constraints.MinWidth > WorkArea.BottomRight.X) then
  begin
    if(Self.Constraints.MaxWidth > WorkArea.BottomRight.X) then
    begin
      Self.Constraints.MinWidth := WorkArea.BottomRight.X;
      Self.Constraints.MaxWidth := WorkArea.BottomRight.X;
      Self.Position := poDesigned;
      SetBounds(0,0,WorkArea.BottomRight.X, WorkArea.BottomRight.Y - 5);
    end
    else
    begin
      Self.Constraints.MinWidth := WorkArea.BottomRight.X;
    end;
  end;
  if(Self.Constraints.MinHeight > WorkArea.BottomRight.Y) then
  begin
    if(Self.Constraints.MaxHeight > WorkArea.BottomRight.Y) then
    begin
      Self.Constraints.MinHeight := WorkArea.BottomRight.Y - iTitleHeight;
      Self.Constraints.MaxHeight := WorkArea.BottomRight.Y;
      Self.Position := poDesigned;
      SetBounds(0,0,WorkArea.BottomRight.X, WorkArea.BottomRight.Y -  5);
    end
    else
    begin
      Self.Constraints.MinHeight := WorkArea.BottomRight.Y;
    end;
  end;
//-------------------------------------------------------------------
end;
在设计时,我将Position属性设置如下

Position := poCentreScreen
现在,当屏幕分辨率较低(如1024x768)且最小和最大高度/宽度约束值高于此分辨率时,我面临三个问题

procedure TfrmMyForm.FormCreate(Sender: TObject);
var      
  WorkArea: TRect;
  iTitleHeight: Integer;
begin
  inherited;
  //-------------------------------------------------------------------
  //Adjust Height/Width of the form if min/max values 
  //don't fall under the current resolution.
  iTitleHeight := GetSystemMetrics(SM_CYCAPTION); //Height of titlebar
  WorkArea := Screen.WorkAreaRect;

  if(Self.Constraints.MinWidth > WorkArea.BottomRight.X) then
  begin
    if(Self.Constraints.MaxWidth > WorkArea.BottomRight.X) then
    begin
      Self.Constraints.MinWidth := WorkArea.BottomRight.X;
      Self.Constraints.MaxWidth := WorkArea.BottomRight.X;
      Self.Position := poDesigned;
      SetBounds(0,0,WorkArea.BottomRight.X, WorkArea.BottomRight.Y - 5);
    end
    else
    begin
      Self.Constraints.MinWidth := WorkArea.BottomRight.X;
    end;
  end;
  if(Self.Constraints.MinHeight > WorkArea.BottomRight.Y) then
  begin
    if(Self.Constraints.MaxHeight > WorkArea.BottomRight.Y) then
    begin
      Self.Constraints.MinHeight := WorkArea.BottomRight.Y - iTitleHeight;
      Self.Constraints.MaxHeight := WorkArea.BottomRight.Y;
      Self.Position := poDesigned;
      SetBounds(0,0,WorkArea.BottomRight.X, WorkArea.BottomRight.Y -  5);
    end
    else
    begin
      Self.Constraints.MinHeight := WorkArea.BottomRight.Y;
    end;
  end;
//-------------------------------------------------------------------
end;
我将Position属性更改为poDesigned,因为否则表单不会移动到我想要的位置

当我在有双监视器的系统上运行我的应用程序时,它的行为出乎意料

我还在表单的系统菜单中添加了一个自定义菜单项,当我将Position属性更改为OnCreate函数中的poDesigned时,它会重置系统菜单并删除自定义菜单项

是否有任何方法可以在不修改position属性的情况下重新调整表单的大小和位置


感谢您的期待。

将位置设置为poDesigned,然后保持该状态。显示表单中心时,请手动将其显示在屏幕上。谢谢Sertac。你的建议似乎是一个很好的解决办法。