Difference between revisions of "Application full screen mode"

From Lazarus wiki
Jump to navigationJump to search
 
(16 intermediate revisions by 4 users not shown)
Line 3: Line 3:
 
==Introduction==
 
==Introduction==
  
Some applications like some web browsers allow show in full screen mode using key {{keypress|F11}}. In full screen mode window haven't border and title bar and use entire screen space. Task bar is hidden too.
+
Some applications like some web browsers allow show in full screen mode using key {{keypress|F11}}. In full screen mode window hasn't border and uses entire screen space.
  
 +
==Standard way==
  
To be able to switch to fullscreen mode you will need:
+
You can switch form to full screen mode simply by assigning '''wsFullScreen''' value to '''TForm.WindowState''' property. You can do that directly in design time in Object Inspector window or in runtime:
 +
 
 +
<syntaxhighlight lang=pascal>
 +
procedure TForm1.FormShow(Sender: TObject);
 +
begin
 +
  WindowState := wsFullScreen;
 +
end;
 +
</syntaxhighlight>
 +
 
 +
==Universal way to use full-screen==
 +
 
 +
Universal way exists, using LCL, you need to call ShowWindow() with SW_SHOWFULLSCREEN. This ShowWindow call is handled inside widgetset, using low-level API (for example, OS X Carbon API, or GTK2 API). Example:
 +
 
 +
<syntaxhighlight lang=pascal>
 +
uses
 +
  ..., LCLIntf, LCLType;
 +
 
 +
procedure TfmMain.SetFullScreen_Universal(AValue: boolean);
 +
begin
 +
  if AValue then
 +
    ShowWindow(Handle, SW_SHOWFULLSCREEN)
 +
  else
 +
    ShowWindow(Handle, SW_SHOWNORMAL);
 +
    //maybe consider here previous maximized state?
 +
end;
 +
</syntaxhighlight>
 +
 
 +
==Win32 way to use full-screen==
 +
 
 +
Universal way, above, works for Win32, but window border still stays, so better code which works:
 +
 
 +
<syntaxhighlight lang=pascal>
 +
procedure TfmMain.SetFullScreen_Win32(AValue: boolean);
 +
begin
 +
  if AValue then
 +
  begin
 +
    FOrigWndState:= WindowState;
 +
    FOrigBounds:= BoundsRect;
 +
 
 +
    BorderStyle:= bsNone;
 +
    BoundsRect:= Monitor.BoundsRect;
 +
  end
 +
  else
 +
  begin
 +
    WindowState:= FOrigWndState;
 +
    BoundsRect:= FOrigBounds;
 +
    BorderStyle:= bsSizeable;
 +
    BoundsRect:= FOrigBounds; //again
 +
  end;
 +
end;
 +
</syntaxhighlight>
 +
 
 +
===Win32 example program===
 +
 
 +
This is example for Win32 only, since it don't  use native full-screen mode on Linux/MacOS, it is here from old times.
 +
 
 +
To be able to switch to full screen mode you will need:
 
* Remember previous form state as position and size and window state
 
* Remember previous form state as position and size and window state
 
* Be able to determine screen size
 
* Be able to determine screen size
 
* Capture some key and perform operation
 
* Capture some key and perform operation
 
  
 
Best way to capture desired key is using Actions. Simply define new action in action manager and assign key shortcut. Than shortcut will work even if main form would lost focus.
 
Best way to capture desired key is using Actions. Simply define new action in action manager and assign key shortcut. Than shortcut will work even if main form would lost focus.
  
==Example program==
+
<syntaxhighlight lang=pascal>unit Unit1;  
 
 
<syntaxhighlight>unit Unit1;  
 
  
 
{$mode objfpc}{$H+}
 
{$mode objfpc}{$H+}
Line 27: Line 81:
  
 
type
 
type
 
  { TForm1 }
 
 
 
   TForm1 = class(TForm)
 
   TForm1 = class(TForm)
 
     procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
 
     procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
Line 49: Line 100:
 
{ TForm1 }
 
{ TForm1 }
  
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
+
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  );
 
const
 
  KeyF11 = 122;
 
 
begin
 
begin
   if Key = KeyF11 then SwitchFullScreen;
+
   if Key = VK_F11 then
 +
  begin
 +
    SwitchFullScreen;
 +
    Key := 0;
 +
  end;
 
end;
 
end;
  
Line 65: Line 117:
  
 
     BorderStyle := bsNone;
 
     BorderStyle := bsNone;
     ScreenBounds := Screen.MonitorFromWindow(Handle).BoundsRect;
+
     BoundsRect := Screen.MonitorFromWindow(Handle).BoundsRect;
    with ScreenBounds do
 
      SetBounds(Left, Top, Right - Left, Bottom - Top) ;
 
 
   end else begin
 
   end else begin
 
     // From full screen
 
     // From full screen
    {$IFDEF MSWINDOWS}
 
 
     BorderStyle := bsSizeable;
 
     BorderStyle := bsSizeable;
    {$ENDIF}     
 
 
     if OriginalWindowState = wsMaximized then
 
     if OriginalWindowState = wsMaximized then
 
       WindowState := wsMaximized
 
       WindowState := wsMaximized
 
     else
 
     else
       with OriginalBounds do
+
       BoundsRect := OriginalBounds;
        SetBounds(Left, Top, Right - Left, Bottom - Top) ;
 
    {$IFDEF LINUX}
 
    BorderStyle := bsSizeable;
 
    {$ENDIF} 
 
 
   end;
 
   end;
 
end;
 
end;
Line 86: Line 130:
 
end.</syntaxhighlight>
 
end.</syntaxhighlight>
  
==Get task bar size==
+
===Win32: get task bar size===
  
If you want to have task bar visible you have to adjust size according task bar position and size.  
+
If you want to have task bar visible you have to adjust size according task bar position and size. For Windows:
  
''For Windows:''
+
<syntaxhighlight lang=pascal>function GetTaskBarSize: TRect;
<syntaxhighlight>function GetTaskBarSize: TRect;
 
 
begin
 
begin
 
   SystemParametersInfo(SPI_GETWORKAREA, 0, @Result, 0);
 
   SystemParametersInfo(SPI_GETWORKAREA, 0, @Result, 0);
end;</syntaxhighlight>
+
end;
 +
</syntaxhighlight>
 +
 
 +
==GTK way to use full-screen==
  
==GTK way to switch to fullscreen mode==
+
This is low-level code for GTK2, it is used already by LCL in "universal way" above.
  
 
To switch form to full screen dimensions:
 
To switch form to full screen dimensions:
<syntaxhighlight>gdk_window_fullscreen(PGtkWidget(Handle)^.window);</syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 +
gdk_window_fullscreen(PGtkWidget(Handle)^.window);
 +
</syntaxhighlight>
  
 
To roll back to normal mode:
 
To roll back to normal mode:
<syntaxhighlight>gdk_window_unfullscreen(PGtkWidget(Handle)^.window);</syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 +
gdk_window_unfullscreen(PGtkWidget(Handle)^.window);
 +
</syntaxhighlight>
  
This is GTK2 only. You must add to uses section such modules as gtk2, gdk2, glib2. You can do it by the directive {$IFDEF LCLGTK2}, gtk2, gdk2, glib2{$ENDIF}.
+
This is GTK2 only. You must add to uses section such modules as gtk2, gdk2, glib2. You can do it by the directive  
 +
<syntaxhighlight lang=pascal>
 +
{$IFDEF LCLGTK2}, gtk2, gdk2, glib2{$ENDIF}
 +
</syntaxhighlight>
  
 
==Changing screen resolution==
 
==Changing screen resolution==
Line 119: Line 172:
  
 
* [http://delphi.about.com/od/delphitips2010/qt/delphi-application-full-screen-mode-f11.htm Run Your Delphi Application in Full Screen - Implement "F11 - Full Screen"]
 
* [http://delphi.about.com/od/delphitips2010/qt/delphi-application-full-screen-mode-f11.htm Run Your Delphi Application in Full Screen - Implement "F11 - Full Screen"]
 +
* [http://lazplanet.blogspot.com/2014/02/make-your-form-fullscreen.html Very simple way to make your form Fullscreen & Restore (LazPlanet)]
  
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]
 
[[Category:Lazarus]]
 
[[Category:Lazarus]]

Latest revision as of 12:04, 26 November 2020

English (en) français (fr)

Introduction

Some applications like some web browsers allow show in full screen mode using key F11. In full screen mode window hasn't border and uses entire screen space.

Standard way

You can switch form to full screen mode simply by assigning wsFullScreen value to TForm.WindowState property. You can do that directly in design time in Object Inspector window or in runtime:

procedure TForm1.FormShow(Sender: TObject);
begin
  WindowState := wsFullScreen;
end;

Universal way to use full-screen

Universal way exists, using LCL, you need to call ShowWindow() with SW_SHOWFULLSCREEN. This ShowWindow call is handled inside widgetset, using low-level API (for example, OS X Carbon API, or GTK2 API). Example:

uses
  ..., LCLIntf, LCLType;

procedure TfmMain.SetFullScreen_Universal(AValue: boolean);
begin
  if AValue then
    ShowWindow(Handle, SW_SHOWFULLSCREEN)
  else
    ShowWindow(Handle, SW_SHOWNORMAL); 
    //maybe consider here previous maximized state?
end;

Win32 way to use full-screen

Universal way, above, works for Win32, but window border still stays, so better code which works:

procedure TfmMain.SetFullScreen_Win32(AValue: boolean);
begin
  if AValue then
  begin
    FOrigWndState:= WindowState;
    FOrigBounds:= BoundsRect;

    BorderStyle:= bsNone;
    BoundsRect:= Monitor.BoundsRect;
  end
  else
  begin
    WindowState:= FOrigWndState;
    BoundsRect:= FOrigBounds;
    BorderStyle:= bsSizeable;
    BoundsRect:= FOrigBounds; //again
  end;
end;

Win32 example program

This is example for Win32 only, since it don't use native full-screen mode on Linux/MacOS, it is here from old times.

To be able to switch to full screen mode you will need:

  • Remember previous form state as position and size and window state
  • Be able to determine screen size
  • Capture some key and perform operation

Best way to capture desired key is using Actions. Simply define new action in action manager and assign key shortcut. Than shortcut will work even if main form would lost focus.

unit Unit1; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  Menus;

type
  TForm1 = class(TForm)
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
  public
    OriginalBounds: TRect;
    OriginalWindowState: TWindowState;
    ScreenBounds: TRect;
    procedure SwitchFullScreen;
  end; 

var
  Form1: TForm1; 

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if Key = VK_F11 then
  begin
    SwitchFullScreen;
    Key := 0;
  end;
end;

procedure TForm1.SwitchFullScreen;
begin
  if BorderStyle <> bsNone then begin
    // To full screen
    OriginalWindowState := WindowState;
    OriginalBounds := BoundsRect;

    BorderStyle := bsNone;
    BoundsRect := Screen.MonitorFromWindow(Handle).BoundsRect;
  end else begin
    // From full screen
    BorderStyle := bsSizeable;
    if OriginalWindowState = wsMaximized then
      WindowState := wsMaximized
    else
      BoundsRect := OriginalBounds;
  end;
end;

end.

Win32: get task bar size

If you want to have task bar visible you have to adjust size according task bar position and size. For Windows:

function GetTaskBarSize: TRect;
begin
  SystemParametersInfo(SPI_GETWORKAREA, 0, @Result, 0);
end;

GTK way to use full-screen

This is low-level code for GTK2, it is used already by LCL in "universal way" above.

To switch form to full screen dimensions:

gdk_window_fullscreen(PGtkWidget(Handle)^.window);

To roll back to normal mode:

gdk_window_unfullscreen(PGtkWidget(Handle)^.window);

This is GTK2 only. You must add to uses section such modules as gtk2, gdk2, glib2. You can do it by the directive

{$IFDEF LCLGTK2}, gtk2, gdk2, glib2{$ENDIF}

Changing screen resolution

There is no OS independent way to switch screen resolution now.

For Windows you can find information in article for Delphi Get and Set Screen Resolution (Display Device Modes)

See also

External links