天天看點

WPF動态改變主題顔色

程式允許使用者根據自己的喜好來對界面進行配色,這種技術在很多軟體中都有應用,比如這款名為AirPlay的音樂播放器軟體:

下面我們就來自己動手實作這種技術:

首先在App.xaml檔案中定義一個鍵值為“color”的單色筆刷,這個筆刷就是可以被使用者改變的動态資源:

<SolidColorBrush x:Key="color" Color="SkyBlue" />

然後來設計這樣一個界面:

我們讓使用者通過4個滑塊來分别定制顔色的A、R、G、B值,其完整代碼為:

<Grid>

    <Grid.RowDefinitions>

        <RowDefinition Height="28" />

        <RowDefinition Height="*" />

    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>

        <ColumnDefinition Width="65*" />

        <ColumnDefinition Width="213*" />

    </Grid.ColumnDefinitions>

    <Label Grid.Row="0" HorizontalContentAlignment="Right">透明度:</Label>

    <Label Grid.Row="1" HorizontalContentAlignment="Right">紅色:</Label>

    <Label Grid.Row="2" HorizontalContentAlignment="Right">綠色:</Label>

    <Label Grid.Row="3" HorizontalContentAlignment="Right">藍色:</Label>

    <Slider Grid.Row="0" Grid.Column="1" Margin="3" Name="a" SmallChange="1" LargeChange="15" Maximum="255" Value="255" />

    <Slider Grid.Row="1" Grid.Column="1" Margin="3" Name="r" SmallChange="1" LargeChange="15" Maximum="255" Value="255" />

    <Slider Grid.Row="2" Grid.Column="1" Margin="3" Name="g" SmallChange="1" LargeChange="15" Maximum="255" Value="255" />

    <Slider Grid.Row="3" Grid.Column="1" Margin="3" Name="b" SmallChange="1" LargeChange="15" Maximum="255" Value="255" />

    <Button Grid.Column="1" Grid.Row="4" HorizontalAlignment="Left" Margin="3,5,0,5" Name="button1" Width="75" Click="button1_Click">更新顔色</Button>

</Grid>

需注意,要把滑塊的最大值設為255。

然後回到App.xaml中,我們來定義視窗、标簽及按鈕的樣式:

視窗樣式代碼如下:

<Style x:Key="window" TargetType="Window">

    <Setter Property="OverridesDefaultStyle" Value="true" />

    <Setter Property="AllowsTransparency" Value="true" />

    <Setter Property="SnapsToDevicePixels" Value="true" />

    <Setter Property="WindowStyle" Value="None" />

    <Setter Property="Template">

        <Setter.Value>

            <ControlTemplate TargetType="Window">

                <Border BorderBrush="{DynamicResource color}" BorderThickness="3" CornerRadius="5" Padding="4">

                    <Border BorderBrush="{DynamicResource color}" BorderThickness="3" CornerRadius="5" Background="{DynamicResource color}">

                        <Border BorderBrush="#1000" BorderThickness="3" CornerRadius="5" Padding="6">

                            <Border.Background>

                                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">

                                    <GradientStop Color="#3FFF" Offset="0.5" />

                                    <GradientStop Color="#1666" Offset="0.5" />

                                </LinearGradientBrush>

                            </Border.Background>

                            <ContentPresenter />

                        </Border>

                    </Border>

                 </Border>

            </ControlTemplate>

        </Setter.Value>

    </Setter>

</Style>

定義樣式為幾個Border進行嵌套,在最底層引用的之前定義的單色筆刷,在上層用低不透明度的白色和黑色覆寫以産生不同的層次效果。

标簽樣式為:

<Style TargetType="Label">

    <Setter Property="Foreground" Value="#AAFFFFFF" />

按鈕樣式為:

<Style TargetType="Button">

            <ControlTemplate TargetType="Button">

                <Border Background="{DynamicResource color}" CornerRadius="3">

                    <Border BorderThickness="2" CornerRadius="3" Padding="{TemplateBinding Padding}">

                        <Border.Background>

                            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">

                                <GradientStop Color="#6FFF" Offset="0" />

                                <GradientStop Color="#2000" Offset="1" />

                            </LinearGradientBrush>

                        </Border.Background>

                        <Border.BorderBrush>

                                <GradientStop Color="#1000" Offset="0" />

                                <GradientStop Color="#4000" Offset="1" />

                        </Border.BorderBrush>

                        <TextBlock TextAlignment="Center" Foreground="#AFFF"><ContentPresenter /></TextBlock>

                </Border>

            </ControlTemplate>

其原理與視窗樣式相同。

然後回到主界面設計視窗,設定窗體的樣式:

Style="{StaticResource window}"

接下來編輯背景代碼,首先為窗體增加事件處理以提供拖動功能:

MouseLeftButtonDown="Window_MouseLeftButtonDown"

背景事件處理代碼:

private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

{

    this.DragMove();

}

為按鈕增加事件處理來更新界面顔色:

private void button1_Click(object sender, RoutedEventArgs e)

    更新顔色(Color.FromArgb((byte)a.Value, (byte)r.Value, (byte)g.Value, (byte)b.Value));

“更新顔色”方法代碼如下:

public void 更新顔色(Color c)

    this.Resources.Remove("color");

    this.Resources.Add("color", new SolidColorBrush(c));

此方法首先移除資源“color”,然後再添加一個同名的新筆刷,這樣就完成了動态替換工作。

現在編譯并執行程式,可以看到如下效果:

<a href="http://files.cnblogs.com/SkyD/WPF_STYLECOLOR.rar">源代碼下載下傳</a>

本文轉自斯克迪亞部落格園部落格,原文連結:http://www.cnblogs.com/SkyD/archive/2008/09/02/1281975.html,如需轉載請自行聯系原作者