【发布时间】:2018-09-19 06:49:14
【问题描述】:
我想对窗口标题栏中的关闭、最小化和最大化按钮进行自定义。在某些程序中,标题栏是不同的,但我不想更改完整的标题栏,只更改按钮。谁能帮我解决这个问题?
【问题讨论】:
-
改变它的外观,或者按下它时的作用?
我想对窗口标题栏中的关闭、最小化和最大化按钮进行自定义。在某些程序中,标题栏是不同的,但我不想更改完整的标题栏,只更改按钮。谁能帮我解决这个问题?
【问题讨论】:
一般来说,窗口的标题栏属于窗口的所谓非客户区。 这意味着您不能更改按钮的外观,也不能添加自定义按钮等。
为此,您需要滚动自己的窗口样式或使用为您执行此操作的库。
一些有用的教程或起点可能是this MSDN article 或how to create a custom window 上的本教程。
要创建自己的窗口样式,可以使用这个简单的样式作为基础:
<Style x:Key="MyCustomWindowStyle" TargetType="{x:Type Window}">
<Setter Property="WindowStyle" Value="None"/>
<Setter Property="AllowsTransparency" Value="True"/>
<Setter Property="Background" Value="White"/>
<Setter Property="BorderBrush" Value="Blue"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Grid Background="{TemplateBinding Background}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<!-- this displays the window title -->
<TextBlock TextAlignment="Center"
Text="{TemplateBinding Title}"/>
<StackPanel Grid.Column="1"
Orientation="Horizontal"
HorizontalAlignment="Stretch"
VerticalAlignment="Center">
<!-- the minimize button, using your own style -->
<Button Style="{StaticResource MyMinimizeButtonStyle}"
Width="20"
Height="20" />
<!-- the close button, using your own style -->
<Button Style="{StaticResource MyCloseButtonStyle}"
Width="20"
Height="20" />
</StackPanel>
<!-- this displays the actual window content -->
<ContentPresenter Grid.Row="1"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
【讨论】:
您可以使用 windowchrome 自定义窗口的镶边。 https://msdn.microsoft.com/en-us/library/system.windows.shell.windowchrome(v=vs.110).aspx
您可以使用它来完全重新模板化您的窗口。 请注意,自从编写以下内容以来,该类已移动。 https://blogs.msdn.microsoft.com/wpfsdk/2010/08/25/experiments-with-windowchrome/
这可能会产生奇怪的副作用,例如当您在 win10 中最大化窗口时。
【讨论】:
您可以使用WindowChrome自定义窗口样式:https://github.com/DinoChan/WindowDemo
此样式不使用 SystemParameters2 类,因为它在 .net 4.5 中不存在。
【讨论】: