// ***********************************************************************
// Assembly : HZH_Controls
// Created : 08-19-2019
//
// ***********************************************************************
//
// 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.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
{
///
/// Class UCCrumbNavigation.
/// Implements the
///
///
[DefaultEvent("ClickItemed")]
public partial class UCCrumbNavigation : UserControl
{
///
/// The m nav color
///
private Color m_navColor = Color.FromArgb(255, 77, 59);
///
/// Gets or sets the color of the nav.
///
/// The color of the nav.
public Color NavColor
{
get { return m_navColor; }
set
{
if (value == Color.Empty || value == Color.Transparent)
return;
m_navColor = value;
Refresh();
}
}
///
/// The m paths
///
GraphicsPath[] m_paths;
private CrumbNavigationItem[] items;
public CrumbNavigationItem[] Items
{
get { return items; }
set
{
items = value;
if (value == null)
m_paths = new GraphicsPath[0];
else
m_paths = new GraphicsPath[value.Length];
Refresh();
}
}
///
/// 获取或设置控件显示的文字的字体。
///
/// The font.
///
///
///
///
///
///
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
Refresh();
}
}
///
/// 获取或设置控件的前景色。
///
/// The color of the fore.
///
///
///
public override System.Drawing.Color ForeColor
{
get
{
return base.ForeColor;
}
set
{
base.ForeColor = value;
Refresh();
}
}
public delegate void CrumbNavigationEventHander(object sender, CrumbNavigationClickEventArgs e);
public event CrumbNavigationEventHander ClickItemed;
///
/// Initializes a new instance of the class.
///
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;
Items = new CrumbNavigationItem[0];
if (ControlHelper.IsDesignMode())
{
Items = new CrumbNavigationItem[3];
for (int i = 0; i < 3; i++)
{
Items[i] = new CrumbNavigationItem() { Text = "item" + (i + 1), Key = i.ToString() };
}
}
}
///
/// Handles the MouseDown event of the UCCrumbNavigation control.
///
/// The source of the event.
/// The instance containing the event data.
void UCCrumbNavigation_MouseDown(object sender, MouseEventArgs e)
{
if (ClickItemed != null)
{
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))
{
ClickItemed(this, new CrumbNavigationClickEventArgs() { Index = i, Item = items[i] });
}
}
}
}
}
}
///
/// Handles the event.
///
/// The instance containing the event data.
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (items != null && items.Length > 0)
{
var g = e.Graphics;
g.SetGDIHigh();
int intLastX = 0;
int intLength = items.Length;
for (int i = 0; i < items.Length; i++)
{
GraphicsPath path = new GraphicsPath();
string strText = items[i].Text;
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((items[i].ItemColor == null || items[i].ItemColor == Color.Empty || items[i].ItemColor == Color.Transparent) ? m_navColor : items[i].ItemColor.Value), 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));
}
}
}
}
}