Geosucher
Geosucher

Reputation: 107

Delphi 2007 Position of modal form for system with two monitors

I use Delphi 2007 with IdeFixPack2007REg44Win10. I have two monitors (27", 2560 x 1440).

I create a test program with one Button. The button click open a new modal form. I can move the form on desktop and close the form. For the second button click, I expect the form at the position where I closed the form. For the main monitor all looks fine.

If I moved the form to the second monitor, I allways get the reopened form on main monitor.

If I moved the form in the area of both monitors (with the center on left monitor), I get a reopened form with a negative left value (left monitor; left part of form outside).

I found the reason in Forms.pas. In TCustomForm.SetVisible I found the procedure SetWindowToMonitor.

In this procedure the left position of form will be computed by:

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

in this the first monitor is default monitor (main form) and the second is form monitor. For my example I get ALeft := 0 + 2385 - 2560 ( -175)

My Source code:

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

Unit2.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

If someone can help me, it will be fine. I want to reopen the form on the position where I closed the form.

Thanks! Roland

Upvotes: 0

Views: 576

Answers (1)

Geosucher
Geosucher

Reputation: 107

I found a solution for my problem. The TForm2 object is child of TForm. The default-Value of DefaultMonitor is there dmActiveForm (means the form will be opened on the same monitor like parent). Setting the DefaultMonitor-Property to dmDesktop doesn't change the position of form.

Upvotes: 1

Related Questions