Multiple Display Support

From RAD Studio
Jump to: navigation, search

Go Up to FireMonkey Application Design


FireMonkey automatically supports running an application on multiple displays. You do not need to implement any programming to support running a FireMonkey application of multiple displays. FireMonkey effectively manages the layout of the user interface of the application on multiple displays. For instance, menus, dialog boxes, and other pop-up controls open on the same display as the parent form. Multi-display support in FireMonkey framework is similar to multi-monitor support in VCL applications.

Notice that the TScreen class defines methods and properties providing access to parameters of multiple displays. Your application automatically creates a global Screen variable, of type TScreen. Screen encapsulates the state of the screen on which your application is running. Use Screen to obtain a list of displays (used to comprise the desktop) and coordinates, dimensions, and other properties of these displays. The DisplayCount property returns the number of displays and Displays provides an access to properties of all displays. All coordinates are virtual screen coordinates. Virtual screen coordinates are relative to the primary display and measured in pixels. The virtual screen is the bounding rectangle of all displays used to comprise the desktop.

For example, you can use code like this to retrieve parameters of multiple displays:

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Timer1: TTimer;
    procedure Timer1Timer(Sender: TObject);
  private
    procedure UpdateMemo;
    function RectToStr(R: TRect): String;
    function DispToStr(Disp: TDisplay): String;
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

function TForm1.RectToStr(R: TRect): String;
begin
  Result := '(' + Inttostr(R.Left) + ',' + Inttostr(R.Top) + ' x ' + Inttostr(R.Width) + ',' + Inttostr(R.Height) + ')';
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Screen.UpdateDisplayInformation;
  if (Screen.DisplayCount > 0) and (Screen.Displays[Screen.DisplayCount - 1].Index > 0) then
    Timer1.Tag := Timer1.Tag + 1;
  UpdateMemo;
end;

function TForm1.DispToStr(Disp: TDisplay): String;
begin
    Result := Inttostr(Disp.Index) + '. ';
    if Disp.Primary then
      Result := Result + ' Primary ';
    Result := Result + SLineBreak;
    Result := Result + 'WorkAreaRect: ' + RectToStr(Disp.WorkareaRect) + '; ';
    Result := Result + SLineBreak;
    Result := Result + 'BoundsRect: ' + RectToStr(Disp.BoundsRect) + '; ';
end;

procedure TForm1.UpdateMemo;
var
  I: Integer;
  P: TPoint;
begin
  Memo1.Lines.Clear;
  P := TPoint.Create(Round(Screen.MousePos.X), Round(Screen.MousePos.Y));
  Memo1.Lines.Add('Screen');
  Memo1.Lines.Add('MousePos: ' + FloatToStr(P.X) + ' ' + FloatToStr(P.Y));
  Memo1.Lines.Add('DisplayFromForm(Self).Index: ' + Inttostr(Screen.DisplayFromForm(Self).Index));
  Memo1.Lines.Add('Size.cx: ' + IntToStr(Screen.Size.cx));
  Memo1.Lines.Add('Size.cy: ' + IntToStr(Screen.Size.cy));
  Memo1.Lines.Add('DisplayCount: ' + Inttostr(Screen.DisplayCount));
  Memo1.Lines.Add('WorkAreaRect: ' + RectToStr(Screen.WorkAreaRect));
  Memo1.Lines.Add('DesktopRect: ' + RectToStr(Screen.DesktopRect));
  Memo1.Lines.Add('Displays');
  for I := 0 to Screen.DisplayCount - 1 do
    Memo1.Lines.Add(DispToStr(Screen.Displays[I]));
  Memo1.Lines.Add('Form');
  Memo1.Lines.Add('BoundsRect: ' + RectToStr(TRect.Create(TPoint.Create(Left, Top), Width, Height)));
end;
end.

If you run this code and move the application into the secondary display, the Memo1 can show a following output:

Screen
MousePos: 1530 294
DisplayFromForm(Self).Index: 1
Size.cx: 1280
Size.cy: 1024
DisplayCount: 2
WorkAreaRect: (0,0 x 1280,1024)
DesktopRect: (0,0 x 2560,1024)
Displays
0.  Primary
WorkAreaRect: (0,0 x 1280,1024);
BoundsRect: (0,0 x 1280,1024);
1.
WorkAreaRect: (1280,0 x 1280,1024);
BoundsRect: (1280,0 x 1280,1024);
Form
BoundsRect: (1472,199 x 648,507)

Notice that here we have highlighted in bold some parameters related to the secondary display.

See Also