WPF整理-使用用户选择主题的颜色和字体

摘要:
&lt:RectangleStroke=“Black”Height=“50”Fill=“{x;&lt:TextBlockText=“HellofromActiveCaptionFont”FontFamily=“{DynamicResource{x!StaticSystemColors.DesktopBrushKey}”/>

“Sometimes it's useful to use one of the selected colors or fonts the user has chosen in the
Windows Control Panel Personalization applet (or the older Display Settings in Windows XP),
such as Window caption, Desktop color, and Selection color. Furthermore, an application
may want to react dynamically to changes in those values. This can be achieved by accessing
special resource keys within the SystemColors and SystemFonts classes.”

有时候,我们想要使用用户在控制面板中选择的主题的颜色,或是我们希望我们的程序能够跟着用户选择不同的主题,变换相应的颜色。这时候我们可以通过访问SystemColors和SystemFonts类的特定的Resource keys。

Code Snip如下:

<Window x:Class="UsingUserSelectedColorsAndFonts.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel >
        <TextBlock Text="Hello from Active Caption Font" FontFamily="{ DynamicResource {x:Static SystemFonts.CaptionFontFamilyKey}}" FontSize="{ DynamicResource {x:Static SystemFonts.CaptionFontSizeKey}}"/>
        <Rectangle Height="100" Stroke="DarkViolet" StrokeThickness="10" Fill="{DynamicResource {x:Static SystemColors.DesktopBrushKey}}"/>           
    </StackPanel>
</Window>

注意这个,和前面的WPF整理-XAML访问静态属性 的对比:

"XAML provides an easy way to set values of properties—type converters and the extended property syntax allow for flexible setting of values. However, some things cannot be expressed as a simple value, such as setting a property to the value of some static property."

这个相对比较简单,知道就行,Code Snip如下:

    <StackPanel>
        <Ellipse Stroke="Black" Height="50" Fill="{x:Static SystemColors.DesktopBrush}"/>
        <Rectangle Stroke="Black" Height="50" Fill="{x:Static SystemColors.ActiveCaptionBrush}"/>
    </StackPanel>

两者采用不同的方法实现相同的效果。

对比如下:

    <StackPanel >
        <TextBlock Text="Hello from Active Caption Font" FontFamily="{ DynamicResource {x:Static SystemFonts.CaptionFontFamilyKey}}" FontSize="{ DynamicResource {x:Static SystemFonts.CaptionFontSizeKey}}"/>
        <Rectangle Height="100" Stroke="DarkViolet" StrokeThickness="10" Fill="{DynamicResource {x:Static SystemColors.DesktopBrushKey}}"/>        
        <StackPanel>
            <TextBlock Text="Hello from Active Caption Font" FontFamily="{ x:Static SystemFonts.CaptionFontFamily}" FontSize="{ x:Static SystemFonts.CaptionFontSize}"/>
            <Rectangle Height="100" Stroke="DarkViolet" StrokeThickness="10" Fill="{x:Static SystemColors.DesktopBrush}"/>                
        </StackPanel>
    </StackPanel>

效果如下:
WPF整理-使用用户选择主题的颜色和字体第1张

如果我们改变系统地主题:

WPF整理-使用用户选择主题的颜色和字体第2张

再次编译运行,则程序运行如下:

WPF整理-使用用户选择主题的颜色和字体第3张

看不出区别~

但,若我们程序保持运行,改变主题,则程序界面随之改变如下:

WPF整理-使用用户选择主题的颜色和字体第4张

这就是两者的区别。

免责声明:文章转载自《WPF整理-使用用户选择主题的颜色和字体》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Python——cmd调用(os.system阻塞处理)(多条命令执行)邂逅Sass和Compass之Compass篇下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

homebrew &amp;amp; brew cask使用技巧及Mac软件安装

homebrew 安装 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 命令 安装软件:brew install 软件名,例:brew install wget 搜索软件:brew search 软件名...

高通开发笔记

1.CIT暗码:*#*#00#*#*,*#*#564548#*#*  *#889#2.修改TP方向:echo 1 > sys/class/input/input0/device/flipx3.修改手机方向分辨率:    编译:device/qcom/d801/system.prop(ro.sf.lcd_density = 120  (没有就自己添加,...

ret ,retf ,iret ,int 指令

ret指令, 执行过程 ret指令用栈中的数据,修改IP的值,从而实现近转移。 CPU执行ret指令时,进行下面两步操作: (IP)=((SS)*16+(SP)) (SP)=(SP)+2; 另一种用法 ret n (n为整数) 等效于 (IP)=((SS)*16+(SP)) (SP)=(SP)+2; (SP)=(SP)+n; 例如ret 4 pop ip...

SpringBoot 学习记录 2021.05.13 Started

环境搭建 Spring Boot 2.x Java JDK 需要安装 JDK java8 也就是 1.8, 用 jdk-8u271-windows-x64.exe 网上有很多安装java8的教程,很简单。我已经安装完了。注意配置环境变量。 C:UsersReciter>java -version java version "1.8.0_211" Ja...

WinInet:HTTPS 请求出现无效的证书颁发机构的处理

     首先,微软提供的WinInet库封装了对网页访问的方法。      最近工作需要从https服务器获取数据,都知道https和http网页的访问方式不同,多了一道证书认证程序,这样就使得https在请求起来比http要复杂的多;好在,WinInet库中提供了对https网页请求的处理,这样就不需要在使用openssl中的一些方法来复杂化程序了。...

element-ui Progress、Badge、Alert组件源码分析整理笔记(四)

Progress进度条组件 <template> <!--最外层--> <div :class="[ 'el-progress--' + type, status ? 'is-' + status : '', { 'el-progress--w...