WPF使用PATH來畫圓


WPF使用Path來畫圓, 在 WPF 中可以使用 Path (路徑) 來畫圓,而 Path 支持兩種寫法:xaml 代碼格式、標記格式,這里介紹的是標記格式:

例子:

<Path Data="M 300,300 A 100,100 0 1 1 300,299 Z" Stroke="Black" StrokeThickness="5" Fill="Transparent"/>

效果:

結合代碼解釋:
1. Stroke 表示圓邊框的顏色,這里是黑色 black

2. StrokeThickness 表示邊框粗細

3. Fill 表示圓內部的顏色,這里為藍色 blue

4. Path 的 Data 屬性,就是描述當前路徑是如何描畫的
Data=”M 300,300 A 100,100 0 1 1 300,299 Z”

           M 300,300  : 300,300 表示起始坐標,字母 M 為 Move, 意思是將畫筆移到 300,300 處;

            A 100,100 0 1 1 300,299 這是圓弧的書寫語法,詳細格式如下:
A 表示 Arc 圓弧,
100,100 表示圓弧的 x, y 值,表示橢圓時表示大小半徑,圓是兩值相同;
0 表示圖形的旋轉角度;
1 當圖形設置了旋轉角度,並且大於 180度時,才會生效,1 表示取大圓弧,0 表示取小圓弧;
1 表示畫圓時筆畫是是順時針畫,需要逆時針時設置為 0;
300,299 表示終止坐標

Z 表示路徑閉合。

官方描述如下:

Elliptical Arc Command
Creates an elliptical arc between the current point and the specified end point.
語法格式如下:

A size rotationAngle isLargeArcFlag sweepDirectionFlag endPoint

或者

a size rotationAngle isLargeArcFlag sweepDirectionFlag endPoint
參數 描述
size System.Windows.Size
The x- and y-radius of the arc.
就是起始坐標,如 (0, 0)
rotationAngle System.Double
The rotation of the ellipse, in degrees.
圖形的旋轉角度
isLargeArcFlag Set to 1 if the angle of the arc should be 180
degrees or greater; otherwise, set to 0.
當畫正圓和橢圓時,不生效;只有在畫圓弧時,
並且旋轉角度大於 180 是才生效,
判斷是取小圓弧合適大圓弧
sweepDirectionFlag Set to 1 if the arc is drawn in a
positive-angle direction; otherwise, set to 0.
1 為順時針畫圖,0 為逆時針畫圖
endPoint System.Windows.Point
The point to which the arc is drawn.
終止坐標

注意:標記命令不區分大小寫

正圓是橢圓的子集,上述的語法其實是針對所有橢圓都有效的,編寫 Data 的思路大致可以參考以下說明:
1.確定起始坐標、終止坐標;
這決定了路徑的起始位置和結束位置,特別注意:
如果需要畫正圓,大小半徑必須一樣,而因為起始坐標和終止坐標不能相同,所以一般終止坐標減一,例如:
起始坐標 (100, 100)
終止坐標可以為 (100, 99) 或者 (99, 100)

2.半徑和坐標之間的關系:
當只畫部分圓弧時,兩個坐標之間的距離會影響半徑的大小:
當兩個坐標之間的距離大於2倍半徑之和時,半徑(橢圓的小半徑)為兩坐標距離/2;
當兩坐標之間的距離小於2倍半徑之和時,半徑 (橢圓的小半徑) 為設定值;

例子如下:
3 個圖形,圓心坐標為(300,300),順時針畫圓

<Path Data="M 300,300 A 100,100 0 1 1 300,0 Z" Stroke="Black" StrokeThickness="5" Fill="Transparent"/> <Path Data="M 300,300 A 100,100 0 1 1 300,100 Z" Stroke="Red" StrokeThickness="5" Fill="Transparent"/> <Path Data="M 300,300 A 100,100 0 1 1 300,200 Z" Stroke="Violet" StrokeThickness="5" Fill="Transparent"/>

結果如下:

黑色圖形的兩個坐標的距離大於2倍半徑,所以最終的半徑變大;
紅色圖形和紫粉色圖形的兩個坐標的距離小於2倍半徑,所以半徑不變;

總結:

1. 畫圓弧(圓形)時,先確定兩個坐標(起始坐標、終止坐標);
2.確定順時針還是逆時針方向畫圖;
3.如果是橢圓還可以根據需求設置旋轉角度;
4.如果設置了旋轉角度大於 180 度,可以通過設置獲取大圓弧還是小圓弧;

橢圓的例子:

<!--黑色橢圓,是原始角度--> <Path Data="M 300,300 A 100,50 0 1 1 300,299 Z" Stroke="Black" StrokeThickness="5" Fill="Transparent"/> <!--紅色橢圓,旋轉了90度--> <Path Data="M 300,300 A 100,50 90 1 1 300,299 Z" Stroke="Red" StrokeThickness="5" Fill="Transparent"/> <!--紫藍色橢圓,逆時針畫的--> <Path Data="M 300,300 A 100,50 0 1 0 300,299 Z" Stroke="BlueViolet" StrokeThickness="5" Fill="Transparent"/>

結果如下:

設置了大於180度的旋轉角度,並根據需要獲取大小圓弧

 <!--紅色的圓,旋轉200度,順時針,獲取大圓弧--> <Path Data="M 50,200 A 100,100 200 1 0 200,100" Stroke="Red" StrokeThickness="5" Fill="Transparent"/> <!--咖啡色的圓,旋轉200度,逆時針,獲取大圓弧--> <Path Data="M 50,200 A 100,100 200 1 1 200,100" Stroke="Chocolate" StrokeDashArray="4 1" StrokeThickness="5" Fill="Transparent"/> <!--綠色的圓,旋轉200度,順時針,獲取小圓弧--> <Path Data="M 50,200 A 100,100 200 0 1 200,100" Stroke="LawnGreen" StrokeDashArray="2 2" StrokeThickness="5" Fill="Transparent"/> <!--藍色的圓,旋轉200度,逆時針,獲取小圓弧--> <Path Data="M 50,200 A 100,100 200 0 0 200,100" Stroke="Blue" StrokeDashArray="3 3" StrokeThickness="5" Fill="Transparent"/>

效果如下圖:

設置了大於180度的旋轉角度,橢圓弧獲取大小圓弧:

注意:strokedasharray = “4 1” 是用來設置虛線顯示效果的,4 表示虛線中每段實體的長度,1表示虛線中每段空白的長度

<!--黑色橢圓弧,旋轉200度,逆時針,取大圓弧--> <Path Data="M 400,300 A 200,100 200 1 0 200,150" Stroke="Black" StrokeThickness="5" Fill="Transparent"/> <!--粉紅色橢圓弧,旋轉200度,順時針,取大圓弧--> <Path Data="M 400,300 A 200,100 200 1 1 200,150" Stroke="PaleVioletRed" StrokeDashArray="4 1" StrokeThickness="5" Fill="Transparent"/> <!--藍色橢圓弧,旋轉200度,逆時針,取小圓弧--> <Path Data="M 400,300 A 200,100 200 0 0 200,150" Stroke="Blue" StrokeThickness="5" StrokeDashArray="4 2" Fill="Transparent"/> <!--綠色橢圓弧,旋轉200度,順時針,取小圓弧--> <Path Data="M 400,300 A 200,100 200 0 1 200,150" Stroke="Green" StrokeThickness="5" StrokeDashArray="4 2" Fill="Transparent"/>

結果如下圖:

 

 

-------------------------------------------------------------------------------------------------------------------------------------

<Grid>
  <Path x:Name="p1" Stroke="Black" StrokeThickness="5" Fill="Transparent"/>
  <Path Data="M 300,300 A 100,100 0 1 1 300,299 Z" Stroke="Black" StrokeThickness="5" Fill="Transparent"/>
</Grid>

 

C# code..

string str = "M 600,600 A 100,100 0 1 1 600,599 Z";
p1.Data = Geometry.Parse(str);

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM