gpt4 book ai didi

wpf - 在 WPF 中,我可以拥有一个带有常规最小化、最大化和关闭按钮的无边框窗口吗?

转载 作者:行者123 更新时间:2023-12-04 16:22:20 27 4
gpt4 key购买 nike

如果您在最大化时查看 Chrome 浏览器,它的选项卡标题就位于窗口顶部。我可以做类似的事情吗?

最佳答案

当然可以,但是您将不得不自己重新制作这些按钮(这并不难,别担心)。

在您的 MainWindow.xaml 中:

<Window ...
Title="" Height="Auto" Width="Auto" Icon="../Resources/MyIcon.ico"
ResizeMode="NoResize" WindowStartupLocation="CenterScreen"
WindowStyle="None" AllowsTransparency="True" Background="Transparent"
...>
<Canvas>
<Button /> <!-- Close -->
<Button /> <!-- Minimize -->
<Button /> <!-- Maximize -->
<TabControl>
...
</TabControl>
</Canvas>
</Window>

然后,您只需要将 Buttons 和 TabControl 根据需要放置在 Canvas 上,并自定义外观和感觉。

编辑:.NET 4.5 中用于关闭/最大化/最小化的内置命令是 SystemCommands.CloseWindowCommand/ SystemCommands.MaximizeWindowCommand/ SystemCommands.MinimizeWindowCommand
因此,如果您使用的是 .NET 4.5,则可以执行以下操作:
<Window ...
Title="" Height="Auto" Width="Auto" Icon="../Resources/MyIcon.ico"
ResizeMode="NoResize" WindowStartupLocation="CenterScreen"
WindowStyle="None" AllowsTransparency="True" Background="Transparent"
...>
<Window.CommandBindings>
<CommandBinding Command="{x:Static SystemCommands.CloseWindowCommand}" CanExecute="CommandBinding_CanExecute_1" Executed="CommandBinding_Executed_1" />
<CommandBinding Command="{x:Static SystemCommands.MaximizeWindowCommand}" CanExecute="CommandBinding_CanExecute_1" Executed="CommandBinding_Executed_2" />
<CommandBinding Command="{x:Static SystemCommands.MinimizeWindowCommand}" CanExecute="CommandBinding_CanExecute_1" Executed="CommandBinding_Executed_3" />
</Window.CommandBindings>
<Canvas>
<Button Command="{x:Static SystemCommands.CloseWindowCommand}" Content="Close" />
<Button Command="{x:Static SystemCommands.MaximizeWindowCommand}" Content="Maximize" />
<Button Command="{x:Static SystemCommands.MinimizeWindowCommand}" Content="Minimize" />
<TabControl>
...
</TabControl>
</Canvas>
</Window>

在你的 C# 代码隐藏中:
    private void CommandBinding_CanExecute_1(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}

private void CommandBinding_Executed_1(object sender, ExecutedRoutedEventArgs e)
{
SystemCommands.CloseWindow(this);
}

private void CommandBinding_Executed_2(object sender, ExecutedRoutedEventArgs e)
{
SystemCommands.MaximizeWindow(this);
}

private void CommandBinding_Executed_3(object sender, ExecutedRoutedEventArgs e)
{
SystemCommands.MinimizeWindow(this);
}

这将使关闭/最大化/最小化与常规窗口完全一样。
当然,您可能想使用 System.Windows.Interactivity将 C# 移动到 ViewModel 中。

关于wpf - 在 WPF 中,我可以拥有一个带有常规最小化、最大化和关闭按钮的无边框窗口吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13930633/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com