Win10系列:C#應用控件進階5

多線形

多線形和多邊形相似,不一樣點在於多線形中最後一個點和第一個點不會默認被鏈接。在多線形的點集中,能夠存在同一個開始點和終結點於是會定義成閉合圖形。下面將演示如何使用Polyline控件繪製一個多線形this

在一個打開的Windows應用商店項目中新建一個空白頁,並命名爲PolylinePage,雙擊打開此頁面的PolylinePage.xaml文件,在Grid元素中添加以下繪製多線形的代碼。 spa

<Polyline Stroke="Red" StrokeThickness="5" Points="70,100 10, 150 210,150 100,100"/> 對象

上面代碼繪製的多線形由Points集合的四個點(70,100)、(10,150)、(210,150)和(100,100)來控制形狀。接着設置Stroke和StrokeThickness屬性爲紅色、5像素,從而定義一個輪廓顏色爲紅色、輪廓粗細爲5像素的多邊形。此多邊形第一個點和最後一個點不會像Polygon元素那樣由Stroke輪廓鏈接起來。運行效果如圖8-6所示。 blog

前面介紹了使用前臺代碼繪製多邊形,接下來看一下使用後臺代碼繪製此多邊形的過程,具體實現代碼以下所示: it

public PolylinePage() io

{ 容器

this.InitializeComponent(); 後臺

//開始繪製多線形 方法

Polyline polyline = new Polyline(); im

polyline.Stroke = new SolidColorBrush(Colors.Red);

//實例化points對象

PointCollection points = new PointCollection();

//把點添加到點集合中

points.Add(new Point(70, 100));

points.Add(new Point(10, 150));

points.Add(new Point(210, 150));

points.Add(new Point(100, 100));

polyline.Points = points;

    //將多邊形放到名字爲MyCanvas的控件中

MyCanvas.Children.Add(polyline);

}

在上面的代碼中,首先實例化一個Polyline類型的對象polyline,設定Stroke屬性爲紅色,接着定義存放點集的points對象,利用points對象的Add屬性添加四個點,把points賦值給polyline對象的Points屬性,最後調用MyCanvas容器對象的Children屬性中的Add方法,將這個多線形加入到頁面中顯示。

運行此頁面,多線形運行效果如圖8-6所示。

圖8-6 多線形形狀效果圖

相關文章
相關標籤/搜索