Handling the Screen
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:
- Handling the list and parameters of multiple monitors accessible by the application.
- The size of the window in which your application is running.
- A list of fonts available to the screen device.
- The look of the cursor.
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);
var
I : integer;
ScreenCount : integer;
P: TPoint;
begin
Memo1.Clear;
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) );
end;
end;