Run Your Delphi Application in Full Screen – Implement “F11 – Full Screen”

F11 Full screen modeI guess you know you can run your browser in full screen mode using the F11 shortcut key. Windows Explorer also supports this feature.

Running in full screen, where an application UI covers the entire screen, over the TaskBar and any Desktop/Tool bars, is handy when a user has a limited screen size (laptops) or when you just want more to be visible by the browser or the Windows Explorer.

Running your application in full screen might also be handy if your users want to be focused only on your application’s window.

F11 – Full Screen

Full screen means “full screen” – where the UI of the application gets “on top” of the TaskBar or any other tool bars – the entire UI of your application covers Windows desktop (if multiple monitors: monitor where your app is).

If you have a main menu in your application you can add a menu item “full screen” and have it assigned the “F11” key for Shortcut property.

Here’s how to allow switching to the full screen view and back to the “normal” view – taking into account what “normal” was before your application went to the “full screen” mode.

{$J+} //writeable constants on
const
  rect: TRect = (Left:0; Top:0; Right:0; Bottom:0);
  ws : TWindowState = wsNormal;
{$J-} //writeable constants off
var
  r : TRect;
begin
  if BorderStyle <> bsNone then
  begin
    ws := WindowState;
    rect := BoundsRect;
    BorderStyle := bsNone;
    r := Screen.MonitorFromWindow(Handle).BoundsRect;
    SetBounds(r.Left, r.Top, r.Right-r.Left, r.Bottom-r.Top) ;
  end
  else
  begin
    BorderStyle := bsSizeable;
    if ws = wsMaximized then
      WindowState := wsMaximized
    else
      SetBounds(rect.Left, rect.Top, rect.Right-rect.Left, rect.Bottom-rect.Top) ;
  end;
end;

The code above presumes that when not in full screen mode your main window has a border – i.e. the BorderStyle is not bsNone.

To get the size of the full screen the MonitorFromWindow method of the Screen global variable is used. MonitorFromWindow retrieves the Monitor (TMonitor) that has the largest area of intersection with the bounding rectangle of a specified window (your main form in this case).

The BoundsRect property gives the dimensions of the monitor in pixels, where (0,0) represents the top-left corner of the monitor. The BoundsRect property does not take into account any task bars or tool bars docked on the monitor.

By using the “{$J+}” and “{$J-}” the typed constants declared in the code (“rect” and “ws”) are made writable – their value will not get lost when the above code is called several times.

Therefore: if the BorderStyle is not bsNone we presume that we are not in full screen mode. To enter the full screen mode, we set the BorderStyle property to bsNone and also use SetBounds to set the Left, Top, Width, and Height properties of the main form all at once.

We also store the current bounds of the main window along with the WindowStyle – when going back from the full screen to restore the last state of the UI of the main application window.

Note: To fill the usable area (not hiding the TaskBar) you can use

var
   r : TRect;
begin
   Borderstyle := bsNone;
   SystemParametersInfo(SPI_GETWORKAREA, 0, @r,0) ;
   SetBounds(r.Left, r.Top, r.Right-r.Left, r.Bottom-r.Top) ;
end;

That’s it. Go full screen!

5 thoughts on “Run Your Delphi Application in Full Screen – Implement “F11 – Full Screen”

  1. Remy Lebeau

    Instead of storing the VCL’s BoundsRect and WindowState values, consider using the Win32 API (Get|Set)WindowPlacement() functions instead. In addition to handling the above values, they also handle the window’s position+size in Minimized, Maximized, and Restored states.

    Reply
  2. Rudy Velthuis

    FWIW, instead of using “writeable consts”, you can simply use var. Global variables can be initialised, even TRect, or WINDOWPLACEMENT records. So simply use var instead of const.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.