Color
可以new出来,也可以通过它的静态方法Color.FromRgb(r,g,b)或Color.FromArgb(a,r,g,b)获得
Color是一个struct.
System.Window.Media命名空间下还有一些Colors的类,包含141个static的只读的属性 (whose names begin alphabetically with AliceBlue and AntiqueWhite and conclude with Yellow and YellowGreen).
Brush的继承关系
其中本章介绍SolidColorBrush和GradientBrush (abstract) (以及其子类LinearGradientBrush RadialGradientBrush). 以下为类继承关系图:
Object
DispatcherObject (abstract)
DependencyObject
Freezable (abstract)
Animatable (abstract)
Brush (abstract)
GradientBrush (abstract)
LinearGradientBrush
RadialGradientBrush
SolidColorBrush
TileBrush (abstract)
DrawingBrush
ImageBrush
VisualBrush
Brush广泛地应用于Control的Background等涉及到颜色的属性
在WPF中,颜色Color是一个单纯的结构,而Brush才是色彩的主演。
如何构造一个SolidColorBrush呢?
- SolidColorBrush的构造函数接收一个参数Color,而Color可通过本章最初介绍的方法获得。
- SolidColorBrush有一个属性 Color,可通过赋值改变SolidColorBrush的颜色。
- Brushes有和Colors对应的141种预定义的单色,但不能直接赋值:
//get an Invalid Operation Exception that states
//"Cannot set a property on object '#FF000000' because it is in a read-only state."
SolidColorBrush brush = Brushes.Black;
//可以这样:
SolidColorBrush brush = Brushes.Black.Clone();
注:以上第三点,究其原因,是因为Brushes的静态变量Black已经frozen了。
The SolidColorBrush objects returned from the Brushes class are in a frozen state,
which means they can no longer be altered.
Like the Changed event, freezing is implemented in the Freezable class, from which Brush inherits.
If the CanFreeze property of a Freezable object is true,
it's possible to call the Freeze method to render the object frozen and unchangeable.
The IsFrozen property indicates this state by becoming true.
Freezing objects can improve performance because they no longer need to be monitored for changes.
A frozen Freezable object can also be shared across threads, while an unfrozen Freezable object cannot.
Although you cannot unfreeze a frozen object, you can make an unfrozen copy of it.
系统画刷以及其优点
SystemColors与Brushes和Colors相似,例如,它可以这样获取颜色及画刷:
SystemColors.WindowColorBrush和SystemColors.WindowColor 也可以这样修改画刷:
Brush brush = new SystemColorBrush(SystemColors.WindowColor);
GradientBrush
包括LinearGradientBrush和RadialGradientBrush
LinearGradientBrush 线性梯度渐变画刷
介绍了2种构造函数
- new LinearGradientBrush(Colors.Red, Colors.Blue, new Point(0, 0), new Point(1, 1));
- new LinearGradientBrush(clr1, clr2, angle);
SpreadMethod 、GradientStops、StartPoint、EndPoint等属性
- 通过设置这些属性,可以改变GradientBrush的样子。
RadialGradientBrush 径向梯度渐变画刷
与LinearGradientBrush相仿,只是色彩渐变的方向是径向
作者给出了几个很有趣的例子,形象地说明了这些Brush的特点。
本章最后介绍了Control类的属性BorderBrush、BorderThickness、Foreground
并引出了下一章:The Concept of Content