完美解决WPF窗体最大化遮住Windows任务栏的情况

      网上很多说用hook还有写乱七八糟一堆代码。我这唯一找到一个很好的方案:

xaml文件加入这句代码:完美解决

private void ButtonMax_Click(object sender, RoutedEventArgs e)
        {
            if (this.WindowState == WindowState.Maximized)
            {
                this.WindowState = WindowState.Normal;
            }
            else if (this.WindowState == WindowState.Normal)
            {
                this.WindowState = WindowState.Maximized;
                //代码在这
                this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
                this.MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
            }
        }

或者在xaml文件里加上这句

<Window x:Class="Working.Ui.WindowManagement"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="" Height="960" Width="1280" WindowStyle="None" MaxWidth="{StaticResource {x:Static SystemParameters.MaximizedPrimaryScreenWidthKey}}" MaxHeight="{StaticResource {x:Static SystemParameters.MaximizedPrimaryScreenHeightKey}}">

  • 13
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
WPF 中,如果要使窗口遮住 Windows 任务栏,可以通过以下步骤来实现: 1. 在 Window 对象的 XAML 中,设置 WindowStyle 属性为 None,这将隐藏窗口的标题栏和边框。 ```xaml <Window x:Class="YourNamespace.YourWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Your Window" WindowStyle="None"> <!-- Window Content --> </Window> ``` 2. 在 Window 对象的 XAML 中,设置 WindowState 属性为 Maximized,这将使窗口最大化。 ```xaml <Window x:Class="YourNamespace.YourWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Your Window" WindowStyle="None" WindowState="Maximized"> <!-- Window Content --> </Window> ``` 3. 在代码中,调用 Win32 API 来隐藏任务栏。首先,创建一个名为 "NativeMethods" 的类,并导入以下命名空间: ```csharp using System; using System.Runtime.InteropServices; ``` 4. 在 "NativeMethods" 类中,定义一个名为 "ShowWindow" 的静态方法,用于调用 Win32 API 来显示或隐藏任务栏: ```csharp public static class NativeMethods { [DllImport("user32.dll")] public static extern int FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll")] public static extern int ShowWindow(int hwnd, int nCmdShow); } ``` 5. 在窗口的 Loaded 事件处理程序中,调用 "NativeMethods.ShowWindow" 方法来隐藏任务栏。在这之前,需要获取任务栏的窗口句柄并传递给 ShowWindow 方法: ```csharp private void Window_Loaded(object sender, RoutedEventArgs e) { int hwnd = NativeMethods.FindWindow("Shell_TrayWnd", ""); NativeMethods.ShowWindow(hwnd, 0); // 0 表示隐藏任务栏 } ``` 这样,当窗口加载时,它将以全屏模式显示,并且会遮住 Windows 任务栏。请注意,这种方法需要使用 P/Invoke 调用 Win32 API,因此需要添加相应的命名空间和引用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Super Blaze

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值