官网

http://www.hzhcontrols.com

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

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

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 

idkey=6e08741ef16fe53bf0314c1c9e336c4f626047943a8b76bac062361bab6b4f8d">

目录

https://www.cnblogs.com/bfyx/p/11364884.html

准备工作

GDI+画的,不了解GDI+可以百度了解下先

开始

添加一个用户控件,命名UCCrumbNavigation

提供属性

  private Color m_navColor = Color.FromArgb(, , );

         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[];
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 > )
{
var g = e.Graphics;
int intLastX = ;
int intLength = m_navigations.Length;
for (int i = ; 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 + ;
path.AddLine(new Point(intLastX + , ), new Point(intLastX + + (i == ? : ) + intTextWidth, )); //if (i != (intLength - 1))
//{
path.AddLine(new Point(intLastX + + (i == ? : ) + intTextWidth, ), new Point(intLastX + + (i == ? : ) + intTextWidth + , this.Height / ));
path.AddLine(new Point(intLastX + + (i == ? : ) + intTextWidth + , this.Height / ), new Point(intLastX + + (i == ? : ) + intTextWidth - , this.Height - ));
//}
//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 + + (i == ? : ) + intTextWidth, this.Height - ), new Point(intLastX + , this.Height - )); if (i != )
{
path.AddLine(new Point(intLastX, this.Height - ), new Point(intLastX + + , this.Height / ));
path.AddLine(new Point(intLastX + + , this.Height / ), new Point(intLastX + , ));
}
else
{
path.AddLine(new Point(intLastX + , this.Height - ), new Point(intLastX + , ));
}
g.FillPath(new SolidBrush(m_navColor), path); g.DrawString(strText, this.Font, new SolidBrush(this.ForeColor), new PointF(intLastX + + (i == ? : ), (this.Height - sizeF.Height) / + ));
m_paths[i] = path;
intLastX += ((i == ? : ) + intTextWidth + (i == (intLength - ) ? : ));
}
} }

处理一下点击事件

  void UCCrumbNavigation_MouseDown(object sender, MouseEventArgs e)
{
if (!DesignMode)
{
if (m_paths != null && m_paths.Length > )
{
for (int i = ; 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(, , ); 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[];
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 > )
{
for (int i = ; 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 > )
{
var g = e.Graphics;
int intLastX = ;
int intLength = m_navigations.Length;
for (int i = ; 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 + ;
path.AddLine(new Point(intLastX + , ), new Point(intLastX + + (i == ? : ) + intTextWidth, )); //if (i != (intLength - 1))
//{
path.AddLine(new Point(intLastX + + (i == ? : ) + intTextWidth, ), new Point(intLastX + + (i == ? : ) + intTextWidth + , this.Height / ));
path.AddLine(new Point(intLastX + + (i == ? : ) + intTextWidth + , this.Height / ), new Point(intLastX + + (i == ? : ) + intTextWidth - , this.Height - ));
//}
//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 + + (i == ? : ) + intTextWidth, this.Height - ), new Point(intLastX + , this.Height - )); if (i != )
{
path.AddLine(new Point(intLastX, this.Height - ), new Point(intLastX + + , this.Height / ));
path.AddLine(new Point(intLastX + + , this.Height / ), new Point(intLastX + , ));
}
else
{
path.AddLine(new Point(intLastX + , this.Height - ), new Point(intLastX + , ));
}
g.FillPath(new SolidBrush(m_navColor), path); g.DrawString(strText, this.Font, new SolidBrush(this.ForeColor), new PointF(intLastX + + (i == ? : ), (this.Height - sizeF.Height) / + ));
m_paths[i] = path;
intLastX += ((i == ? : ) + intTextWidth + (i == (intLength - ) ? : ));
}
} }
}
}
 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)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.MinimumSize = new System.Drawing.Size(, );
this.Name = "UCCrumbNavigation";
this.Size = new System.Drawing.Size(, );
this.ResumeLayout(false); } #endregion }
}

用处及效果

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星 星吧

(三十九)c#Winform自定义控件-面包屑导航的更多相关文章

  1. (三十)c#Winform自定义控件-文本框(三)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  2. Bootstrap <基础十八>面包屑导航(Breadcrumbs)

    面包屑导航(Breadcrumbs)是一种基于网站层次信息的显示方式.以博客为例,面包屑导航可以显示发布日期.类别或标签.它们表示当前页面在导航层次结构内的位置. Bootstrap 中的面包屑导航( ...

  3. NeHe OpenGL教程 第三十九课:物理模拟

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  4. Java进阶(三十九)Java集合类的排序,查找,替换操作

    Java进阶(三十九)Java集合类的排序,查找,替换操作 前言 在Java方向校招过程中,经常会遇到将输入转换为数组的情况,而我们通常使用ArrayList来表示动态数组.获取到ArrayList对 ...

  5. Gradle 1.12用户指南翻译——第三十九章. IDEA 插件

    本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

  6. SQL注入之Sqli-labs系列第三十八关、第三十九关,第四十关(堆叠注入)

    0x1 堆叠注入讲解 (1)前言 国内有的称为堆查询注入,也有称之为堆叠注入.个人认为称之为堆叠注入更为准确.堆叠注入为攻击者提供了很多的攻击手段,通过添加一个新 的查询或者终止查询,可以达到修改数据 ...

  7. 第三百三十九节,Python分布式爬虫打造搜索引擎Scrapy精讲—Scrapy启动文件的配置—xpath表达式

    第三百三十九节,Python分布式爬虫打造搜索引擎Scrapy精讲—Scrapy启动文件的配置—xpath表达式 我们自定义一个main.py来作为启动文件 main.py #!/usr/bin/en ...

  8. centos shell编程5 LANMP一键安装脚本 lamp sed lnmp 变量和字符串比较不能用-eq cat > /usr/local/apache2/htdocs/index.php <<EOF重定向 shell的变量和函数命名不能有横杠 平台可以用arch命令,获取是i686还是x86_64 curl 下载 第三十九节课

    centos shell编程5  LANMP一键安装脚本 lamp  sed  lnmp  变量和字符串比较不能用-eq  cat > /usr/local/apache2/htdocs/ind ...

  9. “全栈2019”Java第三十九章:构造函数、构造方法、构造器

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

随机推荐

  1. Q&A-20180128

    Orleans与Akka对比,为什么选用Orleans? 答: Akka对参与开发的人员要求更高一些,普遍是专家级别,Orleans框架进一步抽象了一层,结合C#语言特性,能普遍降低开发难度. 下面是 ...

  2. 【EdgeBoard体验】开箱与上手

    简介 市面上基于嵌入式平台的神经网络加速平台有很多,今天给大家带来是百度大脑出品的EdgeBoard.按照官网文档的介绍,EdgeBoard是基于Xilinx Zynq Ultrascale+ MPS ...

  3. 使用DQL查询数据库

    DQL ( Data Query Language) 是数据查询语言,如 Select 语句#### 一.DQL 语法 ```sqlSELECT [ALL | DISTINCT]{* | table. ...

  4. Android studio 混淆打包安装后报错NullPointerException int java.util.List.size()

    菜鸟的我,尝试混淆打包app...打包之前没有什么问题,混淆打包之后遇到各种问题.首先,感谢原博主的分享.解决了我的问题.谢谢. 原文地址:http://blog.csdn.net/tou_star/ ...

  5. WGS84坐标与web墨卡托投影坐标转换

    许久没有使用坐标转换,记忆有些模糊了,以后还是会用到,先将WGS84与web墨卡托转换复习一下: 1.84转web墨卡托 //核心公式 平面坐标x = 经度*20037508.34/108 平面坐标y ...

  6. ThinkPHP5.0 模板

    ThinkPHP5.0 模板 模板渲染 默认的视图目录是默认的模块下的view目录 渲染规则:调用 \think\View 类fetch方法 // [模板文件目录]/当前控制器名(小写+下划线)/当前 ...

  7. python带有GIL解释器锁

    1.GIL是什么?GIL的全称是Global Interpreter Lock(全局解释器锁),来源是python设计之初的考虑,为了数据安全所做的决定. 2.每个CPU在同一时间只能执行一个线程(在 ...

  8. 菜单(menu)

    菜单 menu ——菜单默认隐藏 ——实现菜单的接口: Menu,父接口,用于创建主菜单 SubMenu继承Menu接口,用于创建子菜单 ContextMenu接口继承Menu接口,用于创建上下文菜单 ...

  9. 8 NLP-自然语言处理Demo

    1 NLP(自然语言处理) 1.1相似度 相似度和距离之间关系: 1.文本相似度: 1) 语义相似.但字面不相似: 老王的个人简介 铁王人物介绍 2) 字面相似.但是语义不相似: 我吃饱饭了 我吃不饱 ...

  10. httpclient信任所有证书解决SSLException:Unrecognized SSL message,plaintext connection

    在使用 HttpClient 工具调用第三方 Http 接口时报错 javax.net.ssl.SSLException:Unrecognized SSL message,plaintext conn ...