zac
zac

Reputation: 4918

How to make main form appear in 2nd monitor

I use Delphi 10 and I have two monitors when I create default vcl application and run the application the main form always appear in the first monitor is there a way to make it appear in the second monitor by default ?

This may be option in the IDE or property or code

Thanks

Upvotes: 4

Views: 6331

Answers (2)

Rafael Passarela
Rafael Passarela

Reputation: 156

I've two monitor of 1600px width. My primary monitor is on the right, so, for place a form on the left I've to pass a negative value for his Left property.

procedure TForm1.Button1Click(Sender: TObject);
begin
  // (3200 / 2 = + 1600) * -1 = -1600
  Left := (Screen.DesktopWidth div 2) * -1;
end;

The result is -1600 that means the most to the left of my secondary monitor.

You also can get the "most Left" position of each monitor by calling Screen.Monitors[i].Left, something like this code:

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
  lMens: string;
begin
  lMens := '';
  for I := 0 to Screen.MonitorCount - 1 do
  begin
    lMens := lMens + ' | '
             + Format('%d - Left = %d', [i, Screen.Monitors[i].Left])
  end;
  ShowMessage(lMens);
end;

I hope that help's you.

Upvotes: 2

Remy Lebeau
Remy Lebeau

Reputation: 598279

TForm has a DefaultMonitor property that is set to dmActiveForm by default. When no Form is active, the primary monitor is used. However, there is no way to set the DefaultMonitor to the second monitor specifically.

TForm also has a Monitor property, but for whatever reason it is read-only.

So, to display a TForm on a specific monitor, you can locate the desired monitor in the global TScreen.Monitors[] list, and then either:

  1. manually set the Form's Left/Top properties to an X/Y coordinate that is within the bounds of the monitor's BoundsRect or WorkareaRect property.

  2. pass the monitor to the Form's public MakeFullyVisible() method.

Upvotes: 12

Related Questions