how to force wpf fullscreen and disable user back to desktop?

William Liu 466 Reputation points
2024-08-01T05:44:15.97+00:00

I am working on a .net 8 WPF program which works like a POS(Point of sales)on customer's Windows 10 device.

My question is how to force WPF window into fullscreen and disable user going back to desktop or other application (by shotcut, WinKey, taskmgr etc...).

The user can exit the application after authentication.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,784 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,571 Reputation points Microsoft Vendor
    2024-08-01T08:50:22.7966667+00:00

    Hi,@William Liu . Welcome to Microsoft Q&A. To achieve a fullscreen WPF window that restricts user access to other applications or the desktop, you could try following steps:

    Force Full-Screen Mode: Set the WPF window to full screen. Disable System Shortcuts: Use system hooks to disable key combinations like Alt+Tab, WinKey, etc. Disable Task Manager: Prevent the user from opening Task Manager. Secure Exit: Provide a secure way to exit the application, such as an authentication method.

    
    
    <Window x:Class="WpfApp4.MainWindow"
    
          ...
            WindowState="Maximized"
    
            ResizeMode="NoResize"
    
            WindowStartupLocation="CenterScreen"
    
            Loaded="Window_Loaded"
    
            Title="MainWindow" Height="450" Width="800">
    
        <StackPanel>
    
            <TextBox Name="TXT" Height="50"/>
    
            <Button Content="DISABLE" Height="50" Click="Button_Click_1"/>
    
            <Button Content="EXIT" Height="50" Click="Button_Click"/>
    
        </StackPanel>
    
    </Window>
    
    
    

    Codebehind:

    using Microsoft.Win32;
    using System.Runtime.InteropServices;
    using System.Windows;
    using Application = System.Windows.Application;
    namespace WpfApp4
    {
        public partial class MainWindow : Window
        {
    
            public const int WH_MIN = (-1);
            public const int WH_MSGFILTER = (-1);
            public const int WH_JOURNALRECORD = 0;
            public const int WH_JOURNALPLAYBACK = 1;
            public const int WH_KEYBOARD = 2;
            public const int WH_GETMESSAGE = 3;
            public const int WH_CALLWNDPROC = 4;
            public const int WH_CBT = 5;
            public const int WH_SYSMSGFILTER = 6;
            public const int WH_MOUSE = 7;
            public const int WH_HARDWARE = 8;
            public const int WH_DEBUG = 9;
            public const int WH_SHELL = 10;
            public const int WH_FOREGROUNDIDLE = 11;
            public const int WH_CALLWNDPROCRET = 12;
            public const int WH_KEYBOARD_LL = 13;
            public const int WH_MOUSE_LL = 14;
            public const int WH_MAX = 14;
            public const int WH_MINHOOK = WH_MIN;
            public const int WH_MAXHOOK = WH_MAX;
    
            [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
            public struct MSLLHOOKSTRUCT
            {
                public System.Drawing.Point pt;
                public int mouseData;
                public int flags;
                public int time;
                public uint dwExtraInfo;
            }
    
            [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
            public struct KBDLLHOOKSTRUCT
            {
                public int vkCode;
                public int scanCode;
                public int flags;
                public int time;
                public uint dwExtraInfo;
            }
    
            public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
    
            [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
            public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
    
            [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
            public static extern bool UnhookWindowsHookEx(int idHook);
    
            [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
            public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);
    
            public const int KF_EXTENDED = 0x0100;
            public const int KF_DLGMODE = 0x0800;
            public const int KF_MENUMODE = 0x1000;
            public const int KF_ALTDOWN = 0x2000;
            public const int KF_REPEAT = 0x4000;
            public const int KF_UP = 0x8000;
    
            public const int LLKHF_EXTENDED = (KF_EXTENDED >> 8); /* 0x00000001 */
            public const int LLKHF_INJECTED = 0x00000010;
            public const int LLKHF_ALTDOWN = (KF_ALTDOWN >> 8); /* 0x00000020 */
            public const int LLKHF_UP = (KF_UP >> 8);      /* 0x00000080 */
            public const int LLKHF_LOWER_IL_INJECTED = 0x00000002;
    
            public const int LLMHF_INJECTED = 0x00000001;
            public const int LLMHF_LOWER_IL_INJECTED = 0x00000002;
    
            static int hHook = 0;
            HookProc KeyboardLLProcedure;
    
            public const int WM_KEYDOWN = 0x0100;
            public const int WM_SYSKEYDOWN = 0x0104;
            public const int VK_SHIFT = 0x10;
            public const int VK_MENU = 0x12;
    
            [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto)]
            private static extern short GetAsyncKeyState(Keys vKey);
    
    
            
    
            [DllImport("user32.dll")]
            [return: MarshalAs(UnmanagedType.Bool)]
            private static extern bool UnhookWindowsHookEx(IntPtr hhk);
    
         
            private IntPtr _hookID = IntPtr.Zero;
    
            public MainWindow()
            {
                InitializeComponent();
              
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
              
                DisableTaskManager();
            }
    
          
    
    
            private const int VK_CONTROL = 0x11;
            private const int VK_ESCAPE = 0x1B;
            public static int KeyboardLLProc(int nCode, IntPtr wParam, IntPtr lParam)
            {
                bool bShiftKeyDown = false;
                bool bAltKeyDown = false;
    
                if (nCode >= 0)
                {
                    bShiftKeyDown = GetAsyncKeyState((Keys)VK_SHIFT) < 0;
                    bAltKeyDown = GetAsyncKeyState((Keys)VK_MENU) < 0;
                    bool bCtrlKeyDown = GetAsyncKeyState(Keys.ControlKey) < 0;
    
                    KBDLLHOOKSTRUCT pKBDLLHOOKSTRUCT = new KBDLLHOOKSTRUCT();
                    pKBDLLHOOKSTRUCT = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, pKBDLLHOOKSTRUCT.GetType());
                    if (pKBDLLHOOKSTRUCT.vkCode == (uint)Keys.Escape && bCtrlKeyDown)
                    {
                        Console.Beep(8000, 10);
                        return 1;
                    }
                    if (pKBDLLHOOKSTRUCT.vkCode == (short)Keys.LWin|| pKBDLLHOOKSTRUCT.vkCode == (short)Keys.RWin)
                    {
                        Console.Beep(8000, 10);
                        return 1;
                    }
                    if (pKBDLLHOOKSTRUCT.vkCode == (short)Keys.Tab && bAltKeyDown)
                    {
                        Console.Beep(8000, 10);
                        return 1;
                    }
                }
                return nCode < 0 ? CallNextHookEx(hHook, nCode, wParam, lParam) : 0;
            }
            protected override void OnClosed(EventArgs e)
            {
                UnhookWindowsHookEx(_hookID);
                EnableTaskManager();
                base.OnClosed(e);
            }
    
            private void DisableTaskManager()
            {
                RegistryKey regKey = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System");
                if (regKey != null)
                {
                    regKey.SetValue("DisableTaskMgr", 1, RegistryValueKind.DWord);
                    regKey.Close();
                }
            }
    
            private void EnableTaskManager()
            {
                RegistryKey regKey = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System");
                if (regKey != null)
                {
                    regKey.DeleteValue("DisableTaskMgr", false);
                    regKey.Close();
                }
            }
    
            private void SecureExit()
            {
                string password = "secure";
                string input = PromptForPassword();
    
                if (input == password)
                {
                    EnableTaskManager();
    
                    if (hHook != 0)
                        UnhookWindowsHookEx(hHook);
    
                    Application.Current.Shutdown();
                }
                else
                {
                    System.Windows.MessageBox.Show("Incorrect password.");
                }
            }
    
            private string PromptForPassword()
            {
               
                string p = TXT.Text.ToString();
                return p;
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                SecureExit();
            }
    
            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                if (hHook == 0)
                {
                    KeyboardLLProcedure = new HookProc(KeyboardLLProc);
                    hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardLLProcedure, (IntPtr)0, 0);
                }
            }
        }
    }
    
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.