官网

http://www.hzhcontrols.com

前提

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

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

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

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

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

来都来了,点个【推荐】再走吧,谢谢

NuGet

Install-Package HZH_Controls

目录

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

用处及效果

准备工作

没什么可准备的,直接开干吧。

思路:

2个panel,分别放标题和明细

然后重绘控件,在标题旁边画圆并且连线

开始

添加一个类来存放节点信息

  public class TimeLineItem
{
/// <summary>
/// Gets or sets the title.
/// </summary>
/// <value>The title.</value>
public string Title { get; set; }
/// <summary>
/// Gets or sets the details.
/// </summary>
/// <value>The details.</value>
public string Details { get; set; }
}

添加一个用户控件UCTimeLine

添加一些属性

 /// <summary>
/// The line color
/// </summary>
private Color lineColor = TextColors.Light; /// <summary>
/// Gets or sets the color of the line.
/// </summary>
/// <value>The color of the line.</value>
[Description("连接线颜色"), Category("自定义")]
public Color LineColor
{
get { return lineColor; }
set
{
lineColor = value;
Invalidate();
}
}
/// <summary>
/// The title font
/// </summary>
private Font titleFont = new Font("微软雅黑", 14f); /// <summary>
/// Gets or sets the title font.
/// </summary>
/// <value>The title font.</value>
[Description("标题字体"), Category("自定义")]
public Font TitleFont
{
get { return titleFont; }
set
{
titleFont = value;
ReloadItems();
}
} /// <summary>
/// The title forcolor
/// </summary>
private Color titleForcolor = TextColors.MoreDark; /// <summary>
/// Gets or sets the title forcolor.
/// </summary>
/// <value>The title forcolor.</value>
[Description("标题颜色"), Category("自定义")]
public Color TitleForcolor
{
get { return titleForcolor; }
set
{
titleForcolor = value;
ReloadItems();
}
} /// <summary>
/// The details font
/// </summary>
private Font detailsFont = new Font("微软雅黑", ); /// <summary>
/// Gets or sets the details font.
/// </summary>
/// <value>The details font.</value>
[Description("详情字体"), Category("自定义")]
public Font DetailsFont
{
get { return detailsFont; }
set
{
detailsFont = value;
ReloadItems();
}
} /// <summary>
/// The details forcolor
/// </summary>
private Color detailsForcolor = TextColors.Light; /// <summary>
/// Gets or sets the details forcolor.
/// </summary>
/// <value>The details forcolor.</value>
[Description("详情颜色"), Category("自定义")]
public Color DetailsForcolor
{
get { return detailsForcolor; }
set
{
detailsForcolor = value;
ReloadItems();
}
} /// <summary>
/// The items
/// </summary>
TimeLineItem[] items; /// <summary>
/// Gets or sets the items.
/// </summary>
/// <value>The items.</value>
[Description("项列表"), Category("自定义")]
public TimeLineItem[] Items
{
get { return items; }
set
{
items = value;
ReloadItems();
}
}

构造函数初始化一些东西

   public UCTimeLine()
{
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);
InitializeComponent();
items = new TimeLineItem[];
if (ControlHelper.IsDesignMode())
{
items = new TimeLineItem[];
for (int i = ; i < ; i++)
{
items[i] = new TimeLineItem()
{
Title = DateTime.Now.AddMonths(- * ( - i)).ToString("yyyy年MM月"),
Details = DateTime.Now.AddMonths(- * ( - i)).ToString("yyyy年MM月") + "发生了一件大事,咔嚓一声打了一个炸雷,咔嚓一声打了一个炸雷,咔嚓一声打了一个炸雷,咔嚓一声打了一个炸雷,咔嚓一声打了一个炸雷,咔嚓一声打了一个炸雷,咔嚓一声打了一个炸雷,咔嚓一声打了一个炸雷,咔嚓一声打了一个炸雷,然后王二麻子他爹王咔嚓出生了。"
};
}
ReloadItems();
}
}

重新加载列表

 private void ReloadItems()
{
try
{
ControlHelper.FreezeControl(this, true);
this.Controls.Clear();
if (items != null)
{
foreach (var item in items)
{
FlowLayoutPanel panelTitle = new FlowLayoutPanel();
panelTitle.Dock = DockStyle.Top;
panelTitle.AutoScroll = false;
panelTitle.Padding = new System.Windows.Forms.Padding();
panelTitle.Name = "title_" + Guid.NewGuid().ToString(); Label lblTitle = new Label();
lblTitle.Dock = DockStyle.Top;
lblTitle.AutoSize = true;
lblTitle.Font = titleFont;
lblTitle.ForeColor = titleForcolor;
lblTitle.Text = item.Title;
lblTitle.SizeChanged += item_SizeChanged;
panelTitle.Controls.Add(lblTitle);
this.Controls.Add(panelTitle);
panelTitle.BringToFront(); FlowLayoutPanel panelDetails = new FlowLayoutPanel();
panelDetails.Dock = DockStyle.Top;
panelDetails.AutoScroll = false;
panelDetails.Padding = new System.Windows.Forms.Padding();
panelDetails.Name = "details_" + Guid.NewGuid().ToString();
Label lblDetails = new Label();
lblDetails.AutoSize = true;
lblDetails.Dock = DockStyle.Top;
lblDetails.Font = detailsFont;
lblDetails.ForeColor = detailsForcolor;
lblDetails.Text = item.Details;
lblDetails.SizeChanged += item_SizeChanged;
panelDetails.Controls.Add(lblDetails);
this.Controls.Add(panelDetails);
panelDetails.BringToFront(); }
}
}
finally
{
ControlHelper.FreezeControl(this, false);
}
}

当文本大小改变时改变面板大小

 void item_SizeChanged(object sender, EventArgs e)
{
Label lbl = (Label)sender;
lbl.Parent.Height = lbl.Height + ;
}

重绘来画圆和连线

 protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
g.SetGDIHigh();
var lst = this.Controls.ToArray().Where(p => p.Name.StartsWith("title_")).ToList();
for (int i = ; i < lst.Count; i++)
{
//画圆
g.DrawEllipse(new Pen(new SolidBrush(lineColor)), new Rectangle(, lst[i].Location.Y + , , ));
//划线
if (i != lst.Count - )
{
g.DrawLine(new Pen(new SolidBrush(lineColor)), new Point( + , lst[i].Location.Y + - ), new Point( + , lst[i + ].Location.Y + + + ));
}
}
}

最后的话

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

(八十一)c#Winform自定义控件-时间轴-HZHControls的更多相关文章

  1. (十八)c#Winform自定义控件-提示框

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

  2. (四十八)c#Winform自定义控件-下拉按钮

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  3. (五十一)c#Winform自定义控件-文字提示-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  4. (八十)c#Winform自定义控件-分割线标签-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  5. (三十八)c#Winform自定义控件-圆形进度条-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  6. (五十八)c#Winform自定义控件-管道阀门(工业)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  7. (八)c#Winform自定义控件-分割线

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

  8. (二十八)c#Winform自定义控件-文本框(一)

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

  9. (六十八)c#Winform自定义控件-DEMO整理

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

随机推荐

  1. 关于token你需要知道的【华为云技术分享】

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/devcloud/article/detai ...

  2. 转:OAuth2 深入介绍

    OAuth2 深入介绍 1. 前言 2. OAuth2 角色 2.1 资源所有者(Resource Owner) 2.2 资源/授权服务器(Resource/Authorization Server) ...

  3. 利用python实现dll依赖关系导出

    #说明:遍历rootdir目录下所有dll,导出每个dll依赖的dll信息到dstdir目录下 # 配合NotePad++打开所有txt,搜索,可快速定位到某dll被依赖的所有dll文件 import ...

  4. 洛谷 题解 P5595 【【XR-4】歌唱比赛】

    本蒟蒻又双叒叕被爆踩了. 考试时一遍过 其实这题还是很简单的,难度不会大于普及组T1. CSP 2019 RP++ 看开始看到题目,觉得特别长,不想看... 我来和你们分析分析题目,你们就都可以秒了. ...

  5. 小白学 Python 爬虫(17):Requests 基础使用

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  6. A.Sweet Problem

    题目:甜蜜的问题 题意:你有三堆糖果:红色,绿色,蓝色 第一堆有r个糖果,第二堆有g个糖果,第三堆有b个糖果 每天都可以吃两个不同颜色的糖果,找出可以吃糖果的最大天数 分析:先排下序,如果最大堆大于等 ...

  7. 递归的实际业务场景之MySQL 递归查询

    喜欢就点个赞呗! 源码<--请点击此处查看 引入 当我看到一些评论时,例如下面的样子.我挺好奇这个功能是怎么样做出来的.进过查阅资料,发现这其实是 MySQL 的递归操作.下面就让我操作一下怎么 ...

  8. 剑指Offer-41.和为S的连续正数序列(C++/Java)

    题目: 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久,他就 ...

  9. 想要金九银十面试通关,不懂 Java多线程肯定是不行的!

    作者 | 纳达丶无忌 如果对什么是线程.什么是进程仍存有疑惑,请先 Google 之,因为这两个概念不在本文的范围之内. 用多线程只有一个目的,那就是更好的利用 CPU 的资源,因为所有的多线程代码都 ...

  10. webpack实践(三)- html-webpack-plugin

    webpack系列博客中代码均在github上:https://github.com/JEmbrace/webpack-practice <webpack实践(一)- 先入个门> < ...