无边框窗口无法正确最大化

2023-11-23

好吧,我已经在谷歌上搜索了几个小时了,似乎找不到我遇到的问题的直接答案。我有一个自定义窗口WindowStyle = "None" and 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;
        }
    }

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

Here's what it looks like: enter image description here

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

启动.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>

我遇到了同样的问题,但是通过调整窗口大小

Me.ResizeMode = ResizeMode.NoResize 

将窗口状态更改为最大化后,解决了该问题

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无边框窗口无法正确最大化 的相关文章

随机推荐

  • 方法 openOrCreateDatabase(String, int, null) 未定义

    我正在尝试按如下方式打开数据库 SQLiteDatabase myDatabase myDatabase openOrCreateDatabase sudoku db Context MODE PRIVATE null 当我在 Servic
  • 在数据库表中动态添加列的设计模式

    用户希望在 UI 中动态添加新字段 这个新字段应该存储在数据库中 并且应该允许他们对其执行 CRUD 现在我可以通过指定 XML 来完成此操作 但我想要一种更好的方法来搜索这些新列 还有开枪的想法ALTER声明并添加新列似乎是错误的 任何人
  • Sharepoint:基于另一个列表的计算列

    我可能正在寻找错误的方法来做到这一点 但我在这里 我有一个 Sharepoint 列表 标题为 行程 其中包含安排到特定区域的行程列表 我有第二个列表 区域 其中包含固定数量的项目 每个项目代表一个特定区域 区域有一个标题为 条件 的列 描
  • Robolectric 启动期间反射出现 NullPointerException - 有任何提示吗?

    我正在 Eclipse 上开发一个 Android 项目 并且我正在尝试从在模拟器 设备上运行测试 即very慢 到机器人电动 我用 Robolectric 替换了 Android 库 将 JUnit 添加到路径中 将测试用例改回常规Tes
  • 使用Java从串口读取文件

    我是Java初学者 我正在写 FLASH getbytes 像这样serialport 之后我会得到FLASH OK作为回应 我必须再次发送文件请求 之后我会得到回复FILE OK然后我已读取文件直至文件末尾 我不知道如何做到这一点 所以请
  • 提高python中重心坐标计算的效率

    背景 我试图将一张脸扭曲成另一张不同形状的脸 为了将一张图像扭曲为另一张图像 我使用了面部标志的 delaunay 三角剖分 并将一幅肖像的三角形扭曲为第二幅肖像的相应三角形 我使用重心坐标系将三角形内的点映射到另一个三角形上相应的扭曲位置
  • 如何将 mingw-w64 和 MSYS2 与 eclipse 或 codeblocks 等 IDE 一起使用?

    我安装 msys2here 我下载i686 5 3 0 release posix dwarf rt v4 rev0 from here 设置后msys2 i686 20160205 exe 提炼i686 5 3 0 release pos
  • 我的 xpage 应用程序的基本 REST 服务

    我想为我的 XPage 应用程序设置一些基本的 REST 服务 因此 我在 xpage 上添加了 xe restService 控件 并选择 xe customRestService 其中我引用了 Java 类
  • 禁用时更改 FAB 背景颜色

    我试图弄清楚如何在按下浮动操作按钮后禁用 2 秒的持续时间时更改浮动操作按钮的背景颜色 我还希望它在 2 秒持续时间结束时恢复到原来的颜色 这是按下时 2 秒延迟的代码 此代码位于 MainActivity 内的一个片段中 appBar s
  • 如何根据 StatefulSets 中的序数索引将参数传递给 pod?

    是否可以根据 Pod 在 StatefulSet 中的序号索引将不同的参数传递给 Pod 在 StatefulSets 文档上没有找到答案 谢谢 推荐方式参见https kubernetes io docs tasks run applic
  • SonarQube 支持 Java 8 吗?

    使用 Java 8 执行gradle sonarRunner显示此错误消息 声纳Qube版本 4 2 1 java lang ArrayIndexOutOfBoundsException 26721 at org objectweb asm
  • 决赛选手有什么用?

    我已经用 NET 编程四年了 主要是 C 并且广泛使用 IDiposable 但我尚未发现需要终结器 决赛选手有什么用 终结器是确保正确清理某些内容的最后一搏 通常为包装对象保留不受管理的资源 例如非托管句柄等不会被垃圾收集 编写终结器确实
  • 设置 Git 远程 SSH (git-upload-pack / git-receive-pack)

    我有一台具有 SSH 访问权限的服务器 我想在那里放置一个 Git 原始存储库 我刚刚在本地创建了一个 bare shared 存储库 并将其复制到每个 SCP 的服务器上 现在我想根据 SSH url 克隆这个存储库 我试过 ssh US
  • PHP 将字符串转换为 slug

    将文本字符串转换为 slug 的最佳方法是什么 意义 允许使用 alpha 转换为小写 允许数字 应消除空格 而不是转换为破折号 重音字符被等效的标准字母替换 不允许使用其他字符 应将其删除 我在网上找到了大量代码 但它们都倾向于将空格转换
  • 熊猫:分组列出

    我有如下数据 id value time 1 5 2000 1 6 2000 1 7 2000 1 5 2001 2 3 2000 2 3 2001 2 4 2005 2 5 2005 3 3 2000 3 6 2005 我的最终目标是将数
  • 使用 jQueryUI 的新自动完成功能的多列结果的快速示例?

    我刚刚发现jQueryUI 现在拥有自己的内置自动完成组合框 好消息 不幸的是 我发现的下一件事是 使其成为多列似乎并不那么简单 至少通过文档 有一个在这里发帖有人提到他们已经做到了 甚至提供了代码 但我无法理解他们的一些代码在做什么 我只
  • SecItemAdd 和 SecItemCopyMatching 返回错误代码 -34018 (errSecMissingEntitlement)

    有时 当我从 Xcode 在设备上运行应用程序时 我会尝试访问钥匙串 但由于错误 34018 而失败 这与任何记录的钥匙串错误代码都不匹配 并且无法一致地重现 大概有 30 的时间发生 我不清楚为什么会发生 完全缺乏文档使得调试这个问题变得
  • 如何更改/更新/删除 Spring ConfigurableEnvironment 中的属性

    在 Spring 中 您可以使用注入环境对象来读取所有环境属性 Resource private org springframework core env Environment environment 所以问题是我可以以编程方式更改某些属
  • React 类中的清除间隔

    因此 我们有这个简单的 React 组件 它从父组件接收一个整数 单击该按钮后 我们会在屏幕上显示整数并开始倒计时 问题是如何停止倒计时 在阅读其他 SO 帖子时 我发现了clearInterval 但似乎我在这里遗漏了一些东西 任何帮助将
  • 无边框窗口无法正确最大化

    好吧 我已经在谷歌上搜索了几个小时了 似乎找不到我遇到的问题的直接答案 我有一个自定义窗口WindowStyle None and AllowsTransparency True 当我点击最大化按钮时 private void Maximi