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



如果您查看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>

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

编辑:.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 中。

您必须自己实现的按钮。调整大小和移动窗口,如果您设置WindowChrome.WindowChrome附加属性,设置GlassFrameThickness="0"也会删除阴影:

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

最新更新