畫刷是WPF中最常用的對象之一,主要在System.Windows.Media命名空間內,Brush的繼承樹為
object---dispatchobject----dependencyObject---Freezable---Animatable---Brush---GradientBrush等
其中SolidColorBrush對象是最簡單的畫刷,是單一顏色的,而GradientBrush是漸層畫刷,包括兩個子類LinearGradientBrush
與RadialGradientBrush. 也有Brushes對象可以用來直接使用。 例如:
PropertyInfo[] pro=typeof(Brushes).GetProperties();
Background=(Brush)pro[index].GetValue(null,null);
註:須在命名空間中加入System.Reflection。
漸層畫刷大致包括指定幾個Point對象,並指定該點的顏色。。例 :LinearGradientBrush linear=new LinearGradientBrush(Colors.Red,Colors.Blue,new Point(0.5,0.5),new Point(0.8,0.8)); 紅色到藍色的漸層。
GradientBrush有以下幾個屬性值得注意:
StartPoint與EndPoint .
MappingMode決定是否使用相對座標 (即(1,1)代表右下角),它是BrushMappingMode的枚舉;
SpreadMethod 屬性決定超出部分的顏色。 是GradientSpreadMethod的枚舉,Pad,Reflect,Repeat 。
GradientStops是GradientStopCollection類型,而GradientStop具有Color與Offset屬性 ,
RadialGradientBrush 橢圓形漸層,其中Center RadiusX RadiusY 決定橢圓,GradientOrigin是漸層開始的點,
此外還有TileBrush---DrawingBrush. ImageBrush . VisualBrush
ImageBrush : 顧名思義就是映像畫刷。主要屬性是ImageSource
等
首先繼承樹如下:
Object---DispatcherObject---DependencyObject---Visual---UIElement---FrameworkElement---Control--ContentControl
表單表現的重點就是內容Content;Content幾乎可以是任何對象。
WPF中沒有專用的Font類,與字型相關的主要是FontFamily , FontStyle ,FontWeight FontSize
常用的字型有Courier New, Times New Roman , Arial Palatino Linotype ,Verdana, Comic Sans MS, Century Gothic
例:FontFamily=new FontFamily("Times New Roman Bold Italic");
shape---ellipse,polyline,line,path,rectangle
TextBlock對象;
TextBlock txb=new TextBlock();
txb.Inlines.Add("hello");
txb.Inlines.Add(new Italic(new Run("hello")));
txb.Inlines.Add(new Bold(new Run("hello")));