原文:WPF动态改变主题颜色

国内的WPF技术先行者周银辉曾介绍过如何动态改变应用程序的主题样式,今天我们来介绍一种轻量级的改变界面风格的方式——动态改变主题色。

程序允许用户根据自己的喜好来对界面进行配色,这种技术在很多软件中都有应用,比如这款名为AirPlay的音乐播放器软件:

下面我们就来自己动手实现这种技术:

首先在App.xaml文件中定义一个键值为“color”的单色笔刷,这个笔刷就是可以被用户改变的动态资源:

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

然后来设计这样一个界面:

我们让用户通过4个滑块来分别定制颜色的A、R、G、B值,其完整代码为:

<Grid>

<Grid.RowDefinitions>

<RowDefinition Height="28" />

<RowDefinition Height="28" />

<RowDefinition Height="28" />

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

按钮样式为:

<Style TargetType="Button">

<Setter Property="Template">

<Setter.Value>

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

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

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

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

</LinearGradientBrush>

</Border.BorderBrush>

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

</Border>

</Border>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

其原理与窗口样式相同。

然后回到主界面设计窗口,设置窗体的样式:

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”,然后再添加一个同名的新笔刷,这样就完成了动态替换工作。

现在编译并执行程序,可以看到如下效果:

源代码下载

WPF动态改变主题颜色的更多相关文章

  1. vue+scss动态改变主题颜色

    1.新建.scss后缀公用文件,放在assets或者其他地方都可以 /*需要切换的颜色变量*/ $color-primary1:#1776E1; /* 更换的颜色 */ $color-primary2 ...

  2. Eclipse如何快速改变主题颜色

    厌倦了Eclipse的白底黑子,我们来更换下Eclipse的主题颜色,让眼睛更舒服一点 首先先进入网址:http://eclipsecolorthemes.org/ 选择一个主题进入,点击进入如下: ...

  3. jquery动态改变背景颜色插件

    GETHUB下载地址 背景颜色用animate方法时时无法改变颜色的 所以要使用插件进行补充. 用法: <!DOCTYPE html> <html> <head> ...

  4. WPF 动态改变窗口大小

    1.删除 Width 和 Height 属性:2.将 Windows.SizeToContent 属性设置为 WidthAndHeight这时窗口就能自动调整自身大小,从而容纳所包含的内容. 通过将 ...

  5. UGUI 用脚本动态改变Button颜色组合

    public Button button; void Start() { ColorBlock cb = new ColorBlock(); cb.normalColor = Color.red; c ...

  6. ExtJS动态改变字体颜色

    为按钮设置文本属性,用标签包裹变色. //pButton为按钮IDExt.getCmp('pButton').setText('<span style="color:#FF0000;& ...

  7. WPF 自己做一个颜色选择器

    程序开发过程中,经常会遇到需要支持动态配置主题颜色的问题,通常,一个程序会有多种不同的颜色风格主题供选 有时候,更细致一些的地方,会需要支持自己配置颜色,这样我们就需要一个颜色选择器啦,下面是我自己开 ...

  8. WPF:改变ListBoxItem和ListViewItem的颜色

    目录 1. 改变ListBoxItem颜色 2. ListViewItem的颜色设置 注意: 本文仅讨论默认ListBoxItem和ListViewItem的鼠标指向和被选择后的前景和背景颜色设置.如 ...

  9. WPF中利用后台代码实现窗口分栏动态改变

    在WPF中实现窗口分栏并能够通过鼠标改变大小已经非常容易,例如将一个GRID分成竖排三栏显示,就可以将GRID先分成5列,其中两个固定列放GridSplitter. <Grid Backgrou ...

随机推荐

  1. RapidCRC : Home

    RapidCRC : Home   What is RapidCRC? RapidCRC is a tool for windows for checking and creating CRC32 a ...

  2. Computational Geometry Template

    顿时觉得神清气爽!! #include <iostream> #include <math.h> #define eps 1e-8 #define zero(x) (((x)& ...

  3. VC 绘图技巧--自定义形状图形

    自定义形状图形,定义几个点围城的图形,然后进行描边和填充: if (m_memDC.m_hDC!=NULL) { CPoint point[4]; point[0].x=nLeft+(int)(0.1 ...

  4. 与众不同 windows phone (9) - Push Notification(推送通知)之概述, 推送 Toast 通知

    原文:与众不同 windows phone (9) - Push Notification(推送通知)之概述, 推送 Toast 通知 [索引页][源码下载] 与众不同 windows phone ( ...

  5. Delphi中WebBrowser自动填表模板

    unit Unit1;interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, ...

  6. STM8S---IO复用配置(STVP方式)

    1 说明 STM8S的IO复用用程序代码配置起来比較麻烦.通常是操作flash来操作option byte字节.配置寄存器更加麻烦,能够使用STM 标准外设驱动库来设置. 本文使用一种界面配置的方式来 ...

  7. STM32学习之路-SysTick的应用(时间延迟)

    开发板:奋斗V5 好~ 菜B要来搞实验了.. 前面已经说了SysTick的工作原理什么的了,这里就不说了.. 先来做第一个实验: 盗自奋斗的样例,嘿嘿, 用SysTick产生1ms的基准时间,产生中断 ...

  8. JAVA进阶----ThreadPoolExecutor机制(转)

    ThreadPoolExecutor机制 一.概述 1.ThreadPoolExecutor作为java.util.concurrent包对外提供基础实现,以内部线程池的形式对外提供管理任务执行,线程 ...

  9. C# openfiledialog设置filter属性后达不到过滤效果的原因之一

    此处用RichTextBox控件举例>>> 在窗体对应的类中处理Load事件可以为openfiledialog设置Filter的属性: private void Form1_Load ...

  10. 最佳新秀SSH(十三)——Spring集装箱IOC分析和简单的实现

    时间最近一段时期,"集装箱"这个词一直萦绕在我的耳边,连吃饭.睡在我的脑海里蹦来蹦去的. 由于这几天的交流时间.讨论,对于理解容器逐渐加深. 理论上的东西终归要落实到实践,今天就借 ...