2013-01-21 51 views
2

有沒有辦法將樣式應用於WPF Windows?例如:更改最小化,最大化和X按鈕?我知道圖形是Windows外殼的一部分,而不是WPF,所以我不確定它是否可行。造型窗口

+0

我已經看到它在Photoshop中完成了。我對此表示懷疑。抱歉。 –

+0

@ofstream這是絕對有可能的,而且非常普遍。 – tnw

+0

^我不知道。請繼續... –

回答

0

是的,您可以根據需要設計窗口樣式。
不幸的是,如果您想要標題欄,系統按鈕,窗口的透明度或形狀的樣式,通常是全部或全部問題。

首先,設置Window.WindowStyle = WindowStyle.None
然後,您可以使用ContolTemplate創建新的wpf樣式。
可能您需要編寫一些代碼來處理窗口拖動,標題欄雙擊,調整大小等。

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> 
+0

這很棒,但它或多或少只是在OP上拋出答案。當我不斷向人們重複迭代時,當提問者的觀點顯示沒有動機時,不要僅僅給出答案和代碼。不一定讓人們爲之付出努力,而是給他們一個啓動。 – plast1K

0

您將不得不開始修改xaml。你可能想要開始改變背景之類的東西。

這不像我想的那麼簡單。最初,我認爲你只需將你需要的按鈕更改爲你創建的按鈕。這裏有點複雜。例如,讓按鈕看起來不同並不像改變按鈕背景那麼簡單。

您需要製作特定的控制模板,或者將資源應用到項目,圖像以及通過XAML製作styletemplate,它們分別更改圖像的設置。

我做到這一點的一種方式是添加圖像,並根據圖像製作自定義控件。不需要按鈕或其他控件。然後,我創建了onclick的事件,並在鼠標懸停(等等)上,因此我有效地製作了一個看起來像一個按鈕的小圖像,就像一個按鈕。

事情來看待:

Window.Style Window.Resources Templates

老實說,找到這些問題的答案最簡單的方法是問谷歌。你可以問任何事情,但確保你添加WPF到它的末尾。這樣一來,你只能找到WPF具體編碼的答案,很多人不同的操作,但你會發現噸的巨大XAML示例等

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

你只需複製粘貼上面的代碼可以得到你想要的結果。