造型窗口

时间:2013-01-21 19:56:58

标签: c# wpf styles windows-shell skinning

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

4 个答案:

答案 0 :(得分:2)

<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();
    }       
}

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

答案 1 :(得分: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>

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

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

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

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

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

要看的事情:

Window.Style Window.Resources Templates

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