简体   繁体   English

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

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

If you look at the Chrome browser when maximized, it has its tab headers right at the top of the window.如果您在最大化时查看 Chrome 浏览器,它的选项卡标题就位于窗口顶部。 Can I do something similar?我可以做类似的事情吗?

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.然后,您只需要将 Buttons 和 TabControl 根据需要放置在 Canvas 上,并自定义外观和感觉。

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.Interactivity to move the C# into a ViewModel.当然,您可能希望使用System.Windows.Interactivity将 C# 移动到 ViewModel 中。

The buttons you have to implement by yourself.您必须自己实现的按钮。 Resizing and moving window you can still have if you set WindowChrome.WindowChrome attached property, setting GlassFrameThickness="0" will also remove the shadow:如果设置WindowChrome.WindowChrome附加属性,则仍然可以调整大小和移动窗口,设置GlassFrameThickness="0"也将删除阴影:

<Window ...>
   <WindowChrome.WindowChrome>
       <WindowChrome GlassFrameThickness="0"/>
   </WindowChrome.WindowChrome>
</Window>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM