Background

Currently, I’m developing an application for a mobile device that runs on Windows XP Embedded. One of the requirements is that the application has to run in full screen mode. Also, a regular user should not be able to exit the application.

I was able to install the .NET 2.0 Framework on Windows XPe, so the front-end will be a WinForms 2.0 application.

The Solution

A WinForm already provides us half of what we need. We can set in “on top” with the TopMost property and we can take away the control box (minimize, maximize and close) with the ControlBox property.

What we don’t have is a property to really go Full Screen. So we need to do some magic that the application will overlap the Windows Taskbar.

I’ve wrapped both parts in their own separate class, WinApi and FormState. You know: reusability, separation and all that…

using System;
using System.Runtime.InteropServices;

namespace WinFormApp.Custom
{
    /// <summary>
    /// Selected Win AI Function Calls
    /// </summary>
    public class WinApi
    {
        [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
        public static extern int GetSystemMetrics(int which);

        [DllImport("user32.dll")]
        public static extern void
            SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
                         int X, int Y, int width, int height, uint flags);

        private const int SM_CXSCREEN = 0;
        private const int SM_CYSCREEN = 1;
        private static IntPtr HWND_TOP = IntPtr.Zero;
        private const int SWP_SHOWWINDOW = 64; // 0x0040

        public static int ScreenX
        {
            get { return GetSystemMetrics(SM_CXSCREEN); }
        }

        public static int ScreenY
        {
            get { return GetSystemMetrics(SM_CYSCREEN); }
        }

        public static void SetWinFullScreen(IntPtr hwnd)
        {
            SetWindowPos(hwnd, HWND_TOP, 0, 0, ScreenX, ScreenY, SWP_SHOWWINDOW);
        }
    }
}
using System;
using System.Drawing;
using System.Windows.Forms;

namespace WinFormApp.Custom
{
    /// <summary>
    /// Class used to preserve / restore state of the form
    /// </summary>
    public class FormState
    {
        private FormWindowState _winState;
        private FormBorderStyle _brdStyle;
        private Rectangle _bounds;
        private bool _topMost = false;
        private bool _isFullScreen = false;

        private void Save(Form targetForm)
        {
            _winState = targetForm.WindowState;
            _brdStyle = targetForm.FormBorderStyle;
            _bounds = targetForm.Bounds;
            _topMost = targetForm.TopMost;
        }

        public void FullScreen(Form targetForm, FormBorderStyle borderStyle)
        {
            if (!_isFullScreen)
            {
                _isFullScreen = true;
                Save(targetForm);

                targetForm.WindowState = FormWindowState.Maximized;
                targetForm.FormBorderStyle = borderStyle;
                targetForm.TopMost = true;
                targetForm.MaximizeBox = false;
                targetForm.MinimizeBox = false;
                targetForm.ControlBox = false;
                WinApi.SetWinFullScreen(targetForm.Handle);
            }
        }

        public void Restore(Form targetForm)
        {
            if (_isFullScreen)
            {
                _isFullScreen = false;

                targetForm.WindowState = _winState;
                targetForm.FormBorderStyle = _brdStyle;
                targetForm.Bounds = _bounds;
                targetForm.TopMost = _topMost;
            }
        }
    }
}

 

Conclusion

With this code you can run your application Full Screen and keep the user within the application. Somewhere you can place a button that calls the restore method. In my situation, an admin should be able to view the file system. You could even place an entry in the config file, so it doesn’t bother you when you’re debugging.

And before you say: “Haha, I pressed the Windows button on my keyboard and now I can do whatever I want”. My application has to run on a device which hasn’t got a keyboard. It’s a touchscreen device and nothing more. If you really want to disable all windows commands (Windows button, ALT+F4, CTRL + ALT + DEL, …), you will have to dig deeper and google it. But beware! Overriding too much, can get you into trouble…