准备工作
GDI+画的,不了解GDI+可以百度了解下先
开始
添加一个用户控件,命名UCCrumbNavigation
提供属性
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[] { "目录1", "目录2", "目录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();
}
}
重绘
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));
}
}
}
处理一下点击事件
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]);
}
}
}
}
}
完整代码如下
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[] { "目录1", "目录2", "目录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>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </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
}
}
用处及效果

最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星 星吧
作者:冰封一夏
出处: http://www.hzhcontrols.com/doc.html
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,
且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git