箭头

1、ZC:我现在(20190717)查到的 有2中方式画 箭头:(1)在 System.Windows.Controls.Canvas中绘制;(2)在 Graphics上绘制

2、在 System.Windows.Controls.Canvas中绘制

 2.1、使用WPF创建画图箭头 - NET未来之路 - 博客园.html(https://www.cnblogs.com/lonelyxmas/p/9844910.html

    ZC:这个文章中未提供完整 代码,相应的代码在 下面的文章中找到了,扣出来了

   WPF画箭头 - 百度文库.html(https://wenku.baidu.com/view/cc13b2edaeaad1f346933fc6.html

 2.2、类 代码:

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Media;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Shapes;

namespace xxxxx.ShapeSHH
{

    public sealed class Arrow : Shape
    {
        #region Dependency Properties

        public static readonly DependencyProperty X1Property = DependencyProperty.Register("X1", typeof(double), typeof(Arrow), new
        FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));

        public static readonly DependencyProperty Y1Property = DependencyProperty.Register("Y1", typeof(double), typeof(Arrow), new
        FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender |
        FrameworkPropertyMetadataOptions.AffectsMeasure));

        public static readonly DependencyProperty X2Property = DependencyProperty.Register("X2", typeof(double), typeof(Arrow), new
        FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender |
        FrameworkPropertyMetadataOptions.AffectsMeasure));

        public static readonly DependencyProperty Y2Property = DependencyProperty.Register("Y2", typeof(double), typeof(Arrow), new
        FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender |
        FrameworkPropertyMetadataOptions.AffectsMeasure));

        public static readonly DependencyProperty HeadWidthProperty = DependencyProperty.Register("HeadWidth", typeof(double), typeof(Arrow), new
        FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender |
        FrameworkPropertyMetadataOptions.AffectsMeasure));

        public static readonly DependencyProperty HeadHeightProperty = DependencyProperty.Register("HeadHeight", typeof(double), typeof(Arrow), new
        FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender |
        FrameworkPropertyMetadataOptions.AffectsMeasure));

        #endregion

        #region CLR Properties

        [TypeConverter(typeof(LengthConverter))]
        public double X1
        {
            get { return (double)base.GetValue(X1Property); }
            set { base.SetValue(X1Property, value); }
        }
        [TypeConverter(typeof(LengthConverter))]
        public double Y1
        {
            get { return (double)base.GetValue(Y1Property); }
            set { base.SetValue(Y1Property, value); }
        }
        [TypeConverter(typeof(LengthConverter))]
        public double X2
        {
            get { return (double)base.GetValue(X2Property); }
            set { base.SetValue(X2Property, value); }
        }
        [TypeConverter(typeof(LengthConverter))]
        public double Y2
        {
            get { return (double)base.GetValue(Y2Property); }
            set { base.SetValue(Y2Property, value); }
        }
        [TypeConverter(typeof(LengthConverter))]
        public double HeadWidth
        {
            get { return (double)base.GetValue(HeadWidthProperty); }
            set { base.SetValue(HeadWidthProperty, value); }
        }
        [TypeConverter(typeof(LengthConverter))]
        public double HeadHeight
        {
            get { return (double)base.GetValue(HeadHeightProperty); }
            set { base.SetValue(HeadHeightProperty, value); }
        }

        #endregion

        #region Overrides

        protected override Geometry DefiningGeometry
        {
            get
            {
                // Create a StreamGeometry for describing the shape
                StreamGeometry geometry = new StreamGeometry();
                geometry.FillRule = FillRule.EvenOdd;
                using (StreamGeometryContext context = geometry.Open())
                {
                    InternalDrawArrowGeometry(context);
                }
                // Freeze the geometry for performance benefits
                geometry.Freeze();

                return geometry;
            }
        }

        #endregion

        #region Privates

        private void InternalDrawArrowGeometry(StreamGeometryContext context)
        {
            double theta = Math.Atan2(Y1 - Y2, X1 - X2);
            double sint = Math.Sin(theta);
            double cost = Math.Cos(theta);
            Point pt1 = new Point(X1, this.Y1);
            Point pt2 = new Point(X2, this.Y2);
            Point pt3 = new Point(
            X2 + (HeadWidth * cost - HeadHeight * sint), Y2 + (HeadWidth * sint + HeadHeight * cost));
            Point pt4 = new Point(
            X2 + (HeadWidth * cost + HeadHeight * sint), Y2 - (HeadHeight * cost - HeadWidth * sint));
            context.BeginFigure(pt1, true, false);
            context.LineTo(pt2, true, true);
            context.LineTo(pt3, true, true);
            context.LineTo(pt2, true, true);
            context.LineTo(pt4, true, true);
        }
        #endregion
    }

}

 2.3、调用的代码:

  (1)

        private void DrawLeaderLineArrow(Point startPt, Point endPt)
        {
            Arrow arrow = new Arrow();
            arrow.X1 = startPt.X;
            arrow.Y1 = startPt.Y;
            arrow.X2 = endPt.X;
            arrow.Y2 = endPt.Y;
            arrow.HeadWidth = 15;
            arrow.HeadHeight = 5;
            arrow.Stroke = Brushes.Black;
            arrow.StrokeThickness = 1;
            _canvas.Children.Add(arrow);
        }

  (2)

        private void DrawLeaderLineArrow(Point startPt, Point endPt)
        {
InitializeComponent();
Arrow arrow
= new Arrow(); arrow.X1 = 50; arrow.Y1 = 50; arrow.X2 = 100; arrow.Y2 = 100; arrow.HeadWidth = 15; arrow.HeadHeight = 5; arrow.Stroke = Brushes.Black; arrow.StrokeThickness = 1; canvas.Children.Add(arrow); }

3、在 Graphics上绘制

 3.1、C#的箭头画法 - 孤独的猫 - 博客园.html(https://www.cnblogs.com/djcsch2001/archive/2011/05/07/2039991.html

private void Form1_Paint(object sender, PaintEventArgs e)
{
     Graphics g = e.Graphics;
     g.SmoothingMode = SmoothingMode.AntiAlias;
     g.FillRectangle(Brushes.White, this.ClientRectangle);

     Pen p = new Pen(Color.Black, 10);
     p.StartCap = LineCap.Round;
     p.EndCap = LineCap.ArrowAnchor;
     g.DrawLine(p, 30, 30, 80, 30);
     p.Dispose();
}

 3.2、C#开发教程--如何绘制箭头 - David.Wang 做你喜欢的事情 - CSDN博客.html(https://blog.csdn.net/wanglixin1999/article/details/41183305

Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.FillRectangle(Brushes.White, this.ClientRectangle);
Pen p = new Pen(Color.Black, 10);
p.StartCap = LineCap.Round;
p.EndCap = LineCap.ArrowAnchor;
g.DrawLine(p, 30, 30, 80, 30);
p.Dispose();

4、WPF绘制箭头 - waiting - CSDN博客.html(https://blog.csdn.net/u012366767/article/details/84857000

  ZC:这篇文章 看起来很牛逼,没细看 不知道什么原理,先记录下...

5、

猜你喜欢

转载自www.cnblogs.com/csskill/p/11199102.html