WPF Path LineGeometry,RectangleGeometry,EllipseGeometry

FredGrit發表於2024-06-15
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp167
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.WindowState = WindowState.Maximized;
            this.Loaded += MainWindow_Loaded; 
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Path rectPath = GenRectanglePath(this.ActualWidth, this.ActualHeight);
            this.gd.Children.Add(rectPath);
            Path gPath = GenLinePath(this.ActualWidth, this.ActualHeight);
            this.gd.Children.Add(gPath);
            Path elpPath = GenEllipsePath(this.ActualWidth, this.ActualHeight);
            this.gd.Children.Add(elpPath);
            Path circlePath=GenCirclePath(this.ActualWidth,this.ActualHeight);
            this.gd.Children.Add(circlePath);
        }

        private Path GenCirclePath(double actualWidth, double actualHeight)
        {
            EllipseGeometry circleGeo = new EllipseGeometry(new Point(actualWidth / 2, actualHeight / 2),
                actualHeight / 2-50, actualHeight / 2-50);
            Path circlePath = new Path();
            circlePath.Stroke = new SolidColorBrush(Colors.Blue);
            circlePath.StrokeThickness = 20;
            circlePath.Data = circleGeo;
            return circlePath;
        }

        private Path GenEllipsePath(double actualWidth, double actualHeight)
        {
            Path elpPath = new Path();
            elpPath.Stroke = new SolidColorBrush(Colors.Yellow);
            elpPath.StrokeThickness = 20;
            EllipseGeometry elpGeo= GenEllipseGeo(actualWidth, actualHeight);
            elpPath.Data= elpGeo;
            return elpPath;
        }

        private EllipseGeometry GenEllipseGeo(double actualWidth, double actualHeight)
        {
            EllipseGeometry elpGeo = new EllipseGeometry(new Point(actualWidth/2,actualHeight/2),
                actualWidth/2-50,actualHeight/2-50);
            return elpGeo;
        }

        private static Path GenLinePath(double actualWidth, double actualHeight)
        {
            PathGeometry pathGeo = GenLinePathGeometry(actualWidth,actualHeight);
            Path gPath = new Path();
            gPath.Fill = Brushes.Black;
            gPath.Stroke = Brushes.Red;
            gPath.StrokeThickness = 20;
            gPath.Data = pathGeo;
            return gPath;
        }

        private static Path GenRectanglePath(double actualWidth, double actualHeight)
        {
            RectangleGeometry rectGeo = GenRectGeometry(actualWidth, actualWidth);
            Path rectPath = new Path();
            rectPath.Fill = Brushes.Cyan;
            rectPath.Stroke = Brushes.Black;
            rectPath.StrokeThickness = 10;
            rectPath.Data = rectGeo;
            return rectPath;
        }

        private static RectangleGeometry GenRectGeometry(double actualWidth, double actualHeight)
        {
            RectangleGeometry myRectangleGeometry = new RectangleGeometry();
            myRectangleGeometry.Rect = new Rect(0, 0, actualWidth, actualHeight);
            return myRectangleGeometry;
        }

        private static PathGeometry GenLinePathGeometry(double actualWidth, double actualHeight)
        {
            PathGeometry pathGeo = new PathGeometry();
            PathFigure pf1 = new PathFigure();
            pf1.StartPoint = new Point(0, 0);
            pf1.Segments.Add(new LineSegment(new Point(actualWidth, actualHeight), true));
            pathGeo.Figures.Add(pf1);
            return pathGeo;
        }
    }
}

相關文章