Application full screen mode

From Free Pascal wiki
Revision as of 08:52, 21 December 2010 by Chronos (talk | contribs) (different linux and windows behaviour workaround)
Jump to navigationJump to search

Introduction

Some applications like some web browsers allow show in full screen mode using key F11. In full screen mode window haven't border and title bar and use entire screen space. Task bar is hidden too.


To be able to switch to fullscreen 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.

Example program

<delphi>unit Unit1;

{$mode objfpc}{$H+}

interface

uses

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

type

 { TForm1 }
 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

 );

const

 KeyF11 = 122;

begin

 if Key = KeyF11 then SwitchFullScreen;

end;

procedure TForm1.SwitchFullScreen; begin

 if BorderStyle <> bsNone then begin
   // To full screen
   OriginalWindowState := WindowState;
   OriginalBounds := BoundsRect;
   BorderStyle := bsNone;
   ScreenBounds := Screen.MonitorFromWindow(Handle).BoundsRect;
   with ScreenBounds do
     SetBounds(Left, Top, Right - Left, Bottom - Top) ;
 end else begin
   // From full screen
   {$IFDEF MSWINDOWS}
   BorderStyle := bsSizeable;
   {$ENDIF}      
   if OriginalWindowState = wsMaximized then
     WindowState := wsMaximized
   else
     with OriginalBounds do
       SetBounds(Left, Top, Right - Left, Bottom - Top) ;
   {$IFDEF LINUX}
   BorderStyle := bsSizeable;
   {$ENDIF}  
 end;

end;

end.</delphi>

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: <delphi>function GetTaskBarSize: TRect; begin

 SystemParametersInfo(SPI_GETWORKAREA, 0, @Result, 0);

end;</delphi>

GTK way to switch to fullscreen mode

To switch form to full screen dimensions: <delphi>gdk_window_fullscreen(PGtkWidget(Handle)^.window);</delphi>

To roll back to normal mode: <delphi>gdk_window_unfullscreen(PGtkWidget(Handle)^.window);</delphi>

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