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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13930633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:39:50  来源:igfitidea点击:

In WPF, can I have a borderless window that has regular minimize, maximise and close buttons?

wpf

提问by James L

If you look at the Chrome browser when maximized, it has its tab headers right at the top of the window. Can I do something similar?

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

回答by Louis Kottmann

Absolutely, but you're going to have to remake those buttons yourself (it's not hard, don't worry).

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

In your MainWindow.xaml:

在您的 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>

Then you just have to place the Buttons and the TabControl as wished on the Canvas, and customize the look and feel.

然后,您只需将按钮和 TabControl 根据需要放置在画布上,并自定义外观。

EDIT: The built in commands for closing/maximizing/minimizing in .NET 4.5 are SystemCommands.CloseWindowCommand/ SystemCommands.MaximizeWindowCommand/SystemCommands.MinimizeWindowCommand

编辑:内置的用于关闭命令/最大化/最小化在.NET 4.5的SystemCommands.CloseWindowCommand/ SystemCommands.MaximizeWindowCommand/SystemCommands.MinimizeWindowCommand

So if you're using .NET 4.5, you can do:

因此,如果您使用 .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>

And in your C# code-behind:

在你的 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);
    }

This will make close/maximize/minimize works exactly like with a regular window.
Of course, you may want to use System.Windows.Interactivityto move the C# into a ViewModel.

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