官网

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. 20141126-传智播客.NET3.2版

  2. 【学习笔记】动态规划—斜率优化DP(超详细)

    [学习笔记]动态规划-斜率优化DP(超详细) [前言] 第一次写这么长的文章. 写完后感觉对斜优的理解又加深了一些. 斜优通常与决策单调性同时出现.可以说决策单调性是斜率优化的前提. 斜率优化 \(D ...

  3. mac环境下java项目无创建文件的权限

    1.问题: 先抛问题,由于刚刚换用mac环境,之前windows上开发的代码调试完毕,还未上线.之后上线部署之前,tl直连测试本地环境(mac)环境,功能无法使用,显示java.io.IOExcept ...

  4. 五分钟了解Zabbix

    Zabbix-简介 Zabbix概念 Zabbix组成 Server Zabbix server 是 Zabbix agent 向其报告可用性.系统完整性信息和统计信息的核心组件.是存储所有配置信息. ...

  5. python 3.5学习笔记(第五章)

    本章内容 1.什么是模块 2.模块的导入方法 3.搜索路径 4.重要标准库 一.什么是模块 1.模块本质上是一个以.py 结尾的python文件,包含了python对象定义和python语句. 2.模 ...

  6. Python 3.5学习笔记(第二章)

    本章内容 1.模块 2.数据类型与数据运算 3.进制 4.byte 与 string 的互相转换 5.列表 6.元组 7.字符串操作 8.字典 一.模块 Python 把某些常用的定义存放在文件中,为 ...

  7. pgsql查询优化之模糊查询

    前言 一直以来,对于搜索时模糊匹配的优化一直是个让人头疼的问题,好在强大pgsql提供了优化方案,下面就来简单谈一谈如何通过索引来优化模糊匹配 案例 我们有一张千万级数据的检查报告表,需要通过检查报告 ...

  8. 你可能不知道的github的秘密

    github也可以使用快捷键 先举例子,如何快速查找项目中的文件? 只需要进入项目,并按下T键 在浏览代码时,如何快速跳到指定行? 只需要进入项目,并按下L键 下面是一些常用的快捷键 聚焦搜索栏 按下 ...

  9. redis 发布与订阅原理分析

    前言:用了redis也有一段时间了,但是发布与订阅的使用频率也不高,趁着这次空闲,深究下redis的发布与订阅模式. 一.订阅频道和信息发布 功能说明:Redis 的 SUBSCRIBE 命令可以让客 ...

  10. 个人永久性免费-Excel催化剂功能第60波-数据有效性验证增强版,补足Excel天生不足

    Excel在数据处理.数据分析上已经是公认的最好用的软件之一,其易用性和强大性也吸引无数的初中高级用户每天都在使用Excel.但这些优点的同时,也带出了一些问题,正因为其不同于一般的专业软件,需要专业 ...