WPF通过代码DrawingContext绘制不同的图形(圆形,矩形,菱形,十字交错形)

这边通过接口来实现,使用的时候继承这个接口即可使用

csharp 复制代码
 public interface IShapeDraw
 {
     void DrawPolygon(DrawingContext dc, Brush brush, Pen? p, List<Point> points)
     {
         if (!points.Any())
             return;

         // 创建一个路径,并添加三角形的点
         var geometry = new StreamGeometry();
         using (var geometryContext = geometry.Open())
         {
             geometryContext.BeginFigure(points[0], true, true);
             for (var i = 1; i < points.Count; i++)
             {
                 geometryContext.LineTo(points[i], true, false);
             }
         }
         // 绘制三角形并填充颜色
         dc.DrawGeometry(brush, p, geometry);
     }

     /// <summary>
     /// 三角形
     /// </summary>
     void DrawTriangle(DrawingContext dc, Brush brush, Pen? p, Point point1, Point point2, Point point3)
     {
         DrawPolygon(dc, brush, p, new List<Point>() { point1, point2, point3 });
     }

     /// <summary>
     /// 棱形
     /// </summary>
     void DrawDiamond(DrawingContext dc, Brush brush, Pen? p, Point point1, Point point2, Point point3, Point point4)
     {
         DrawPolygon(dc, brush, p, new List<Point>() { point1, point2, point3, point4 });
     }

     /// <summary>
     /// 十字型图
     /// </summary>
     void DrawCross(DrawingContext dc, Brush brush, Pen? p, List<Point> horizontal2Point, List<Point> vertical2Point)
     {
         if (horizontal2Point.Count != 2)
             return;
         if (vertical2Point.Count != 2)
             return;

         // 定义十字型的线段
         var horizontalStart = horizontal2Point[0];
         var horizontalEnd = horizontal2Point[1];

         var verticalStart = vertical2Point[0];
         var verticalEnd = vertical2Point[1];

         // 创建路径,并添加线段
         var geometry = new StreamGeometry();
         using (var geometryContext = geometry.Open())
         {
             geometryContext.BeginFigure(horizontalStart, false, false);
             geometryContext.LineTo(horizontalEnd, true, false);

             geometryContext.BeginFigure(verticalStart, false, false);
             geometryContext.LineTo(verticalEnd, true, false);
         }

         dc.DrawGeometry(brush, p, geometry);
     }

     /// <summary>
     /// 绘制圆弧
     /// </summary>
     /// <param name="dc"></param>
     /// <param name="center">中心点</param>
     /// <param name="startAngle">其实角度</param>
     /// <param name="endAngle">截止角度</param>
     /// <param name="radius">半径</param>

     void DrawArc(DrawingContext dc, Pen pen, Point center, double startAngle, double endAngle, double radius)
     {
         // 使用ArcSegment绘制圆弧
         var arcGeometry = new PathGeometry();
         var arcFigure = new PathFigure
         {
             StartPoint = new Point(center.X + radius * Math.Cos(startAngle * Math.PI / 180), center.Y + radius * Math.Sin(startAngle * Math.PI / 180)),
             IsClosed = false,
             IsFilled = true
         };
         arcFigure.Segments.Add(new ArcSegment(new Point(center.X + radius * Math.Cos(endAngle * Math.PI / 180), center.Y + radius * Math.Sin(endAngle * Math.PI / 180)), new Size(radius, radius), 0, Math.Abs(endAngle - startAngle) > 180, SweepDirection.Clockwise, true));
         arcGeometry.Figures.Add(arcFigure);

         // 绘制圆弧
         dc.DrawGeometry(Brushes.Transparent, pen, arcGeometry);
     }
相关推荐
ou.cs17 小时前
wpf 基于Behavior库 的行为模块
wpf
滴_咕噜咕噜1 天前
学习笔记(prism--视频【WPF-prism核心教程】)--待更新
笔记·学习·wpf
别给迷住了2 天前
WPF 绘制过顶点的圆滑曲线(样条,贝塞尔)
wpf·贝塞尔曲线·样条曲线·圆滑曲线·过顶点·beziersegment
白露与泡影2 天前
Redisson分布式锁的源码解读
分布式·wpf
勇者神龟2 天前
.net framework wpf 打包免安装exe文件
.net·wpf
吉量*3 天前
WPF系列四:图形控件Rectangle
wpf
假男孩儿3 天前
WPF 最小化到系统托盘
wpf
勇敢小菜鸟4 天前
WPF自定义窗口 输入验证不生效
wpf
鲤籽鲲4 天前
WPF TextBox 输入限制 详解
wpf
鸿喵小仙女4 天前
C# WPF读写STM32/GD32单片机Flash数据
stm32·单片机·c#·wpf