Fullscreen is a common option in many of the popular programs we use. Today we learn the simple trick of making our program fullscreen.
Introduction
Fullscreen is an excellent option for programers. Because often you will need to eliminate all the other windows and just focus on the content that is on your form.
When a software goes Fullscreen it sizes itself up to the screen size and removes even its titlebar, Close-Minimize buttons. Even the Taskbars and other panels of the OS hides and let only the content to show. It is very handy when you for example, want to create an image slideshow and want the users to see the images in a fullscreen view with a dark background.
Making a form fullscreen is not hard. In fact its so simple that after I uncover its mysteries, you will be frustrated on the simplicity of it! ;-)
How does it work
Okey, let me explain. If we want to make a form fullscreen, (1) we have to hide the border around the window, and the Close, Minimize and Maximize buttons as well. That would leave us with a rectangular area on the screen. Now we have to make it take all the screen space for itself. So, (2) we would have to Maximize the form. That’s it! It will make the form fullscreen and automatically hide Taskbar or other windows. The OS takes care of hiding all the bars and panels.
But to be extra sure, you can set the form to Always on Top and Hide the taskbar as well.
To make your Form Fullscreen
So basically we:
- Set the Form’s BorderStyle property to bsNone.
- Set WindowState property to wsMinimized.
- Optionally, you can set FormStyle to fsSystemStayOnTop (to make it always on top.)
- Optionally, you can Hide the taskbar with the help of this post. (But remember to show the taskbar again on program exit. If you don’t show it, the user will be left with no taskbar!)
To Restore the Fullscreen mode
Here, we do the opposite:
- Set WindowState property to wsNormal
- Set the form’s BorderStyle property to bsSizeable (or your previous style that you have set).
- If you changed the FormStyle, you can set FormStyle to fsNormal (or what you previous have set)
- If you have hidden the taskbar, show it again with the help of this post.
You will notice that when doing fullscreen we have first modified the the BorderStyle. But when restoring from fullscreen we first modified WindowState. We switched this order because setting the BorderStyle First loses the dimension (width-height) of the form while restored.
Tutorial
We will now see the above thing in action.
Start Lazarus.
Create a new Application Project (Project -> New Project -> Application -> OK).
Draw 2 TButtons in the form. They will be named as Button1
and Button2
. Set Button1‘s Caption to Fullscreen
and Button2‘s to Exit Fullscreen
.
We’ll code the Fullscreen button. Double click Button1 and enter:
1 | procedure TForm1.Button1Click(Sender: TObject); |
Now, we get to the Restore Fullscreen option. Double click Button2 and enter :
(If you can’t switch between Code View and Form View try F12 or View -> Toggle Form/Unit View)
1 | procedure TForm1.Button2Click(Sender: TObject); |
I have not added the Windows API code to Hide the Taskbar. So it remains a pure cross platform solution. But if you wish to include it, you are welcome. You can also use compiler directive to include the code only when compiled from windows. For example:
1 | uses |
The above code only includes windows
unit if it is being compiled from Windows. You can use compiler directive for other codes as well.
If by any chance you got stuck in the fullscreen mode and can’t close the form, use Alt+F4.
Now Run the project (F9 or Run -> Run).
Click the buttons to test if it goes fullscreen and back.
You can use this in an Image viewer, a web browser or a media player. May be even for a kiosk!
EDIT (22 FEB 14):
This is a basic example code for making your form fullscreen. It has some limitations that it doesn’t remember the last state and it always restores to wsNormal.
If you want more advanced version with keyboard shortcut and form size & state remembering and linux troubleshooting, see this wiki for code.
Downloads
You can download the source code for the tutorial project and executable/EXE files from the links below: