c#Winform Custom Control - Bread Crumb Navigation

Keywords: Windows github git

premise

It's been 7 or 8 years, and I've always wanted to make a beautiful set of custom controls, so I've got this series of articles.

GitHub: https://github.com/kwwwvagaa/NetWinformControl

Code cloud: https://gitee.com/kwwwvagaa/net_winform_custom_control.git

If you think it's OK, please click star to support it.

Welcome to exchange and discuss: Penguins 568015492

Catalog

https://blog.csdn.net/kwwwvagaa/article/details/100586547

Preparation

GDI+ painting, do not know GDI+, you can understand the next.

start

Add a user control named UCCrumbNavigation

Provide attributes

 1  private Color m_navColor = Color.FromArgb(100, 100, 100);
 2 
 3         public Color NavColor
 4         {
 5             get { return m_navColor; }
 6             set
 7             {
 8                 if (value == Color.Empty || value == Color.Transparent)
 9                     return;
10                 m_navColor = value;
11                 Refresh();
12             }
13         }
14 
15 
16         private string[] m_navigations = new string[] { "Directory 1", "Directory 2", "Directory 3" };
17         GraphicsPath[] m_paths;
18         public string[] Navigations
19         {
20             get { return m_navigations; }
21             set
22             {
23                 m_navigations = value;
24                 if (value == null)
25                     m_paths = new GraphicsPath[0];
26                 else
27                     m_paths = new GraphicsPath[value.Length];
28                 Refresh();
29             }
30         }
31 
32         public override Font Font
33         {
34             get
35             {
36                 return base.Font;
37             }
38             set
39             {
40                 base.Font = value;
41                 Refresh();
42             }
43         }
44 
45         public override System.Drawing.Color ForeColor
46         {
47             get
48             {
49                 return base.ForeColor;
50             }
51             set
52             {
53                 base.ForeColor = value;
54                 Refresh();
55             }
56         }

Repaint

 1 protected override void OnPaint(PaintEventArgs e)
 2         {
 3             base.OnPaint(e);
 4 
 5             if (m_navigations != null && m_navigations.Length > 0)
 6             {
 7                 var g = e.Graphics;
 8                 int intLastX = 0;
 9                 int intLength = m_navigations.Length;
10                 for (int i = 0; i < m_navigations.Length; i++)
11                 {
12                     GraphicsPath path = new GraphicsPath();
13                     string strText = m_navigations[i];
14                     System.Drawing.SizeF sizeF = g.MeasureString(strText.Replace(" ", "A"), Font);
15                     int intTextWidth = (int)sizeF.Width + 1;
16                     path.AddLine(new Point(intLastX + 1, 1), new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth, 1));
17 
18                     //if (i != (intLength - 1))
19                     //{
20                     path.AddLine(new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth, 1), new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth + 10, this.Height / 2));
21                     path.AddLine(new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth + 10, this.Height / 2), new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth - 1, this.Height - 1));
22                     //}
23                     //else
24                     //{
25                     //    path.AddLine(new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth, 1), new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth, this.Height - 1));
26                     //}
27 
28                     path.AddLine(new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth, this.Height - 1), new Point(intLastX + 1, this.Height - 1));
29 
30                     if (i != 0)
31                     {
32                         path.AddLine(new Point(intLastX, this.Height - 1), new Point(intLastX + 1 + 10, this.Height / 2));
33                         path.AddLine(new Point(intLastX + 1 + 10, this.Height / 2), new Point(intLastX + 1, 1));
34                     }
35                     else
36                     {
37                         path.AddLine(new Point(intLastX + 1, this.Height - 1), new Point(intLastX + 1, 1));
38                     }
39                     g.FillPath(new SolidBrush(m_navColor), path);
40 
41                     g.DrawString(strText, this.Font, new SolidBrush(this.ForeColor), new PointF(intLastX + 2 + (i == 0 ? 0 : 10), (this.Height - sizeF.Height) / 2 + 1));
42                     m_paths[i] = path;
43                     intLastX += ((i == 0 ? 0 : 10) + intTextWidth + (i == (intLength - 1) ? 0 : 10));
44                 }
45             }
46 
47         }

Handle the click event

 1  void UCCrumbNavigation_MouseDown(object sender, MouseEventArgs e)
 2         {
 3             if (!DesignMode)
 4             {
 5                 if (m_paths != null && m_paths.Length > 0)
 6                 {
 7                     for (int i = 0; i < m_paths.Length; i++)
 8                     {
 9                         if (m_paths[i].IsVisible(e.Location))
10                         {
11                             HZH_Controls.Forms.FrmTips.ShowTipsSuccess(this.FindForm(), m_navigations[i]);
12                         }
13                     }
14                 }
15             }
16         }

The complete code is as follows

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace HZH_Controls.Controls
{
    public partial class UCCrumbNavigation : UserControl
    {
        private Color m_navColor = Color.FromArgb(100, 100, 100);

        public Color NavColor
        {
            get { return m_navColor; }
            set
            {
                if (value == Color.Empty || value == Color.Transparent)
                    return;
                m_navColor = value;
                Refresh();
            }
        }


        private string[] m_navigations = new string[] { "Directory 1", "Directory 2", "Directory 3" };
        GraphicsPath[] m_paths;
        public string[] Navigations
        {
            get { return m_navigations; }
            set
            {
                m_navigations = value;
                if (value == null)
                    m_paths = new GraphicsPath[0];
                else
                    m_paths = new GraphicsPath[value.Length];
                Refresh();
            }
        }

        public override Font Font
        {
            get
            {
                return base.Font;
            }
            set
            {
                base.Font = value;
                Refresh();
            }
        }

        public override System.Drawing.Color ForeColor
        {
            get
            {
                return base.ForeColor;
            }
            set
            {
                base.ForeColor = value;
                Refresh();
            }
        }

        public UCCrumbNavigation()
        {
            InitializeComponent();
            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.MouseDown += UCCrumbNavigation_MouseDown;
        }

        void UCCrumbNavigation_MouseDown(object sender, MouseEventArgs e)
        {
            if (!DesignMode)
            {
                if (m_paths != null && m_paths.Length > 0)
                {
                    for (int i = 0; i < m_paths.Length; i++)
                    {
                        if (m_paths[i].IsVisible(e.Location))
                        {
                            HZH_Controls.Forms.FrmTips.ShowTipsSuccess(this.FindForm(), m_navigations[i]);
                        }
                    }
                }
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (m_navigations != null && m_navigations.Length > 0)
            {
                var g = e.Graphics;
                int intLastX = 0;
                int intLength = m_navigations.Length;
                for (int i = 0; i < m_navigations.Length; i++)
                {
                    GraphicsPath path = new GraphicsPath();
                    string strText = m_navigations[i];
                    System.Drawing.SizeF sizeF = g.MeasureString(strText.Replace(" ", "A"), Font);
                    int intTextWidth = (int)sizeF.Width + 1;
                    path.AddLine(new Point(intLastX + 1, 1), new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth, 1));

                    //if (i != (intLength - 1))
                    //{
                    path.AddLine(new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth, 1), new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth + 10, this.Height / 2));
                    path.AddLine(new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth + 10, this.Height / 2), new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth - 1, this.Height - 1));
                    //}
                    //else
                    //{
                    //    path.AddLine(new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth, 1), new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth, this.Height - 1));
                    //}

                    path.AddLine(new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth, this.Height - 1), new Point(intLastX + 1, this.Height - 1));

                    if (i != 0)
                    {
                        path.AddLine(new Point(intLastX, this.Height - 1), new Point(intLastX + 1 + 10, this.Height / 2));
                        path.AddLine(new Point(intLastX + 1 + 10, this.Height / 2), new Point(intLastX + 1, 1));
                    }
                    else
                    {
                        path.AddLine(new Point(intLastX + 1, this.Height - 1), new Point(intLastX + 1, 1));
                    }
                    g.FillPath(new SolidBrush(m_navColor), path);

                    g.DrawString(strText, this.Font, new SolidBrush(this.ForeColor), new PointF(intLastX + 2 + (i == 0 ? 0 : 10), (this.Height - sizeF.Height) / 2 + 1));
                    m_paths[i] = path;
                    intLastX += ((i == 0 ? 0 : 10) + intTextWidth + (i == (intLength - 1) ? 0 : 10));
                }
            }

        }
    }
}
namespace HZH_Controls.Controls
{
    partial class UCCrumbNavigation
    {
        /// <summary> 
        /// Required designer variables.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up all resources in use.
        /// </summary>
        /// <param name="disposing">true if managed resources should be released; otherwise false. </param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #Code generated by region component designer

        /// <summary> 
        /// Designer supports required methods - no
        /// Use the code editor to modify the content of this method.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // UCCrumbNavigation
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.Cursor = System.Windows.Forms.Cursors.Hand;
            this.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
            this.MinimumSize = new System.Drawing.Size(0, 25);
            this.Name = "UCCrumbNavigation";
            this.Size = new System.Drawing.Size(220, 25);
            this.ResumeLayout(false);

        }

        #endregion


    }
}

Utility and effect

Last words

If you like, please go to https://gitee.com/kwwwvagaa/net_winform_custom_control Spot a star.

Posted by carlosx2 on Wed, 02 Oct 2019 04:17:07 -0700