當前位置: 妍妍網 > 碼農

WPF中繪制一面五星紅旗

2024-02-15碼農

要在WPF中繪制一面五星紅旗,您可以使用`Path`元素以及一系列`Polygon`和`PathGeometry`來建立所需的形狀。 以下是一個範例程式碼:

```xaml<Windowx: class="DrawingChineseFlag.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="Chinese Flag Drawing"Height="450"Width="600"><Grid><Canvas><!-- 繪制紅色背景 --><RectangleWidth="300"Height="200"Fill="Red" /><!-- 繪制大星星 --><PathFill="Yellow"><Path.Data><PathGeometry><PathFigureStartPoint="150,70"><PolygonPoints="150,70 145,95 125,95 142,110 130,130 150,115 170,130 158,110 175,95 155,95" /></PathFigure></PathGeometry></Path.Data></Path><!-- 繪制四個小星星 --><PathFill="Yellow"><Path.Data><PathGeometry><PathFigureStartPoint="170,90"><PolygonPoints="170,90 165,100 175,100" /></PathFigure></PathGeometry></Path.Data></Path><!-- 將以下程式碼復制4次,以繪制四個小星星 --><!-- 第一個小星星 --><PathFill="Yellow"><Path.Data><PathGeometry><PathFigureStartPoint="160,120"><PolygonPoints="160,120 155,130 165,130" /></PathFigure></PathGeometry></Path.Data></Path><!-- 第二個小星星 --><PathFill="Yellow"><Path.Data><PathGeometry><PathFigureStartPoint="180,120"><PolygonPoints="180,120 175,130 185,130" /></PathFigure></PathGeometry></Path.Data></Path><!-- 第三個小星星 --><PathFill="Yellow"><Path.Data><PathGeometry><PathFigureStartPoint="160,140"><PolygonPoints="160,140 155,150 165,150" /></PathFigure></PathGeometry></Path.Data></Path><!-- 第四個小星星 --><PathFill="Yellow"><Path.Data><PathGeometry><PathFigureStartPoint="180,140"><PolygonPoints="180,140 175,150 185,150" /></PathFigure></PathGeometry></Path.Data></Path></Canvas></Grid></Window>```

在這個範例中,我們使用了WPF的`Rectangle`元素來繪制紅色背景,以及`Path`元素和`Polygon`來繪制五角星形狀。大星星使用了一個`Polygon`來定義其形狀,而四個小星星則分別使用了四個單獨的`Polygon`來定義其形狀。

您可以根據自己的需要調整紅色背景的尺寸、五角星的位置和大小,以及星星之間的間距等等。

希望這個範例能幫助您繪制一面五星紅旗!