SoFunction
Updated on 2025-04-11

C# Example code for implementing WinForm full screen top

Application requirements

When we run some Windows applications, we need to run them in the form top mode (making other application forms unable to block the top application forms) and enter full screen state. This article will introduce how to use C# to implement the basic functions of WinForm's full-screen top.

The basic functions mainly implement the following points:

(1) Change some appearance properties of WinForm, including borderless, maximization and top attributes.

(2) Block some keyboard operations to prevent the application from closing or switching to other applications. Basically, you can block left and right WIN menu keys, close window key combination (Alt+F4), switch window key combination (Alt+Tab), and start menu key (Ctrl+Esc).

design

Design the CraneofficeWinLock class, which can implement some methods, automatically set some WinForm properties and block some keyboard operations. The main design is as follows:

Serial number name Member Type type illustrate
1 form property Specify the form to automatically set properties
2 OnKeyPress method void How to deal with blocked keyboard operations
3 Start method void The main entrance method, start the program, and you need to pass the OnKeyPress method handle
4 Stop method void Stop all blocking operations

Sample operating environment

Operating system: Windows 11, Windows 10, Windows 2019 Server

.net version: .netFramework 4.7.2 or above

Development Tools: VS2019 C#

Running device: Microsoft Surface Pro

Implement code

Core code

The code is as follows:

   public class CraneofficeWinLock
    {
        private const int WH_KEYBOARD_LL = 13; //keyboard        public Form form = null;
        private delegate int HookHandle(int nCode, int wParam, IntPtr lParam);
        public delegate void ProcessKeyHandle(HookStruct param, out bool handle);
        private static int _hHookValue = 0;
        private HookHandle _KeyBoardHookProcedure;
        [StructLayout()]
        public class HookStruct
        {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;
        }
        [DllImport("")]
        private static extern int SetWindowsHookEx(int idHook, HookHandle lpfn, IntPtr hInstance, int threadId);
        [DllImport("", CharSet = , CallingConvention = )]
        private static extern bool UnhookWindowsHookEx(int idHook);
        [DllImport("")]
        private static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);
        [DllImport("")]
        private static extern int GetCurrentThreadId();
        [DllImport("")]
        private static extern IntPtr GetModuleHandle(string name);
 
        private IntPtr _hookWindowPtr = ;
        public CraneofficeWinLock() { }
        private static ProcessKeyHandle _clientMethod = null;
        public void Start(ProcessKeyHandle clientMethod)
        {
            if (form != null)
            {
                 = ;
                 = ;
                 = true;
            }
            _clientMethod = clientMethod;
            if (_hHookValue == 0)
            {
                _KeyBoardHookProcedure = new HookHandle(OnHookProc);
 
                _hookWindowPtr = GetModuleHandle(().);
 
                _hHookValue = SetWindowsHookEx(
                WH_KEYBOARD_LL,
                _KeyBoardHookProcedure,
                _hookWindowPtr,
                0);
 
                //If setting the hook fails.                if (_hHookValue == 0) Stop();
            }
        }
        public void Stop()
        {
            if (_hHookValue != 0)
            {
                bool ret = UnhookWindowsHookEx(_hHookValue);
                if (ret) _hHookValue = 0;
            }
        }
        private static int OnHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                HookStruct hookStruct = (HookStruct)(lParam, typeof(HookStruct));
 
                if (_clientMethod != null)
                {
                    bool handle = false;
                    _clientMethod(hookStruct, out handle);
                    if (handle) return 1; //1: means the keyboard, return exit                }
            }
            return CallNextHookEx(_hHookValue, nCode, wParam, lParam);
        }
        public void OnKeyPress( hookStruct, out bool handle)
        {
 
            handle = false;
            if ( == 91) // Left win (start menu key)            {
                handle = true;
            }
            if ( == 92)// Right win            {
                handle = true;
            }
 
            if ( == (int) && (int) == (int))
            {
                handle = true;
            }
            if ( == (int)Keys.F4 && (int) == (int))
            {
                handle = true;
            }
            if ( == (int) && (int) == (int))
            {
                handle = true;
            }
            if ( == (int)Keys.F1)
            {
                handle = true;
            }
 
            Keys key = (Keys);
 
        }
 
    }

Call Example

The sample code is as follows:

private  _winlock = null;
 
private void Form1_Load(object sender, EventArgs e)
{
     _winlock = new ();
     _winlock.form = this;
     _winlock.Start(_winlock.OnKeyPress);
}

summary

We can stop the blocking operation in the exit code, as follows:

if (_winlock != null) _winlock.Stop();
();

In addition, to prevent some other unconsidered situations, I am lazy, and I wrote a timer (1000 milliseconds) code to activate the state of the form in real time to keep the form at the top level forever, as follows:

private void timer1_Tick(object sender, EventArgs e)
{
     ();
}

This is the article about C#’s example code to implement WinForm full-screen top content. For more related C# WinForm full-screen top content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!