c# - 无边框窗口无法正确最大化

标签 c# wpf xaml

好吧,我已经在 Google 上搜索了几个小时,但似乎无法找到我遇到的问题的直接答案。我有一个带有 WindowStyle = "None"AllowsTransparency = "True" 的自定义窗口 当我点击最大化按钮时:

    private void MaximizeButton_Click(object sender, RoutedEventArgs e)
    {
        if(this.WindowState == WindowState.Normal)
        {

            App.Current.MainWindow.WindowState = WindowState.Maximized;
        }
        else
        {
            App.Current.MainWindow.WindowState = WindowState.Normal;
        }
    }

除了窗口的顶部和左侧似乎有 -6 像素的边距外,它几乎完全按照预期的方式最大化。

它是这样的:enter image description here

我不希望那里有那个空白(它之所以是白色,是因为谷歌浏览器在它后面打开,它实际上是透明的)。我需要应用程序最大化以适应整个屏幕,不包括任务栏。到目前为止,我发现的唯一修复方法是在按下最大化按钮时将窗口边距设置为 Margin = "6, 6, 0, 0"。以下是供引用的其余代码:

StartUp.xaml

<Window x:Class="Expense_Calculator.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Expense_Calculator"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525"
    WindowStartupLocation="CenterScreen"
    WindowStyle="None"
    AllowsTransparency="True">
<Grid Name="Container" Background="#323232">
    <Grid.RowDefinitions>
        <RowDefinition Height="33"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid>
        <DockPanel Style="{StaticResource TitleDockPanel}">
            <Label Style="{StaticResource TitleBarTitle}">App Name</Label>
            <Button Name="CloseButton" Click="CloseButton_Click" DockPanel.Dock="Right" Style="{StaticResource TitleBarButtonClose}">
                <Image Source="images/close.png"/>
            </Button>
            <Button Name="MaximizeButton" Click="MaximizeButton_Click" DockPanel.Dock="Right" Style="{StaticResource TitleBarButton}">
                <Image Source="images/maximize.png"/>
            </Button>
            <Button Name="MinimizeButton" Click="MinimizeButton_Click" DockPanel.Dock="Right" Style="{StaticResource TitleBarButton}">
                <Image Source="images/minimize.png"/>
            </Button>
        </DockPanel>
    </Grid>
    <Grid Style="{StaticResource UserArea}" Grid.Row="1">
        <Grid Name="WelcomePage">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Label Style="{StaticResource Label1}">Welcome to your Expense Calculator!</Label>
            <Button Cursor="Hand" Style="{StaticResource Button1}" Grid.Row="1">Get Started</Button>
        </Grid>
    </Grid>
</Grid>

StartUp.xaml.cs

using System.Windows;

namespace Expense_Calculator
{
    /// <summary>
    /// Interaction logic for StartUp.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.MaxHeight = SystemParameters.WorkArea.Height;
            this.MaxWidth = SystemParameters.WorkArea.Width;
            InitializeComponent();
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void MaximizeButton_Click(object sender, RoutedEventArgs e)
        {
            if(this.WindowState == WindowState.Normal)
            {
                App.Current.MainWindow.WindowState = WindowState.Maximized;
            }
            else
            {
                App.Current.MainWindow.WindowState = WindowState.Normal;
            }
        }

        private void MinimizeButton_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }
    }
}

App.xaml

<Application x:Class="Expense_Calculator.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:Expense_Calculator"
         StartupUri="StartUp.xaml">
<Application.Resources>

    <!--Title Bar-->
    <Style x:Key="TitleDockPanel" TargetType="DockPanel">
        <Setter Property="VerticalAlignment" Value="Top"/>
        <Setter Property="Background" Value="#323232"/>
        <Setter Property="Height" Value="33"/>
    </Style>
    <Style x:Key="TitleBarTitle" TargetType="Label">
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="FontSize" Value="13"/>
        <Setter Property="FontWeight" Value="DemiBold"/>
        <Setter Property="Padding" Value="10, 0"/>
    </Style>
    <Style x:Key="TitleBarButton" TargetType="Button">
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="HorizontalAlignment" Value="Right"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border x:Name="border" Background="#323232" Height="33" Width="33">
                        <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Width="15" Height="15"></ContentPresenter>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition GeneratedDuration="0:0:.1"/>
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <ColorAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="Background.Color" To="#464646" Duration="0"/>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ColorAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="Background.Color" To="#3774FF" Duration="0"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="TitleBarButtonClose" TargetType="Button">
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="HorizontalAlignment" Value="Right"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border x:Name="border" Background="#323232" Height="33" Width="33">
                        <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Width="15" Height="15"></ContentPresenter>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition GeneratedDuration="0:0:.1"/>
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <ColorAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="Background.Color" To="Firebrick" Duration="0"/>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ColorAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="Background.Color" To="#781414" Duration="0"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <!--End - Title Bar-->

    <!--Welcome Page-->
    <Style x:Key="UserArea" TargetType="Grid">
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>
    <Style x:Key="Label1" TargetType="Label">
        <Setter Property="FontSize" Value="20"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Margin" Value="0, 0, 0, 25"/>
    </Style>
    <Style x:Key="Button1" TargetType="Button">
        <Setter Property="Width" Value="Auto"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border x:Name="border" Background="#323232" CornerRadius="16" BorderBrush="#505050" BorderThickness="1" Padding="15, 6">
                        <ContentPresenter x:Name="content" HorizontalAlignment="Center" VerticalAlignment="Center">
                            <TextBlock.Foreground>
                                <SolidColorBrush Color="#7D7D7D"/>
                            </TextBlock.Foreground>
                            <TextBlock.FontSize>14</TextBlock.FontSize>
                        </ContentPresenter>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition GeneratedDuration="0:0:0.15"/>
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <ColorAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="Background.Color" To="#3C3C3C" Duration="0"/>
                                        <ColorAnimation Storyboard.TargetName="content" Storyboard.TargetProperty="(TextBlock.Foreground).Color" To="White" Duration="0"/>
                                        <ColorAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="BorderBrush.Color" To="#C8C8C8" Duration="0"/>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ColorAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="Background.Color" To="#282828" Duration="0"/>
                                        <ColorAnimation Storyboard.TargetName="content" Storyboard.TargetProperty="(TextBlock.Foreground).Color" To="White" Duration="0"/>
                                        <ColorAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="BorderBrush.Color" To="#C8C8C8" Duration="0"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </Border>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="border" Property="Opacity" Value=".25"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <!--End - Welcome Page-->
</Application.Resources>
</Application>

最佳答案

根据设计(出于什么原因,我不知道),当您使用 WindowStyle="None" 并最大化窗口时,它会超出屏幕的实际边缘几个四面都是像素。

在您的代码中,您将窗口的实际大小限制为工作区的确切尺寸。由于窗口的最大化仍然将窗口的左上角那几个像素放在工作区左上角的左侧和上方,因此窗口的可见部分必然小于工作的整个宽度区域,因此右侧和底部的暴露区域。

As noted by commenter Evk ,通过删除窗口的大小限制(如果您愿意,您只能在窗口最大化时执行此操作),窗口可以扩展到 WPF 想要的完整大小,确保完全覆盖工作区域。

在您的后续评论中,不清楚您是否真的希望覆盖任务栏。无论哪种情况,您都可能会发现这些链接有助于满足您在这方面的特定需求:
Maximize window with WindowState Problem (application will hide windows taskbar)
Maximizing window (with WindowStyle=None) considering Taskbar

或者,您仍然可以设置大小限制,但要考虑到 WPF 在窗口最大化时坚持的额外像素边距,将尺寸设置得比需要的大,这样就没有暴露区域。

对于它的值(value),这里有一个简化的代码示例,它只关注这里的特定行为:

<Window x:Class="TestSO39578992MaximizeBorderless.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:p="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestSO39578992MaximizeBorderless"
        mc:Ignorable="d" Background="Yellow" 
        WindowStyle="None" AllowsTransparency="True"
        Title="MainWindow" Height="350" Width="525">
  <Window.Style>
    <p:Style TargetType="Window">
      <Setter Property="WindowState" Value="Normal"/>
      <!-- Uncomment "Topmost" setters to experiment with its effect on the task bar visibility -->
      <!--<Setter Property="Topmost" Value="False"/>-->
      <p:Style.Triggers>
        <DataTrigger Binding="{Binding IsChecked, ElementName=checkBox1}" Value="True">
          <Setter Property="WindowState" Value="Maximized"/>
          <!--<Setter Property="Topmost" Value="True"/>-->
        </DataTrigger>
      </p:Style.Triggers>
    </p:Style>
  </Window.Style>
  <!-- set the margin here, to account for the extra space WPF is adding -->
  <!-- <Grid Margin="6"> -->
  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition/>
      <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition/>
      <RowDefinition/>
    </Grid.RowDefinitions>
    <CheckBox x:Name="checkBox1" Content="Maximized" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    <TextBlock Text="Upper Right" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Top"/>
    <TextBlock Text="Lower Left" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Bottom"/>
    <TextBlock Text="Lower Right" Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Column="1"/>
  </Grid>
</Window>

当然还有:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        this.MaxHeight = SystemParameters.WorkArea.Height;
        this.MaxWidth = SystemParameters.WorkArea.Width;

        // Compensate for the extra space WPF adds by increasing the max width and height here
        //this.MaxHeight = SystemParameters.WorkArea.Height + 12;
        //this.MaxWidth = SystemParameters.WorkArea.Width + 12;

        InitializeComponent();
    }
}

我在所有四个角中都包含了 TextBlock 元素,以便更轻松地查看窗口大小如何受各种属性值的影响。

关于c# - 无边框窗口无法正确最大化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39578992/

相关文章:

c# - HttpUtility.UrlEncode 标准编码与指定编码?

c# - 用以前的详细信息填写表格并找到清除表格的方法?

WPF 应用程序消息循环和 PostThreadMessage

xaml - 使用 Fontsize、FontAttributes 和 Horizo​​ntalOptions 时出现未处理的异常错误

xaml - 在 WinRT/Windows 应用商店应用程序中安全地存储加密 key

c# - XAML 标签 : binding text content when using text styles

c# - 将两种扩展方法合二为一

c# - 为什么我不能两次读取 Http Request Input 流?

c# - 为什么这个正则表达式匹配额外的字段?

c# - 使用c#在wpf listview中添加图像