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

坚强是说给别人听的谎言 提交于 2019-12-28 13:21:54

问题


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 button.

I am trying to develop the my application window like this.

Add my code snippet below

 <controls:MetroWindow x:Class="EDUI.MainWindow"
            xmlns:controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:local="clr-namespace:EDiscoveryCore;assembly=EDiscoveryCore"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="eDi"  BorderBrush="SkyBlue" BorderThickness="2" Height="999" Width="1071" WindowState="Maximized" x:Name="MainWindows">

回答1:


You need to set the WindowStyle to none as well as WindowState to Maximized

<Window ...    
 WindowStyle="None"   
 WindowState="Maximized">



回答2:


You need to set the ResizeMode to NoResize and WindowState to Maximized

  <Window ...    
    ResizeMode="NoResize" WindowState="Maximized">



回答3:


Try this:

<Window ShowTitleBar="False" IgnoreTaskbarOnMaximize="True">



回答4:


If the taskbar doesn't disappear, it may help to change the Window visibility before and after changing window style, like this:

    private void MainWindow_StateChanged(object sender, EventArgs e) {
        if (this.WindowState == WindowState.Maximized) {
            // hide the window before changing window style
            this.Visibility = Visibility.Collapsed;
            this.Topmost = true;
            this.WindowStyle = WindowStyle.None;
            this.ResizeMode = ResizeMode.NoResize;
            // re-show the window after changing style
            this.Visibility = Visibility.Visible;
        }
        else {
            this.Topmost = false;
            this.WindowStyle = WindowStyle.SingleBorderWindow;
            this.ResizeMode = ResizeMode.CanResize;
        }
    }



回答5:


You just need to set the WindowStyle to none:

<Window ...
    WindowStyle="None">



回答6:


I had this issue with the taskbar staying on top of my window. The current solution i use is setting the window as Topmost for a short time, then setting it back to false (i want my window to work nice with Alt+Tab)

private Timer t;
public void OnLoad()
    {
        var window = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);
        StartTopmostTimer(window);
    }

    private void StartTopmostTimer(Window window)
    {
        t = new Timer(o => SetTopMostFalse(window), null, 1000, Timeout.Infinite);
    }

    private void SetTopMostFalse(Window window)
    {
        Application.Current.Dispatcher.BeginInvoke(new Action(() =>
        {
            window.Topmost = false;
        }));
        t.Dispose();
    }

I also use this code to switch between full screen and window mode:

public void SwitchFullScreen()
    {
        var window = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);

        if (window != null)
        {
            if (window.WindowStyle == WindowStyle.None)
            {
                window.WindowStyle = WindowStyle.SingleBorderWindow;
                window.WindowState = state;
            }
            else
            {
                state = window.WindowState;
                window.WindowStyle = WindowStyle.None;
                window.WindowState = WindowState.Maximized;
                window.Topmost = true;
                StartTopmostTimer(window);
            }
        }
    }



回答7:


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)




回答8:


Simply hook this event handler to the Loaded event of the form, works fine.
Do not apply this stuff in the constructor of the form (which won't work for me).

    private void aWindow_Loaded(object sender, RoutedEventArgs e)
    {
        MaxHeight = SystemParameters.FullPrimaryScreenHeight;
        MaxWidth = SystemParameters.FullPrimaryScreenWidth;
        Width = SystemParameters.FullPrimaryScreenWidth;
        Height = SystemParameters.FullPrimaryScreenHeight;
        WindowState = WindowState.Maximized;
        ResizeMode = ResizeMode.NoResize;
        Left = 0;
        Top = 0;
        Topmost = true;
        ShowInTaskbar = false;

        //throw new NotImplementedException();
    }


来源:https://stackoverflow.com/questions/25499393/make-my-wpf-application-full-screen-cover-taskbar-and-title-bar-of-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!