Einzelnen Beitrag anzeigen

rokli

Registriert seit: 21. Mär 2009
Ort: Rödinghausen
301 Beiträge
 
Delphi 10.4 Sydney
 
#22

AW: Fensterposition sowie -größe auf zweitem Monitor speichern und laden

  Alt 23. Apr 2018, 11:11
Unabhängig von der Anzahl der Monitore verwende ich schon sehr lange die folgenden INIs:

Beim Programmstart:

Delphi-Quellcode:
procedure p_IniRead(AForm : TCustomForm);
// INI Datei einlesen und Fensterpositionen bestimmen
var
   Ini         : TIniFile;
   tmpFormName : string;
begin
   Ini          := TIniFile.Create(ChangeFileExt( Application.ExeName, '.INI' ) );
   try
      tmpFormName      := AForm.Name;
      AForm.Top      := Ini.ReadInteger(tmpFormName, 'Top', AForm.Top );
      AForm.Left      := Ini.ReadInteger(tmpFormName, 'Left', AForm.Left );
      AForm.Height   := Ini.ReadInteger(tmpFormName, 'Height', AForm.Height);
      AForm.Width      := Ini.ReadInteger(tmpFormName, 'Width', AForm.Width);
      if Ini.ReadBool( tmpFormName, 'InitMax', false ) then
         AForm.WindowState := wsMaximized
      else
         AForm.WindowState := wsNormal;
   finally
      Ini.Destroy;
   end;

   if Screen.MonitorFromWindow(aForm.Handle, mdNull) = nil then begin
      // Form is outside of any monitor. Move to center of main monitor
      aForm.Top      := (Screen.Monitors[0].Height - aForm.Height) div 2;
      aForm.Left      := (Screen.Monitors[0].Width - aForm.Width) div 2;
   end;

end;
und wenn Fenster geschlossen werden, speichern der Fensterpositionen:

Delphi-Quellcode:
procedure TFrmMain.p_IniFormPosRead(AForm : TCustomForm);
// Position der Form merken
var
   Ini         : TIniFile;
   tmpFormName : string;
begin
   Ini          := TIniFile.Create(ChangeFileExt( Application.ExeName, '.INI' ) );
   try
      tmpFormName      := AForm.Name;
      AForm.Top      := Ini.ReadInteger(tmpFormName, 'Top', AForm.Top );
      AForm.Left      := Ini.ReadInteger(tmpFormName, 'Left', AForm.Left );
      AForm.Height   := Ini.ReadInteger(tmpFormName, 'Height', AForm.Height);
      AForm.Width      := Ini.ReadInteger(tmpFormName, 'Width', AForm.Width);
      if Ini.ReadBool( tmpFormName, 'InitMax', false ) then
         AForm.WindowState := wsMaximized
      else
         AForm.WindowState := wsNormal;
   finally
      Ini.Destroy;
   end;
Und durch die Weitergabe der Form klappt das auch mit beliebigen Fenstern in einem Programm. Und das Handling, wenn mal nur ein Monitor vorhanden ist, ist auch gleich enthalten (wurde aber oben ja auch schon gezeigt).

Gruß
Rolf
wenn nicht anders angegeben, schreibe ich zu D7, XE2 und MS SQL - ansonsten fragen Sie ihren Administrator oder einen Operator. Update 06/2020: Delphi 10.4 Sydney
  Mit Zitat antworten Zitat