方法: システム ブラシで領域を塗りつぶす

SystemColors クラスを使用すると、ControlBrushControlBrushKeyDesktopBrush などのシステム ブラシと色を利用できます。 システム ブラシは、指定したシステム カラーで領域を塗りつぶす SolidColorBrush オブジェクトです。 システム ブラシは、常に純色の塗りつぶしを生成します。グラデーションを作成するために使用することはできません。

システム ブラシは、静的なリソースとしても、動的なリソースとしても使用できます。 アプリケーションの実行中にユーザーがシステム ブラシを変更したときにブラシを自動的に更新する場合は、動的なリソースを使用します。それ以外の場合は、静的なリソースを使用します。 SystemColors クラスには、厳密な名前付け規則に従うさまざまな静的プロパティが含まれます。

  • *<SystemColor>*Brush

    指定したシステム カラーの 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>

グラデーションでのシステム カラーの使用方法を示す例については、「グラデーションでシステム カラーを使用する」を参照してください。

関連項目