Ich habe mit einer Schaltfläche „Öffnen“, die eine andere Form öffnet, wie dies eine Delphi 7 Hauptformular befindet:Multimonitor - Öffnen Sie das Anwendungsfenster auf dem Monitor in dem Mauszeiger
procedure TForm1.Button1Click(Sender: TObject);
begin
try
Application.CreateForm(TfrmPswd, frmPswd);
Application.NormalizeTopMosts;
Application.ProcessMessages;
frmPswd.ShowModal;
finally
frmPswd.Release;
frmPswd := nil;
end;
end;
Auf der frmPswd OnCreate Ereignis ich versuche, es zu zentralisieren, je nach Monitor, auf dem Mauszeiger befindet, wie folgt aus:
procedure TfrmPswd.FormCreate(Sender: TObject);
var
Monitor: TMonitor;
begin
Monitor := Screen.MonitorFromPoint(Mouse.CursorPos);
frmPswd.Top := Round((Monitor.Height - frmPswd.Height)/2);
frmPswd.Left := Round((Monitor.Width - frmPswd.Width)/2);
end;
Wenn das Hauptformular in demselben Monitor wie der Maus-Cursor befindet, öffnet sich frmPswd Form wie erwartet, in das Zentrum dieses Monitors. Aber wenn das Hauptformular in einem Monitor anders als Maus ist, erscheint frmPswd in einer seltsamen Position, die ich nicht verstehen kann, warum.
EDIT:
Hier sind die Ergebnisse wie von Remy Lebeau gefragt, auch mit dem neuen Code:
Monitor := Screen.MonitorFromPoint(Mouse.CursorPos);
Self.Left := Monitor.Left + ((Monitor.Width - Self.Width) div 2);
Self.Top := Monitor.Top + ((Monitor.Height - Self.Height) div 2);
Monitor 0
Top: 0
Left: 0
Width: 1440
Height: 900
Monitor 1
Top: -180
Left: -1920
Width: 1920
Height: 1080
frmPswd.Width = 200
frmPswd.Height = 200
Main form in Monitor 0 and Mouse cursor in Monitor 0
frmPswd.Top = 350
frmPswd.Left = 620
Main form in Monitor 1 and Mouse cursor in Monitor 1
frmPswd.Top = 260
frmPswd.Left = -1060
Main form in Monitor 0 and Mouse cursor in Monitor 1
frmPswd.Top = 440
frmPswd.Left = 860
Main form in Monitor 1 and Mouse cursor in Monitor 0
frmPswd.Top = 170
frmPswd.Left = -1300
Die anderen Monitor ist wahrscheinlich nicht bei (0, 0), dessen Offset hinzuzufügen. Also form.left = monitor.left + ..... –
Übrigens, es ist keine gute Idee, die Maus als bestimmenden Faktor für die Formularplatzierung zu verwenden. Sie können Ihr Programm mit der Tastatur über die Verknüpfung auf dem Hauptmonitor starten und sich fragen, wo das Anmeldeformular verschwunden ist. –
@SertacAkyuz, danke für die Antwort. Du hast recht, zweiter Monitor oben links ist nicht (0, 0). aber wenn frmPswd.Left: = Monitor.Left + Round ((Monitor.Width - frmPswd.Width)/2) Ich habe dasselbe Problem :( – Guybrush