首页 > 解决方案 > Delphi 2007 带两个显示器的系统的模态表单位置

问题描述

我将 Delphi 2007 与 IdeFixPack2007REg44Win10 一起使用。我有两台显示器(27 英寸,2560 x 1440)。

我用一个按钮创建了一个测试程序。单击按钮打开一个新的模态表单。我可以在桌面上移动表格并关闭表格。对于第二个按钮单击,我希望表单位于我关闭表单的位置。对于主显示器,一切看起来都很好。

如果我将表单移动到第二个监视器,我总是会在主监视器上重新打开表单。

如果我在两个监视器的区域中移动表单(左侧监视器的中心),我会得到一个重新打开的表单,其左侧值为负(左侧监视器;表单的左侧部分在外面)。

我在 Forms.pas 中找到了原因。在 TCustomForm.SetVisible 中,我找到了 SetWindowToMonitor 过程。

在此过程中,表格的左侧位置将通过以下方式计算:

ALeft := Screen.Monitors[i].Left + Left - Screen.Monitors[j].Left

在此第一个监视器是默认监视器(主窗体),第二个是表单监视器。对于我的示例,我得到 ALeft := 0 + 2385 - 2560 ( -175)

我的源代码:

TestFormPos_Main.pas:

    unit TestFormPos_Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, LowForm, Unit2;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
    x,y,h,b : Integer;
  end;

var Form1   : TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
 Memo1.Lines.Add('Form - Left  : ' + chr(9) + IntToStr(Form2.Left));
 Memo1.Lines.Add('Form - Top   : ' + chr(9) + IntToStr(Form2.Top));
 Memo1.Lines.Add('Form - Width : ' + chr(9) + IntToStr(Form2.Width));
 Memo1.Lines.Add('Form - Height: ' + chr(9) + IntToStr(Form2.Height));
 Memo1.Lines.Add('');
 Form2.ShowModal;
 Memo1.Lines.Add(' *** Form- Left   : ' + chr(9) + IntToStr(Form2.Left));
 Memo1.Lines.Add(' *** Form- Top    : ' + chr(9) + IntToStr(Form2.Top));
 Memo1.Lines.Add(' *** Form- Width  : ' + chr(9) + IntToStr(Form2.Width));
 Memo1.Lines.Add(' *** Form- Height : ' + chr(9) + IntToStr(Form2.Height));
 Memo1.Lines.Add('');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 x := 100;
 y := 80;
 b := 850;
 h := 660;
end;

end.

TestFormPos_Main.dfm:

   object Form1: TForm1
      Left = 0
      Top = 0
      Caption = 'Testform'
      ClientHeight = 422
      ClientWidth = 852
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -12
      Font.Name = 'Tahoma'
      Font.Style = []
      OldCreateOrder = False
      OnCreate = FormCreate
      PixelsPerInch = 106
      TextHeight = 14
      object Button1: TButton
        Left = 322
        Top = 367
        Width = 157
        Height = 47
        Caption = 'Open Form (modal)'
        TabOrder = 0
        OnClick = Button1Click
      end
      object Memo1: TMemo
        Left = 8
        Top = 6
        Width = 839
        Height = 347
        TabOrder = 1
      end
    end

单元2.pas:

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, jpeg, ExtCtrls, Grids;

    type
  TForm2 = class(TForm)
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

end.

unit2.dfm:

object Form2: TForm2
  Left = 0
  Top = 0
  Caption = 'FS-Tabellen'
  ClientHeight = 325
  ClientWidth = 666
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -12
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 106
  TextHeight = 14
end

如果有人可以帮助我,那就好了。我想在我关闭表单的位置重新打开表单。

谢谢!罗兰

标签: formspositiondelphi-2007multiple-monitors

解决方案


我找到了解决我的问题的方法。TForm2 对象是 TForm 的子对象。DefaultMonitor 的默认值是 dmActiveForm (意味着表单将在与父级相同的监视器上打开)。将 DefaultMonitor-Property 设置为 dmDesktop 不会更改表单的位置。


推荐阅读