(三十九)c#Winform自定义控件-面包屑导航
官网
前提
入行已经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自定义控件-面包屑导航的更多相关文章
- (三十)c#Winform自定义控件-文本框(三)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- Bootstrap <基础十八>面包屑导航(Breadcrumbs)
面包屑导航(Breadcrumbs)是一种基于网站层次信息的显示方式.以博客为例,面包屑导航可以显示发布日期.类别或标签.它们表示当前页面在导航层次结构内的位置. Bootstrap 中的面包屑导航( ...
- NeHe OpenGL教程 第三十九课:物理模拟
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- Java进阶(三十九)Java集合类的排序,查找,替换操作
Java进阶(三十九)Java集合类的排序,查找,替换操作 前言 在Java方向校招过程中,经常会遇到将输入转换为数组的情况,而我们通常使用ArrayList来表示动态数组.获取到ArrayList对 ...
- Gradle 1.12用户指南翻译——第三十九章. IDEA 插件
本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...
- SQL注入之Sqli-labs系列第三十八关、第三十九关,第四十关(堆叠注入)
0x1 堆叠注入讲解 (1)前言 国内有的称为堆查询注入,也有称之为堆叠注入.个人认为称之为堆叠注入更为准确.堆叠注入为攻击者提供了很多的攻击手段,通过添加一个新 的查询或者终止查询,可以达到修改数据 ...
- 第三百三十九节,Python分布式爬虫打造搜索引擎Scrapy精讲—Scrapy启动文件的配置—xpath表达式
第三百三十九节,Python分布式爬虫打造搜索引擎Scrapy精讲—Scrapy启动文件的配置—xpath表达式 我们自定义一个main.py来作为启动文件 main.py #!/usr/bin/en ...
- 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 ...
- “全栈2019”Java第三十九章:构造函数、构造方法、构造器
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
随机推荐
- 「玩转Python」突破封锁继续爬取百万妹子图
前言 从零学 Python 案例,自从提交第一个妹子图版本引来了不少小伙伴的兴趣.最近,很多小伙伴发来私信说,妹子图不能爬了!? 趁着周末试了一把,果然爬不动了,爬下来的都是些 0kb 的假图片,然后 ...
- VUE v-for循环中每个item节点动态绑定不同函数方法
一. 业务场景: 一个title 处 可能有 一个或多个按钮, 按钮对应不同的响应事件 二. 思路 : 按钮个数 根据传入的数据length 来循环渲染, 每条数据对应的事件名称 通过动态绑定 三 ...
- css inline-block 水平居中
给父元素添加text-align: center即可. body { text-align: center; background-color: black; } #outer { margin: 1 ...
- DEDECMS教程:织梦栏目更新HTML出现“模板文件不存在,无法解析文档”的解决方法(转)
- 利用 ssh 传输文件
前提条件: 服务器要开启写入权限: 本地和服务器都要安装有 scp 包: 如何传输: 1. 从服务器上下载文件: scp username@servername:远程目录/文件名 本地目录 例:scp ...
- spark 源码分析之六--Spark RPC剖析之Dispatcher和Inbox、Outbox剖析
在上篇 spark 源码分析之五 -- Spark内置RPC机制剖析之一创建NettyRPCEnv 中,涉及到了Diapatcher 内容,未做过多的剖析.本篇来剖析一下它的工作原理. Dispatc ...
- vue更改了数据但是视图却没有更新
今日总结:让钩子函数背锅的一上午,跪求原谅 需求:页面刷新时,保留当前页面的选中状态 解决思路:在data里设置一下初始值,保证首次进入有默认选中状态,其次再操作时,把选中的状态存sessionSto ...
- Android通过辅助功能实现抢微信红包原理简单介绍
简书文章:https://www.jianshu.com/p/e1099a94b979 附抢红包开源项目地址,代码已全改为Kotlin了,已适配到最新微信7.0.5版本,如果对你有所帮助赏个star吧 ...
- 阿里技术面全A,终面却被产品经理拉下马。。。
大纲: 一.投递简历 二.准备面试 三.技术一面 四.健身房里的技术二面 五.产品经理的死亡三面 六.总结 一.投递简历 找内推.大公司投简历尽量找内推,无论是校招还是社招.校招可以去牛客网或知乎找, ...
- DDMS 视图 Emulator Control 为灰色
Emulator Control 模拟发送短信时,发现所有选项均为灰色,如图所示: 解决方法: 确认以下四种情形或方法 已测试 Genymotion 模拟器和真机均不行,而Eclipse自带模拟器可以 ...