如何使用系统画笔绘制区域

SystemColors 类提供对系统画笔和颜色(如 ControlBrushControlBrushKeyDesktopBrush)的访问。 系统画笔是一个 SolidColorBrush 对象,用于绘制具有指定系统颜色的区域。 系统画笔始终产生实心填充;它不能用于创建渐变。

可以将系统画笔用作静态资源或动态资源。 如果您希望在应用程序运行时,当用户更改系统画笔时画笔能够自动更新,请使用动态资源,否则,请使用静态资源。 SystemColors 类包含遵循严格命名约定的各种静态属性:

  • *<系统颜色>*画笔

    获取对指定系统颜色 SolidColorBrush 的静态引用。

  • *<SystemColor>*BrushKey

    获取对指定系统颜色 SolidColorBrush 的动态引用。

  • *<SystemColor>*Color

    获取对指定系统颜色的 Color 结构的静态引用。

  • *<SystemColor>*ColorKey

    获取对指定系统颜色 Color 结构的动态引用。

系统颜色是一种 Color 结构体,可用于配置画笔。 例如,可以通过使用系统颜色设置 LinearGradientBrush 对象的渐变停止点 Color 属性来创建渐变。 有关示例,请参阅 在渐变中使用系统颜色。

以下示例使用动态系统画笔引用来设置按钮的背景。

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  WindowTitle="SystemColors Example" Background="White">  
  <StackPanel Margin="20">
 
    <!-- Uses a dynamic resource to set the 
         background of a button. 
         If the desktop brush changes while this application
         is running, this button will be updated. -->
    <Button 
      Background="{DynamicResource {x:Static SystemColors.DesktopBrushKey}}" 
      Content="Hello, World!" />

  </StackPanel>
</Page>

下一个示例将使用静态系统画笔引用来设置按钮的背景。

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  WindowTitle="SystemColors Example" Background="White">  
  <StackPanel Margin="20">
 
    <!-- Uses a static brush to set the
         background of a button. 
         If the desktop brush changes while this application
         is running, this button will not be updated until
         the page is loaded again. -->
    <Button 
      Background="{x:Static SystemColors.DesktopBrush}" 
      Content="Hello, World!"  /> 

  </StackPanel>
</Page>

有关如何在渐变中使用系统颜色的示例,请参阅 在渐变中使用系统颜色。

另请参阅