2

有没有办法将样式应用于 WPF Windows?例如:更改最小化、最大化和 X 按钮?我知道图形是 Windows shell 的一部分,而不是 WPF,所以我不确定它是否可能。

4

4 回答 4

3
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
x:Class="AccountOperator.CustomWindowStyle"
x:Name="Window"
Title="CustomWindowStyle"
Width="640" Height="480" WindowStyle="None" Margin="0" BorderThickness="2">

<Window.Resources>

    <Style x:Key="MaxButtonStyle" TargetType="{x:Type Button}">         
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border  BorderBrush="#535666" BorderThickness="2,5,2,2" Width="20" Height="15" Background="White"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="MinButtonStyle" TargetType="{x:Type Button}">

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border  Width="20" Height="7"  BorderBrush="#535666" BorderThickness="2" Background="#FFFFFF" HorizontalAlignment="Left" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


    <Style x:Key="CloseButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <Border Width="18" Height="4" BorderThickness="2" BorderBrush="#535666" RenderTransformOrigin="0.5,0.5">
                            <Border.RenderTransform>
                                <TransformGroup>
                                    <ScaleTransform/>
                                    <SkewTransform/>
                                    <RotateTransform Angle="50"/>
                                    <TranslateTransform/>
                                </TransformGroup>
                            </Border.RenderTransform>
                        </Border>
                        <Border Width="18" Height="4" BorderThickness="2" BorderBrush="#535666" RenderTransformOrigin="0.5,0.5">
                            <Border.RenderTransform>
                                <TransformGroup>
                                    <ScaleTransform/>
                                    <SkewTransform/>
                                    <RotateTransform Angle="-50"/>
                                    <TranslateTransform/>
                                </TransformGroup>
                            </Border.RenderTransform>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

<Grid x:Name="LayoutRoot">
    <StackPanel  Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Right">      
        <Button x:Name="BtnMax" Width="20" Height="15" Click="BtnMax_Click" Style="{DynamicResource MaxButtonStyle}" Margin="10"/>
        <Button x:Name="BtnClose" Height="18" Width="20" Margin="10"  Style="{DynamicResource CloseButtonStyle}" Click="BtnClose_Click"/>
        <Button x:Name="BtnMin" Width="20" Height="20" Style="{DynamicResource MinButtonStyle}" Click="BtnMin_Click" Margin="10"/>
    </StackPanel>
</Grid>

public partial class CustomWindowStyle : Window
{
    public CustomWindowStyle()
    {
        this.InitializeComponent();
    }

    private void BtnMax_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        this.WindowState = WindowState.Maximized;
    }

    private void BtnMin_Click(object sender, System.Windows.RoutedEventArgs e)
    {           
        this.WindowState = WindowState.Minimized; 
    }

    private void BtnClose_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        this.Close();
    }       
}

您只需复制并粘贴上面的代码即可得到您想要的结果。

于 2013-01-22T09:27:59.417 回答
1

您可以使用以下项目来完成您的任务:wpfwindow.codeplex.com

在此项目中是文件“ButtonIcons.xaml”,您可以更改这些按钮的属性。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

  <!-- MINIMIZE -->
  <Border x:Key="WindowButtonMinimizeIcon" Width="12" Height="5" Margin="0,5,0,0" BorderBrush="#535666" BorderThickness="1" Background="#FFFFFF" />

  <!-- MINIMIZE (disabled) -->
  <Border x:Key="WindowButtonMinimizeIconDisabled" Width="12" Height="5" Margin="0,5,0,0" 
          BorderBrush="#9FA5B2" BorderThickness="1" Background="#FFFFFF" />

  <!-- RESTORE -->
  <Canvas x:Key="WindowButtonRestoreIcon">
    <Rectangle Stroke="#535666" Fill="White" Width="10" Height="10" Canvas.Top="1" Canvas.Left="7" />
    <Rectangle Stroke="#535666" Fill="White" Width="4" Height="4" Canvas.Top="4" Canvas.Left="10" />
    <Rectangle Stroke="#535666" Fill="White" Width="10" Height="10" Canvas.Top="3" Canvas.Left="5" />
    <Rectangle Stroke="#535666" Fill="White" Width="4" Height="4" Canvas.Top="6" Canvas.Left="8" />
  </Canvas>

  <!-- RESTORE (disabled) -->
  <Canvas x:Key="WindowButtonRestoreIconDisabled">
    <Rectangle Stroke="#9FA5B2" Fill="White" Width="10" Height="10" Canvas.Top="1" Canvas.Left="7" />
    <Rectangle Stroke="#9FA5B2" Fill="White" Width="4" Height="4" Canvas.Top="4" Canvas.Left="10" />
    <Rectangle Stroke="#9FA5B2" Fill="White" Width="10" Height="10" Canvas.Top="3" Canvas.Left="5" />
    <Rectangle Stroke="#9FA5B2" Fill="White" Width="4" Height="4" Canvas.Top="6" Canvas.Left="8" />
  </Canvas>

  <!-- MAXIMIZE -->
  <Border x:Key="WindowButtonMaximizeIcon" BorderBrush="#535666" BorderThickness="1" Width="12" Height="10">
    <Border BorderBrush="#FCFCFC" BorderThickness="2">
      <Border BorderBrush="#535666" BorderThickness="1" />
    </Border>
  </Border>


  <!-- MAXIMIZE (disabled) -->
  <Border x:Key="WindowButtonMaximizeIconDisabled" BorderBrush="#9FA5B2" BorderThickness="1" Width="12" Height="10">
    <Border BorderBrush="#FCFCFC" BorderThickness="2">
      <Border BorderBrush="#9FA5B2" BorderThickness="1" />
    </Border>
  </Border>

  <!-- CLOSE -->
  <Image Source="/CustomWindow;component/Images/buttonX.png" Width="13" Height="10" x:Key="WindowButtonCloseIcon" />

  <!-- Background colors for red button (e.g close button) -->
  <LinearGradientBrush x:Key="RedButtonBackground" StartPoint="0,0" EndPoint="0,1">
    <GradientStop Offset="0" Color="#F89C8C" />
    <GradientStop Offset="0.45" Color="#D47F75" />
    <GradientStop Offset="0.45" Color="#C04C3C" />
    <GradientStop Offset="1" Color="#C98172" />
  </LinearGradientBrush>

  <LinearGradientBrush x:Key="RedButtonMouseOverBackground" StartPoint="0,0" EndPoint="0,1">
    <GradientStop Offset="0" Color="#F89C8C" />
    <GradientStop Offset="0.45" Color="#E36A53" />
    <GradientStop Offset="0.45" Color="#C72B0E" />
    <GradientStop Offset="0.75" Color="#D44310" />
    <GradientStop Offset="1" Color="#F5E478" />
  </LinearGradientBrush>

</ResourceDictionary>
于 2013-01-21T20:14:55.267 回答
0

您将不得不开始修改 xaml。您可能想从更改背景等内容开始。

这并不像我想象的那么简单。最初,我认为您只需将所需的按钮更改为您创建的按钮。这里有点复杂。例如,使按钮看起来不同并不像更改按钮背景那么简单。

您需要制作特定的控件模板,或将资源应用于项目、图像,并通过 XAML 分别制作styletemplate更改图像的设置。

我这样做的一种方法是添加图像,并根据图像制作自定义控件。不需要按钮或其他控件。onclick然后我为和 鼠标悬停(等等)创建了事件,所以我有效地制作了一个看起来像按钮的小图像,就像一个按钮。

要看的东西:

Window.Style Window.Resources Templates

老实说,找到这些答案的最简单方法是询问 Google。您可以询问任何内容,但请确保将WPF添加到其末尾。这样,您只能找到 WPF 特定编码的答案,很多人的操作方式不同,但您会发现大量出色的 xaml 示例等。

于 2013-01-21T20:27:52.870 回答
0

是的,您可以根据需要设置窗口样式。
不幸的是,如果您想设计标题栏、系统按钮、窗口的透明度或形状,通常是全有或全无的问题。

首先,设置Window.WindowStyle = WindowStyle.None
然后你可以创建新的 wpf 样式ContolTemplate
您可能需要编写一些代码来处理窗口拖动、标题栏双击、调整大小等。

于 2013-01-21T20:10:17.467 回答