Handling the Screen

From RAD Studio
Jump to: navigation, search

Go Up to Controlling Application Behavior

A global variable Screen of type TScreen is created when you create a project. Screen encapsulates the state of the screen on which your application is running. Common tasks performed by Screen include:

If your VCL application runs on multiple monitors, VCL effectively manages the layout of the application's user interface on multiple monitors. For instance, menus, dialog boxes, and other pop-up controls open on the same monitor as the parent form. Use the Screen variable to obtain a list of monitors and their dimensions. The Monitors property provides an access to all monitors used to comprise the desktop, and MonitorCount returns the number of monitors. All coordinates are relative to the primary monitor. For example, one can obtain the following information:

procedure TForm2.Button1Click(Sender: TObject);
  I : integer;
  ScreenCount : integer;
  P: TPoint;
  ScreenCount := Screen.MonitorCount;
  P := Mouse.CursorPos;
  Memo1.Lines.Add('Monitor number: ' + IntToStr(ScreenCount) );
  for I := 0 to ScreenCount-1 do begin
    Memo1.Lines.Add('Screen.Width: ' + IntToStr(Screen.Width));
    Memo1.Lines.Add('Monitor: '+IntToStr(Screen.Monitors[I].MonitorNum) );
    Memo1.Lines.Add('Top: '+IntToStr(Screen.Monitors[I].Top) );
    Memo1.Lines.Add('Left: '+IntToStr(Screen.Monitors[I].Left) );
    Memo1.Lines.Add('Width: '+IntToStr(Screen.Monitors[I].Width) );
    Memo1.Lines.Add('Height: '+IntToStr(Screen.Monitors[I].Height) );
    Memo1.Lines.Add('MonitorFromRect(mdNearest): '
      + IntToStr( (Screen.MonitorFromRect(
           TRect.Create(P,100,100), mdNearest)).MonitorNum) );
    Memo1.Lines.Add( 'Mouse.CursorPos: ' + IntToStr(P.X) 
      + ' ' + IntToStr(P.Y) );

See Also