Make my wpf application Full Screen (Cover taskbar and title bar of window)

后端 未结 9 1916
心在旅途
心在旅途 2020-12-05 14:34

I would like to make my application such that it can maximize to full screen means it hide the windows task bar and the title bar as well. And it should triggered by a butto

9条回答
  •  忘掉有多难
    2020-12-05 15:18

    Step 1: Write class with static methods Hide() and Show() for taskbar

    public class Taskbar
    {
        [DllImport("user32.dll")]
        private static extern int FindWindow(string className, string windowText);
        [DllImport("user32.dll")]
        private static extern int ShowWindow(int hwnd, int command);
    
        private const int SW_HIDE = 0;
        private const int SW_SHOW = 1;
    
        protected static int Handle
        {
            get
            {
                return FindWindow("Shell_TrayWnd", "");
            }
        }
    
        private Taskbar()
        {
            // hide ctor
        }
    
        public static void Show()
        {
            ShowWindow(Handle, SW_SHOW);
        }
    
        public static void Hide()
        {
            ShowWindow(Handle, SW_HIDE);
        }
    }
    

    Step 2: Connect to window Closing event to get taskbar back when window will close with Alt+F4

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        Taskbar.Show();
    }
    

    Hide taskbar and show fullscreen:

    Taskbar.Hide();
    WindowStyle = WindowStyle.None;
    WindowState = WindowState.Maximized;
    ResizeMode = ResizeMode.NoResize;
    Width = System.Windows.SystemParameters.PrimaryScreenWidth;
    Height = System.Windows.SystemParameters.PrimaryScreenHeight;
    Topmost = true;
    Left = 0;
    Top = 0;
    

    Show taskbar and run in window

    Taskbar.Show();
    WindowStyle = WindowStyle.SingleBorderWindow;
    WindowState = WindowState.Normal;
    ResizeMode = ResizeMode.CanResize;
    Width = System.Windows.SystemParameters.WorkArea.Width-100;
    Height = System.Windows.SystemParameters.WorkArea.Height-100;
    Topmost = false;
    Left = 0;
    Top = 0;
    

    Tested on Windows 10 1703 (Creators Update)

提交回复
热议问题