Multimonitor - Open the application window in the monitor where mouse cursor is located

Viewed 1263

I have a Delphi 7 main form with an "open" button, that opens another form, just like this:

procedure TForm1.Button1Click(Sender: TObject);
begin
  try
    Application.CreateForm(TfrmPswd, frmPswd);
    Application.NormalizeTopMosts;
    Application.ProcessMessages;
    frmPswd.ShowModal;
  finally
    frmPswd.Release;
    frmPswd := nil;
  end;
end;

On the frmPswd OnCreate event I am trying to centralize it, depending on monitor where mouse cursor is located, like this:

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;

When the main form is located in the same monitor as the mouse cursor, frmPswd form opens like expected, in the center of that monitor. But when the main form is in a monitor different from mouse, frmPswd appears in a strange position I can't understand why.

EDIT:

Here are the results as asked by Remy Lebeau, even with the new 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
1 Answers
Related