使我的WPF应用程序全屏显示(覆盖任务栏和窗口标题栏)

22

我希望我的应用程序可以最大化至全屏,这意味着它需要隐藏Windows任务栏和标题栏。并且它应该由按钮触发。

我正在尝试开发我的应用程序窗口,就像这样。 enter image description here

请参考以下代码片段

 <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">
11个回答

25

您需要将WindowStyle设置为“none”,以及将WindowState设置为“Maximized”。

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

21

你需要将 ResizeMode 设置为 NoResize,WindowState 设置为 Maximized

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

7
对于Windows 10 WPF,ResizeMode="NoResize" 对我来说是关键。感谢@Narek。 - Ian GM
1
也适用于我。谢谢! - T James
2
这也是解决Windows 10上WPF的方案。如果没有它,在Win7上运行良好,但在Win8和Win10上则不行。我正在使用WindowState="Maximized" WindowStyle="None" ResizeMode="NoResize"。 - Milan Kocic
1
我已经有了 WindowStyle=NoneWindowState=Maximized(按这个顺序!),在Windows 7和Windows 8.1上都没有问题。但是一旦我的应用程序打开一个对话框窗口,在Windows 10上任务栏就会出现在最上面,而且除非切换到另一个窗口并返回到我的应用程序,否则它不会消失。ResizeMode=NoResize解决了这个问题! - Nicolas

18

如果任务栏没有消失,可以尝试在更改窗口样式之前和之后更改窗口可见性,像这样:

    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;
        }
    }

1
这对我来说解决了问题。非常感谢! - krobelusmeetsyndra
这在第二个显示器上运行正常。主要的显示器已经使用 WindowStyle=NoneWindowState=Maximized 正常工作。 - Simon Legg

13

试一下这个:

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

10
要使用“IgnoreTaskbarOnMaximize”功能,您需要使用MahApps库,请查看http://mahapps.com/。 - user1069816
ShowInTaskbar="False" - lindexi
“ShowTitleBar”是一个真正的属性吗?它给了我一个错误,显然隐藏标题栏的正确方法是“WindowStyle =”None“。” - komodosp
@komodosp ShowTitleBarMahApps.MetroWindow 中的一个属性。 - undefined

4
您只需将WindowStyle设置为none:
<Window ...
    WindowStyle="None">

2
步骤1:编写一个包含静态方法Hide()和Show()的类,用于任务栏。
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);
    }
}

步骤2:连接到窗口关闭事件,以便在使用Alt+F4关闭窗口时恢复任务栏。
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    Taskbar.Show();
}

隐藏任务栏并显示全屏:
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;

显示任务栏并在窗口中运行

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;

本内容测试于Windows 10 1703版本(创作者更新)。

在此输入图片描述


1

以上方法都没有对我起作用。即使使用 Windows API 隐藏任务栏,但是在窗口最大化后仍然会有空白间隙。 有效的方法是不设置窗口的 Maximized 属性,获取桌面的大小并设置这些属性:

Top = 0;
Left = 0;
Width = width_of_your_desktop;
Height = height_of_your_desktop;

甚至不需要设置最顶层!要获取屏幕尺寸,您可以使用SystemParameters.PrimaryScreenHeight和PrimaryScreenWidth的值,或者如果您想获取窗口当前所在的屏幕,请使用下面的GetMonitorFromWindow:

[StructLayout(LayoutKind.Sequential)]
private struct MonitorInfo
{
    public uint cbSize;
    public Rect2 rcMonitor;
    public Rect2 rcWork;
    public uint dwFlags;
}

[StructLayout(LayoutKind.Sequential)]
private struct Rect2
{
    public int left;
    public int top;
    public int right;
    public int bottom;
}

private const int MONITOR_DEFAULTTONULL = 0;
private const int MONITOR_DEFAULTTOPRIMARY = 1;
private const int MONITOR_DEFAULTTONEAREST = 2;
[DllImport("user32.dll")]
private static extern IntPtr MonitorFromWindow(IntPtr hwnd, int flags);
[DllImport("user32.dll")]
private static extern bool GetMonitorInfo(IntPtr hwnd, ref MonitorInfo mInfo);

public static Rect GetMonitorFromWindow(Window win) {
    var mi = new MonitorInfo();
    mi.cbSize = (uint)Marshal.SizeOf(mi);
    var hwmon = MonitorFromWindow(new System.Windows.Interop.WindowInteropHelper(win).EnsureHandle(), MONITOR_DEFAULTTONULL);
    if (hwmon != null && GetMonitorInfo(hwmon, ref mi)) {
        //convert to device-independent vaues
        var mon = mi.rcMonitor;
        Point realp1;
        Point realp2;
        var trans = PresentationSource.FromVisual(win).CompositionTarget.TransformFromDevice;
        realp1 = trans.Transform(new Point(mon.left, mon.top));
        realp2 = trans.Transform(new Point(mon.right, mon.bottom));
        return new Rect(realp1, realp2);
    }
    else
        throw new Exception("Failed to get monitor info.");
}

1

我曾遇到任务栏一直停留在我的窗口上的问题。目前我采用的解决方案是将窗口设置为“最顶层”一段时间,然后再将其设置为“否”(我希望我的窗口能够与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();
    }

我也使用这段代码在全屏和窗口模式之间进行切换:
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);
            }
        }
    }

0
只需将此事件处理程序挂钩到窗体的Loaded事件上即可,效果很好。
不要在窗体的构造函数中应用此内容(这对我无效)。
    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();
    }

0
一个完整可行的解决方案。
 private void BtnFullScreen_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        this.Topmost = false;
        if (WindowState != WindowState.Maximized)
        {
            WindowState = WindowState.Maximized;
            WindowStyle = WindowStyle.None;

            btnFullScreen.Source = this.FindResource("FullScreenIn") as System.Windows.Media.ImageSource;
             
            this.ResizeMode = ResizeMode.NoResize;
            this.Visibility = Visibility.Collapsed;
            this.Topmost = true;
           
            //// re-show the window after changing style
            this.Visibility = Visibility.Visible;
            MaxHeight = SystemParameters.VirtualScreenHeight; MaxWidth = SystemParameters.VirtualScreenWidth;
        }
        else
        {
            WindowState = WindowState.Normal;
            WindowStyle = WindowStyle.SingleBorderWindow;
            ResizeMode = ResizeMode.CanResize;

            //WindowStartupLocation = WindowStartupLocation.CenterScreen;
            Top = 0.0;
            double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
            double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
            double windowWidth = this.Width;
            double windowHeight = this.Height;
            this.Left = (screenWidth / 2) - (windowWidth / 2);
            btnFullScreen.Source = this.FindResource("FullScreenOut") as System.Windows.Media.ImageSource;
             
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接