2016-09-19 56 views
3

好的,所以我一直在搜索谷歌几个小时,似乎无法找到我遇到的问题的直接答案。我有WindowStyle = "None"AllowsTransparency = "True"自定义窗口当我在我的最大化按钮点击:WPF - 无界限窗口无法正确最大化

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

它最大限度几乎完全相同的方式,它应该只是看起来像有窗口的顶部和左侧-6px保证金。

Here's what it looks like

我不想说白了空间,在那里(这是唯一的白人,因为谷歌Chrome是它背后打开的,它实际上是透明的)。我需要应用程序最大限度地适应整个屏幕,不包括任务栏。到目前为止,我发现的唯一修复方法是在按下最大化按钮时将窗口边距设置为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> 
+0

尝试删除this.MaxHeight = SystemParameters.WorkArea.Height和this.MaxWidth = SystemParameters.WorkArea.Width;来自构造函数。 – Evk

+0

当我这样做时,应用程序最大化以适应整个屏幕。所以它涵盖了任务栏。它虽然摆脱了所有的空白空间。但它也似乎重叠的双方,你可以看到[这里](http://imgur.com/1HDswCV) –

回答

4

通过设计(出于何种原因,我不知道),当您有WindowStyle="None"并且您最大化窗口时,它将延伸到屏幕的实际边缘以外的所有边上几个像素。

在您的代码中,您将窗口的实际大小限制为工作区域的确切尺寸。由于窗口的最大化仍将窗口的左上角放置在工作区域的左上角和左上角的几个像素上,窗口的可见部分必然小于工作的整个宽度区域,因此右侧和底部的暴露区域。通过删除窗口上的大小限制(只有当窗口最大化时,如果你喜欢,你可以做到这一点),窗口可以扩展到WPF想要的全尺寸,确保工作区域的全部覆盖。

在您的后续评论中,您是否确实想要覆盖任务栏还不清楚。在这两种情况下,你可能会发现,解决在这方面您的特定需求有用的这些链接:
Maximize window with WindowState Problem (application will hide windows taskbar)
Maximizing window (with WindowStyle=None) considering Taskbar

或者,你仍然可以设置大小限制,但考虑到WPF坚持追加像素保证金在窗户最大化时,将尺寸设置得比需要的大,这样就没有暴露的区域。

对于它的价值,这里是仅仅着眼于特定的行为在这里的简化代码示例:

<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元素四个角,以便更容易地了解窗口大小如何受各种属性值的影响。

+0

我真的不理解你的[第二链接](https://blogs.msdn.microsoft.com/llobo/2006/08/01 /最大化窗口与 - windowstylenone-考虑,任务栏/)。主要是因为我不确定如何使用它。但是当我使用你的代码时,它仍然是一样的结果。每当我使用'this.MaxHeight = SystemParameters.WorkArea.Height + 6;'(并且宽度相同),它覆盖整个屏幕。但工作领域似乎并不重要。顶部和左侧仍然被切断。它似乎是工作区域是6px并且离开。另外我试图不掩盖任务栏。 –

+0

要清楚:我上面的代码不是解决方案,它只是一种重现问题的方法,不需要您原始示例显示的所有额外内容。请注意,除了使窗口更大(通过在设置最大高度和宽度时添加补偿),还需要为窗口内容元素设置“边距”,以便窗口内容布局本身也占额外利润率WPF正在增加。我将使用注释行编辑代码示例以显示我的意思。 –

+0

好吧,我错了,我误解了你的答案。我很感谢你的详细回答。这确实解决了问题,所以我要接受你的答案。感谢) –