箭头

1、效果图,向左,向右,向上,向下

 

设置参数

 

2  、代码编写

 public partial class UArrowControl : UserControl
    {
        /// <summary>
        /// 报警灯颜色
        /// </summary>
        private Color arrowColor = Color.Red;

        [Description("箭头颜色"), Category("自定义")]
        public Color ArrowColor
        {
            get { return arrowColor; }
            set
            {
                arrowColor = value;
                Refresh();
            }
        }

        /// <summary>
        /// 灯的边框颜色
        /// </summary>
        private Color? borderColor = null;

        [Description("箭头的边框颜色,为空则无边框"), Category("自定义")]
        public Color? BorderColor
        {
            get { return borderColor; }
            set
            {
                borderColor = value;
                Refresh();
            }
        }

        /// <summary>
        /// The direction
        /// </summary>
        private ArrowDirection direction = ArrowDirection.Right;

        [Description("箭头方向"), Category("自定义")]
        public ArrowDirection Direction
        {
            get { return direction; }
            set
            {
                direction = value;
                ResetPath();
                Refresh();
            }
        }
        /// <summary>
        /// 获取或设置控件显示的文字的字体。
        /// </summary>
        /// <value>The font.</value>
        public override Font Font
        {
            get
            {
                return base.Font;
            }
            set
            {
                base.Font = value;
                Refresh();
            }
        }
        /// <summary>
        /// 获取或设置控件的前景色。
        /// </summary>
        /// <value>The color of the fore.</value>

        /// </PermissionSet>
        public override Color ForeColor
        {
            get
            {
                return base.ForeColor;
            }
            set
            {
                base.ForeColor = value;
                Refresh();
            }
        }
        /// <summary>
        /// The text
        /// </summary>
        private string text;
        /// <summary>
        /// Gets or sets the text.
        /// </summary>
        /// <value>The text.</value>
        [Bindable(true)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Localizable(true)]
        [Description("箭头文字"), Category("自定义")]
        public override string Text
        {
            get
            {
                return text;
            }
            set
            {
                text = value;
                Refresh();
            }
        }
        /// <summary>
        /// The m path
        /// </summary>
        GraphicsPath m_path;
        /// <summary>
        /// Initializes a new instance of the <see cref="UCArrow"/> class.
        /// </summary>
        public UArrowControl()
        {
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.Selectable, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.ForeColor = Color.White;
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.SizeChanged +=AlarmControl_SizeChanged;
            this.Size = new Size(100, 50);
        }

        /// <summary>
        /// Handles the SizeChanged event of the UCArrow control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        void AlarmControl_SizeChanged(object sender, EventArgs e)
        {
            ResetPath();
        }

        /// <summary>
        /// 重置路径
        /// </summary>
        private void ResetPath()
        {
            Point[] ps = null;
            switch (direction)
            {
                case ArrowDirection.Left:
                    ps = new Point[]
                    {
                        new Point(0,this.Height/2),
                        new Point(40,0),
                        new Point(40,this.Height/4),
                        new Point(this.Width-1,this.Height/4),
                        new Point(this.Width-1,this.Height-this.Height/4),
                        new Point(40,this.Height-this.Height/4),
                        new Point(40,this.Height),
                        new Point(0,this.Height/2)
                    };
                    break;
                case ArrowDirection.Right:
                    ps = new Point[]
                    {
                        new Point(0,this.Height/4),
                        new Point(this.Width-40,this.Height/4),
                        new Point(this.Width-40,0),
                        new Point(this.Width-1,this.Height/2),
                        new Point(this.Width-40,this.Height),
                        new Point(this.Width-40,this.Height-this.Height/4),
                        new Point(0,this.Height-this.Height/4),
                        new Point(0,this.Height/4)
                    };
                    break;
                case ArrowDirection.Top:
                    ps = new Point[]
                    {
                       new Point(this.Width/2,0),
                       new Point(this.Width,40),
                       new Point(this.Width-this.Width/4,40),
                       new Point(this.Width-this.Width/4,this.Height-1),
                       new Point(this.Width/4,this.Height-1),
                       new Point(this.Width/4,40),
                       new Point(0,40),
                       new Point(this.Width/2,0),
                    };
                    break;
                case ArrowDirection.Bottom:
                    ps = new Point[]
                    {
                       new Point(this.Width-this.Width/4,0),
                       new Point(this.Width-this.Width/4,this.Height-40),
                       new Point(this.Width,this.Height-40),
                       new Point(this.Width/2,this.Height-1),
                       new Point(0,this.Height-40),
                       new Point(this.Width/4,this.Height-40),
                       new Point(this.Width/4,0),
                       new Point(this.Width-this.Width/4,0),
                    };
                    break;
                case ArrowDirection.Left_Right:
                    ps = new Point[]
                    {
                        new Point(0,this.Height/2),
                        new Point(40,0),
                        new Point(40,this.Height/4),
                        new Point(this.Width-40,this.Height/4),
                        new Point(this.Width-40,0),
                        new Point(this.Width-1,this.Height/2),
                        new Point(this.Width-40,this.Height),
                        new Point(this.Width-40,this.Height-this.Height/4),
                        new Point(40,this.Height-this.Height/4),
                        new Point(40,this.Height),
                        new Point(0,this.Height/2),
                    };
                    break;
                case ArrowDirection.Top_Bottom:
                    ps = new Point[]
                    {
                       new Point(this.Width/2,0),
                       new Point(this.Width,40),
                       new Point(this.Width-this.Width/4,40),
                       new Point(this.Width-this.Width/4,this.Height-40),
                       new Point(this.Width,this.Height-40),
                       new Point(this.Width/2,this.Height-1),
                       new Point(0,this.Height-40),
                       new Point(this.Width/4,this.Height-40),
                       new Point(this.Width/4,40),
                       new Point(0,40),
                       new Point(this.Width/2,0),
                    };
                    break;
            }
            m_path = new GraphicsPath();
            m_path.AddLines(ps);
            m_path.CloseAllFigures();
        }

        /// <summary>
        /// 引发 事件。
        /// </summary>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            var g = e.Graphics;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.FillPath(new SolidBrush(ArrowColor), m_path);

            if (borderColor != null && borderColor != Color.Empty)
                g.DrawPath(new Pen(new SolidBrush(borderColor.Value)), m_path);
            if (!string.IsNullOrEmpty(text))
            {
                var size = g.MeasureString(Text, Font);
                RectangleF rect = new RectangleF((this.Width - size.Width) / 2, (this.Height - size.Height) / 2, this.Width/2, this.Height / 2);
                StringFormat format = new StringFormat();
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;
                g.DrawString(Text, Font, new SolidBrush(ForeColor), rect,format);
            }
        }
    }

    /// <summary>
    /// Enum ArrowDirection
    /// </summary>
    public enum ArrowDirection
    {
        /// <summary>
        /// The left
        /// </summary>
        Left,
        /// <summary>
        /// The right
        /// </summary>
        Right,
        /// <summary>
        /// The top
        /// </summary>
        Top,
        /// <summary>
        /// The bottom
        /// </summary>
        Bottom,
        /// <summary>
        /// The left right
        /// </summary>
        Left_Right,
        /// <summary>
        /// The top bottom
        /// </summary>
        Top_Bottom

    }

 

posted @ 2024-05-20 11:15  陌念  阅读(34)  评论(0)    收藏  举报