Share via


Přehled štětců WPF

Všechno viditelné na obrazovce je viditelné, protože ho namaloval štětec. Například štětec slouží k popisu pozadí tlačítka, popředí textu a výplně obrazce. Toto téma představuje koncepty malování pomocí štětců WPF (Windows Presentation Foundation) a poskytuje příklady. Štětce umožňují malovat objekty uživatelského rozhraní s čímkoli od jednoduchých, plných barev až po složité sady vzorů a obrázků.

Malování štětcem

"Maluje Brush " oblast s jejím výstupem. Různé štětce mají různé typy výstupu. Některé štětce malují oblast plnou barvou, jiné s přechodem, vzorem, obrázkem nebo výkresem. Následující obrázek ukazuje příklady jednotlivých Brush typů.

Brush types
Příklady štětců

Většina vizuálních objektů umožňuje určit, jak se malují. V následující tabulce jsou uvedeny některé běžné objekty a vlastnosti, se kterými můžete použít Brush.

Třída Vlastnosti štětce
Border BorderBrush, Background
Control Background, Foreground
Panel Background
Pen Brush
Shape Fill, Stroke
TextBlock Background

Následující části popisují různé Brush typy a poskytují příklad jednotlivých typů.

Malování plnou barvou

A SolidColorBrush maluje oblast plnou Color. Existují různé způsoby, jak zadat Color : SolidColorBrushmůžete například zadat její alfa, červenou, modrou a zelenou kanály nebo použít některou z předdefinovaných barev poskytovaných Colors třídou.

Následující příklad používá SolidColorBrush k malování Fill .Rectangle Následující obrázek znázorňuje malovaný obdélník.

A rectangle painted using a SolidColorBrush
Obdélník malovaný pomocí SolidColorBrush

Rectangle exampleRectangle = new Rectangle();
exampleRectangle.Width = 75;
exampleRectangle.Height = 75;

// Create a SolidColorBrush and use it to
// paint the rectangle.
SolidColorBrush myBrush = new SolidColorBrush(Colors.Red);
exampleRectangle.Fill = myBrush;
Dim exampleRectangle As New Rectangle()
exampleRectangle.Width = 75
exampleRectangle.Height = 75

' Create a SolidColorBrush and use it to
' paint the rectangle.
Dim myBrush As New SolidColorBrush(Colors.Red)
exampleRectangle.Fill = myBrush
<Rectangle Width="75" Height="75">
  <Rectangle.Fill>
    <SolidColorBrush Color="Red" />
  </Rectangle.Fill>
</Rectangle>

Další informace o SolidColorBrush třídě najdete v tématu Malování s přehledem plných barev a přechodů.

Malování lineárním přechodem

A LinearGradientBrush maluje oblast lineárním přechodem. Lineární přechod kombinuje dvě nebo více barev přes čáru, osu přechodu. Objekty slouží GradientStop k určení barev v přechodu a jejich pozicích.

Následující příklad používá LinearGradientBrush k malování Fill .Rectangle Následující obrázek znázorňuje malovaný obdélník.

A rectangle painted using a LinearGradientBrush
Obdélník malovaný pomocí LinearGradientBrush

Rectangle exampleRectangle = new Rectangle();
exampleRectangle.Width = 75;
exampleRectangle.Height = 75;

// Create a LinearGradientBrush and use it to
// paint the rectangle.
LinearGradientBrush myBrush = new LinearGradientBrush();
myBrush.GradientStops.Add(new GradientStop(Colors.Yellow, 0.0));
myBrush.GradientStops.Add(new GradientStop(Colors.Orange, 0.5));
myBrush.GradientStops.Add(new GradientStop(Colors.Red, 1.0));

exampleRectangle.Fill = myBrush;
Dim exampleRectangle As New Rectangle()
exampleRectangle.Width = 75
exampleRectangle.Height = 75

' Create a LinearGradientBrush and use it to
' paint the rectangle.
Dim myBrush As New LinearGradientBrush()
myBrush.GradientStops.Add(New GradientStop(Colors.Yellow, 0.0))
myBrush.GradientStops.Add(New GradientStop(Colors.Orange, 0.5))
myBrush.GradientStops.Add(New GradientStop(Colors.Red, 1.0))

exampleRectangle.Fill = myBrush
<Rectangle Width="75" Height="75">
  <Rectangle.Fill>
    <LinearGradientBrush>
      <GradientStop Color="Yellow" Offset="0.0" />
      <GradientStop Color="Orange" Offset="0.5" />
      <GradientStop Color="Red" Offset="1.0" />
    </LinearGradientBrush>
  </Rectangle.Fill>
</Rectangle>

Další informace o LinearGradientBrush třídě najdete v tématu Malování s přehledem plných barev a přechodů.

Malování s paprskovým přechodem

A RadialGradientBrush maluje oblast paprskovým přechodem. Paprskový přechod prolíná dvě nebo více barev v kruhu. Stejně jako u LinearGradientBrush třídy použijete GradientStop objekty k určení barev v přechodu a jejich pozicích.

Následující příklad používá RadialGradientBrush k malování Fill .Rectangle Následující obrázek znázorňuje malovaný obdélník.

A rectangle painted using a RadialGradientBrush
Obdélník malovaný pomocí RadialGradientBrush

Rectangle exampleRectangle = new Rectangle();
exampleRectangle.Width = 75;
exampleRectangle.Height = 75;

// Create a RadialGradientBrush and use it to
// paint the rectangle.
RadialGradientBrush myBrush = new RadialGradientBrush();
myBrush.GradientOrigin = new Point(0.75, 0.25);
myBrush.GradientStops.Add(new GradientStop(Colors.Yellow, 0.0));
myBrush.GradientStops.Add(new GradientStop(Colors.Orange, 0.5));
myBrush.GradientStops.Add(new GradientStop(Colors.Red, 1.0));

exampleRectangle.Fill = myBrush;
Dim exampleRectangle As New Rectangle()
exampleRectangle.Width = 75
exampleRectangle.Height = 75

' Create a RadialGradientBrush and use it to
' paint the rectangle.
Dim myBrush As New RadialGradientBrush()
myBrush.GradientOrigin = New Point(0.75, 0.25)
myBrush.GradientStops.Add(New GradientStop(Colors.Yellow, 0.0))
myBrush.GradientStops.Add(New GradientStop(Colors.Orange, 0.5))
myBrush.GradientStops.Add(New GradientStop(Colors.Red, 1.0))

exampleRectangle.Fill = myBrush
<Rectangle Width="75" Height="75">
  <Rectangle.Fill>
    <RadialGradientBrush GradientOrigin="0.75,0.25">
      <GradientStop Color="Yellow" Offset="0.0" />
      <GradientStop Color="Orange" Offset="0.5" />
      <GradientStop Color="Red" Offset="1.0" />
    </RadialGradientBrush>
  </Rectangle.Fill>
</Rectangle>

Další informace o RadialGradientBrush třídě najdete v tématu Malování s přehledem plných barev a přechodů.

Malování s obrázkem

Maluje ImageBrush oblast s ImageSource.

Následující příklad používá ImageBrush k malování Fill .Rectangle Následující obrázek znázorňuje malovaný obdélník.

A Rectangle painted by an ImageBrush
Obdélník malovaný pomocí obrázku

Rectangle exampleRectangle = new Rectangle();
exampleRectangle.Width = 75;
exampleRectangle.Height = 75;

// Create an ImageBrush and use it to
// paint the rectangle.
ImageBrush myBrush = new ImageBrush();
myBrush.ImageSource =
    new BitmapImage(new Uri(@"sampleImages\pinkcherries.jpg", UriKind.Relative));

exampleRectangle.Fill = myBrush;
Dim exampleRectangle As New Rectangle()
exampleRectangle.Width = 75
exampleRectangle.Height = 75

' Create an ImageBrush and use it to
' paint the rectangle.
Dim myBrush As New ImageBrush()
myBrush.ImageSource = New BitmapImage(New Uri("sampleImages\pinkcherries.jpg", UriKind.Relative))

exampleRectangle.Fill = myBrush
<Rectangle Width="75" Height="75">
  <Rectangle.Fill>
    <ImageBrush ImageSource="sampleImages\pinkcherries.jpg"  />
  </Rectangle.Fill>
</Rectangle>

Další informace o ImageBrush třídě najdete v tématu Malování s obrázky, výkresy a vizuály.

Malování výkresem

A DrawingBrush maluje oblast s Drawing. A Drawing může obsahovat obrazce, obrázky, text a média.

Následující příklad používá DrawingBrush k malování Fill .Rectangle Následující obrázek znázorňuje malovaný obdélník.

A rectangle painted using a DrawingBrush
Obdélník malovaný pomocí DrawingBrush

Rectangle exampleRectangle = new Rectangle();
exampleRectangle.Width = 75;
exampleRectangle.Height = 75;

// Create a DrawingBrush and use it to
// paint the rectangle.
DrawingBrush myBrush = new DrawingBrush();

GeometryDrawing backgroundSquare =
    new GeometryDrawing(
        Brushes.White,
        null,
        new RectangleGeometry(new Rect(0, 0, 100, 100)));

GeometryGroup aGeometryGroup = new GeometryGroup();
aGeometryGroup.Children.Add(new RectangleGeometry(new Rect(0, 0, 50, 50)));
aGeometryGroup.Children.Add(new RectangleGeometry(new Rect(50, 50, 50, 50)));

LinearGradientBrush checkerBrush = new LinearGradientBrush();
checkerBrush.GradientStops.Add(new GradientStop(Colors.Black, 0.0));
checkerBrush.GradientStops.Add(new GradientStop(Colors.Gray, 1.0));

GeometryDrawing checkers = new GeometryDrawing(checkerBrush, null, aGeometryGroup);

DrawingGroup checkersDrawingGroup = new DrawingGroup();
checkersDrawingGroup.Children.Add(backgroundSquare);
checkersDrawingGroup.Children.Add(checkers);

myBrush.Drawing = checkersDrawingGroup;
myBrush.Viewport = new Rect(0, 0, 0.25, 0.25);
myBrush.TileMode = TileMode.Tile;

exampleRectangle.Fill = myBrush;
Dim exampleRectangle As New Rectangle()
exampleRectangle.Width = 75
exampleRectangle.Height = 75

' Create a DrawingBrush and use it to
' paint the rectangle.
Dim myBrush As New DrawingBrush()

Dim backgroundSquare As New GeometryDrawing(Brushes.White, Nothing, New RectangleGeometry(New Rect(0, 0, 100, 100)))

Dim aGeometryGroup As New GeometryGroup()
aGeometryGroup.Children.Add(New RectangleGeometry(New Rect(0, 0, 50, 50)))
aGeometryGroup.Children.Add(New RectangleGeometry(New Rect(50, 50, 50, 50)))

Dim checkerBrush As New LinearGradientBrush()
checkerBrush.GradientStops.Add(New GradientStop(Colors.Black, 0.0))
checkerBrush.GradientStops.Add(New GradientStop(Colors.Gray, 1.0))

Dim checkers As New GeometryDrawing(checkerBrush, Nothing, aGeometryGroup)

Dim checkersDrawingGroup As New DrawingGroup()
checkersDrawingGroup.Children.Add(backgroundSquare)
checkersDrawingGroup.Children.Add(checkers)

myBrush.Drawing = checkersDrawingGroup
myBrush.Viewport = New Rect(0, 0, 0.25, 0.25)
myBrush.TileMode = TileMode.Tile

exampleRectangle.Fill = myBrush
<Rectangle Width="75" Height="75">
  <Rectangle.Fill>
    <DrawingBrush Viewport="0,0,0.25,0.25" TileMode="Tile">
      <DrawingBrush.Drawing>
        <DrawingGroup>
          <GeometryDrawing Brush="White">
            <GeometryDrawing.Geometry>
              <RectangleGeometry Rect="0,0,100,100" />
            </GeometryDrawing.Geometry>
          </GeometryDrawing>

          <GeometryDrawing>
            <GeometryDrawing.Geometry>
              <GeometryGroup>
                <RectangleGeometry Rect="0,0,50,50" />
                <RectangleGeometry Rect="50,50,50,50" />
              </GeometryGroup>
            </GeometryDrawing.Geometry>
            <GeometryDrawing.Brush>
              <LinearGradientBrush>
                <GradientStop Offset="0.0" Color="Black" />
                <GradientStop Offset="1.0" Color="Gray" />
              </LinearGradientBrush>
            </GeometryDrawing.Brush>
          </GeometryDrawing>
        </DrawingGroup>
      </DrawingBrush.Drawing>
    </DrawingBrush>
  </Rectangle.Fill>
</Rectangle>

Další informace o DrawingBrush třídě najdete v tématu Malování s obrázky, výkresy a vizuály.

Malování s vizuálem

A VisualBrush maluje oblast objektem Visual . Příklady vizuálních objektů zahrnují Button, Pagea MediaElement. Umožňuje VisualBrush také promítnout obsah z jedné části aplikace do jiné oblasti. Je velmi užitečné při vytváření efektů odrazu a zvětšení částí obrazovky.

Následující příklad používá VisualBrush k malování Fill .Rectangle Následující obrázek znázorňuje malovaný obdélník.

A rectangle painted using a VisualBrush
Obdélník malovaný pomocí vizuálního štětce

Rectangle exampleRectangle = new Rectangle();
exampleRectangle.Width = 75;
exampleRectangle.Height = 75;

// Create a VisualBrush and use it
// to paint the rectangle.
VisualBrush myBrush = new VisualBrush();

//
// Create the brush's contents.
//
StackPanel aPanel = new StackPanel();

// Create a DrawingBrush and use it to
// paint the panel.
DrawingBrush myDrawingBrushBrush = new DrawingBrush();
GeometryGroup aGeometryGroup = new GeometryGroup();
aGeometryGroup.Children.Add(new RectangleGeometry(new Rect(0, 0, 50, 50)));
aGeometryGroup.Children.Add(new RectangleGeometry(new Rect(50, 50, 50, 50)));
RadialGradientBrush checkerBrush = new RadialGradientBrush();
checkerBrush.GradientStops.Add(new GradientStop(Colors.MediumBlue, 0.0));
checkerBrush.GradientStops.Add(new GradientStop(Colors.White, 1.0));
GeometryDrawing checkers = new GeometryDrawing(checkerBrush, null, aGeometryGroup);
myDrawingBrushBrush.Drawing = checkers;
aPanel.Background = myDrawingBrushBrush;

// Create some text.
TextBlock someText = new TextBlock();
someText.Text = "Hello, World";
FontSizeConverter fSizeConverter = new FontSizeConverter();
someText.FontSize = (double)fSizeConverter.ConvertFromString("10pt");
someText.Margin = new Thickness(10);

aPanel.Children.Add(someText);

myBrush.Visual = aPanel;
exampleRectangle.Fill = myBrush;

Dim exampleRectangle As New Rectangle()
exampleRectangle.Width = 75
exampleRectangle.Height = 75

' Create a VisualBrush and use it
' to paint the rectangle.
Dim myBrush As New VisualBrush()

'
' Create the brush's contents.
'
Dim aPanel As New StackPanel()

' Create a DrawingBrush and use it to
' paint the panel.
Dim myDrawingBrushBrush As New DrawingBrush()
Dim aGeometryGroup As New GeometryGroup()
aGeometryGroup.Children.Add(New RectangleGeometry(New Rect(0, 0, 50, 50)))
aGeometryGroup.Children.Add(New RectangleGeometry(New Rect(50, 50, 50, 50)))
Dim checkerBrush As New RadialGradientBrush()
checkerBrush.GradientStops.Add(New GradientStop(Colors.MediumBlue, 0.0))
checkerBrush.GradientStops.Add(New GradientStop(Colors.White, 1.0))
Dim checkers As New GeometryDrawing(checkerBrush, Nothing, aGeometryGroup)
myDrawingBrushBrush.Drawing = checkers
aPanel.Background = myDrawingBrushBrush

' Create some text.
Dim someText As New TextBlock()
someText.Text = "Hello, World"
Dim fSizeConverter As New FontSizeConverter()
someText.FontSize = CDbl(fSizeConverter.ConvertFromString("10pt"))
someText.Margin = New Thickness(10)

aPanel.Children.Add(someText)

myBrush.Visual = aPanel
exampleRectangle.Fill = myBrush

<Rectangle Width="75" Height="75">
  <Rectangle.Fill>
    <VisualBrush TileMode="Tile">
      <VisualBrush.Visual>
        <StackPanel>
          <StackPanel.Background>
            <DrawingBrush>
              <DrawingBrush.Drawing>
                <GeometryDrawing>
                  <GeometryDrawing.Brush>
                    <RadialGradientBrush>
                      <GradientStop Color="MediumBlue" Offset="0.0" />
                      <GradientStop Color="White" Offset="1.0" />
                    </RadialGradientBrush>
                  </GeometryDrawing.Brush>
                  <GeometryDrawing.Geometry>
                    <GeometryGroup>
                      <RectangleGeometry Rect="0,0,50,50" />
                      <RectangleGeometry Rect="50,50,50,50" />
                    </GeometryGroup>
                  </GeometryDrawing.Geometry>
                </GeometryDrawing>
              </DrawingBrush.Drawing>
            </DrawingBrush>
          </StackPanel.Background>
          <TextBlock FontSize="10pt" Margin="10">Hello, World!</TextBlock>
        </StackPanel>
      </VisualBrush.Visual>
    </VisualBrush>
  </Rectangle.Fill>
</Rectangle>

Další informace o VisualBrush třídě najdete v tématu Malování s obrázky, výkresy a vizuály.

Malování pomocí předdefinovaných a systémových štětců

Pro usnadnění poskytuje Windows Presentation Foundation (WPF) sadu předdefinovaných a systémových štětců, které můžete použít k malování objektů.

Běžné funkce štětce

Brush objekty poskytují Opacity vlastnost, kterou lze použít k vytvoření průhledného nebo částečně průhledného štětce. Hodnota Opacity 0 dělá kartáč zcela průhledný, zatímco Opacity hodnota 1 dělá kartáč zcela neprůhledný. Následující příklad používá Opacity vlastnost k vytvoření SolidColorBrush 25% neprůchožné.

<Rectangle Width="100" Height="100">
  <Rectangle.Fill>
    <SolidColorBrush Color="Blue" Opacity="0.25" />
  </Rectangle.Fill>
</Rectangle>
Rectangle myRectangle = new Rectangle();
myRectangle.Width = 100;
myRectangle.Height = 100;
SolidColorBrush partiallyTransparentSolidColorBrush
    = new SolidColorBrush(Colors.Blue);
partiallyTransparentSolidColorBrush.Opacity = 0.25;
myRectangle.Fill = partiallyTransparentSolidColorBrush;

Pokud štětec obsahuje barvy, které jsou částečně průhledné, zkombinuje se hodnota neprůhlednosti barvy barvy pomocí násobení s hodnotou neprůhlednosti štětce. Pokud má například štětec hodnotu neprůhlednosti 0,5 a barva použitá v štětci má také neprůhlednou hodnotu 0,5, má výstupní barva hodnotu neprůhlednosti 0,25.

Poznámka:

Je efektivnější změnit hodnotu neprůhlednosti štětce, než je změnit neprůhlednost celého prvku pomocí jeho UIElement.Opacity vlastnosti.

Obsah štětce můžete otočit, škálovat, zkosit a přeložit pomocí jeho Transform nebo RelativeTransform vlastností. Další informace naleznete v tématu Přehled transformace štětce.

Protože se jedná o Animatable objekty, Brush mohou být objekty animované. Další informace najdete v přehledu animace.

Zamrznutelné funkce

Vzhledem k tomu, že dědí z Freezable třídy, Brush třída poskytuje několik speciálních funkcí: Brush objekty lze deklarovat jako prostředky, sdílet mezi více objekty a klonovat. Kromě toho lze všechny Brush typy kromě VisualBrush toho vytvořit jen pro čtení, aby se zlepšil výkon a byl bezpečný pro přístup z více vláken.

Další informace o různých funkcích poskytovaných Freezable objekty naleznete v tématu Zamrznutelné objekty Přehled.

Další informace o tom, proč VisualBrush objekty nelze zamrazit, naleznete na VisualBrush stránce typu.

Viz také