// *********************************************************************** // Assembly : HZH_Controls // Created : 2019-09-10 // // *********************************************************************** // // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com // // // Blog: https://www.cnblogs.com/bfyx // GitHub:https://github.com/kwwwvagaa/NetWinformControl // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git // // If you use this code, please keep this note. // *********************************************************************** using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing; using System.Drawing.Drawing2D; using System.ComponentModel; namespace HZH_Controls.Controls { /// /// Class UCArrow. /// Implements the /// /// public class UCArrow : UserControl { /// /// The arrow color /// private Color arrowColor = Color.FromArgb(255, 77, 59); /// /// Gets or sets the color of the arrow. /// /// The color of the arrow. [Description("箭头颜色"), Category("自定义")] public Color ArrowColor { get { return arrowColor; } set { arrowColor = value; Refresh(); } } /// /// The border color /// private Color? borderColor = null; /// /// Gets or sets the color of the border. /// /// The color of the border. [Description("箭头边框颜色,为空则无边框"), Category("自定义")] public Color? BorderColor { get { return borderColor; } set { borderColor = value; Refresh(); } } /// /// The direction /// private ArrowDirection direction = ArrowDirection.Right; /// /// Gets or sets the direction. /// /// The direction. [Description("箭头方向"), Category("自定义")] public ArrowDirection Direction { get { return direction; } set { direction = value; ResetPath(); Refresh(); } } /// /// 获取或设置控件显示的文字的字体。 /// /// The font. /// /// /// /// /// /// public override Font Font { get { return base.Font; } set { base.Font = value; Refresh(); } } /// /// 获取或设置控件的前景色。 /// /// The color of the fore. /// /// /// public override Color ForeColor { get { return base.ForeColor; } set { base.ForeColor = value; Refresh(); } } /// /// The text /// private string text; /// /// Gets or sets the text. /// /// The text. [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(); } } /// /// The m path /// GraphicsPath m_path; /// /// Initializes a new instance of the class. /// public UCArrow() { 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 += UCArrow_SizeChanged; this.Size = new Size(100, 50); } /// /// Handles the SizeChanged event of the UCArrow control. /// /// The source of the event. /// The instance containing the event data. void UCArrow_SizeChanged(object sender, EventArgs e) { ResetPath(); } /// /// Resets the path. /// 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(); } /// /// 引发 事件。 /// /// 包含事件数据的 。 protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); var g = e.Graphics; g.SetGDIHigh(); base.Region = new Region(m_path); 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); g.DrawString(Text, Font, new SolidBrush(ForeColor), new PointF((this.Width - size.Width) / 2, (this.Height - size.Height) / 2)); } } } /// /// Enum ArrowDirection /// public enum ArrowDirection { /// /// The left /// Left, /// /// The right /// Right, /// /// The top /// Top, /// /// The bottom /// Bottom, /// /// The left right /// Left_Right, /// /// The top bottom /// Top_Bottom } }