前一篇文章中简要讲解了圆角按钮、圆形按钮的使用,以及在windows.resource和app.resource中设置圆角或圆形按钮的样式。
这篇主要讲解Polygon(多边形)、Ellipse(椭圆)、Path(路径)这三个内容。
Polygon
我们先看一下的源码:
namespace System.Windows.Shapes
{
public sealed class Polygon : Shape
{
public static readonly DependencyProperty PointsProperty = DependencyProperty.Register("Points", typeof(PointCollection), typeof(Polygon), new FrameworkPropertyMetadata((object)new FreezableDefaultValueFactory((Freezable)PointCollection.get_Empty()), FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender));
public static readonly DependencyProperty FillRuleProperty = DependencyProperty.Register("FillRule", typeof(FillRule), typeof(Polygon), (PropertyMetadata)new FrameworkPropertyMetadata(FillRule.EvenOdd, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender), (ValidateValueCallback)ValidateEnums.IsFillRuleValid);
private Geometry _polygonGeometry;
public PointCollection Points
{
get
{
return (PointCollection)GetValue(PointsProperty);
}
set
{
SetValue(PointsProperty, value);
}
}
public FillRule FillRule
{
get
{
return (FillRule)GetValue(FillRuleProperty);
}
set
{
SetValue(FillRuleProperty, value);
}
}
protected override Geometry DefiningGeometry => _polygonGeometry;
internal override void CacheDefiningGeometry()
{
PointCollection points = Points;
PathFigure pathFigure = new PathFigure();
if (points == null)
{
_polygonGeometry = Geometry.Empty;
return;
}
if (points.Count > 0)
{
pathFigure.StartPoint = points[0];
if (points.Count > 1)
{
Point[] array = new Point[points.Count - 1];
for (int i = 1; i < points.Count; i++)
{
array[i - 1] = points[i];
}
pathFigure.Segments.Add(new PolyLineSegment(array, isStroked: true));
}
pathFigure.IsClosed = true;
}
PathGeometry pathGeometry = new PathGeometry();
pathGeometry.Figures.Add(pathFigure);
pathGeometry.FillRule = FillRule;
_polygonGeometry = pathGeometry;
}
}
}
从源码的信息可以看到Polygon是继承自Shape的类,可用的属性只有PointsProperty、FillRuleProperty两个属性;PointsProperty是PointCollection的Point集合,而Point实质就是一个二维坐标集合,因此在Polygon的使用中Points的中的数据必须是2N个。用法如下:
<Polygon Points="100,400 200,370 180,470" Fill="#4EB1B6" /><!--多边形-->
效果图:
Ellipse
源码如下:
namespace System.Windows.Shapes
{
public sealed class Ellipse : Shape
{
private Rect _rect = Rect.Empty;
public override Geometry RenderedGeometry => DefiningGeometry;
public override Transform GeometryTransform => Transform.Identity;
protected override Geometry DefiningGeometry
{
get
{
if (_rect.IsEmpty)
{
return Geometry.Empty;
}
return new EllipseGeometry(_rect);
}
}
internal override int EffectiveValuesInitialSize => 13;
static Ellipse()
{
Shape.StretchProperty.OverrideMetadata(typeof(Ellipse), new FrameworkPropertyMetadata(Stretch.Fill));
}
protected override Size MeasureOverride(Size constraint)
{
if (base.Stretch == Stretch.UniformToFill)
{
double width = constraint.Width;
double height = constraint.Height;
if (double.IsInfinity(width) && double.IsInfinity(height))
{
return GetNaturalSize();
}
width = ((!double.IsInfinity(width) && !double.IsInfinity(height)) ? Math.Max(width, height) : Math.Min(width, height));
return new Size(width, width);
}
return GetNaturalSize();
}
protected override Size ArrangeOverride(Size finalSize)
{
double strokeThickness = GetStrokeThickness();
double num = strokeThickness / 2.0;
_rect = new Rect(num, num, Math.Max(0.0, finalSize.Width - strokeThickness), Math.Max(0.0, finalSize.Height - strokeThickness));
switch (base.Stretch)
{
case Stretch.None:
{
double num4 = (_rect.Width = (_rect.Height = 0.0));
break;
}
case Stretch.Uniform:
if (_rect.Width > _rect.Height)