官网

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. 用Python调用华为云API接口发短信

    [摘要] 用Python调用华为云API接口实现发短信,当然能给调用发短信接口前提条件是通过企业实名认证,而且有一个通过审核的短信签名,话不多说,showcode #!/usr/bin/python3 ...

  2. sbt assembly a fat jar for spark-submit cluster model

    在用spark-submit提交作业时,用sbt package打包好的jar程序,可以很好的运行在client模式,当在cluster模式, 一直报错:Exception in thread &qu ...

  3. js 防抖、截流

    突发奇想,在触发事件的时候,一些会频繁触发的事件会不会造成资源的浪费或者大量的计算造成页面卡顿,比如onresize,onscroll,onmousemove等事件. 然后就引出了一个新知识点:防抖. ...

  4. MVC 入门 自动生成 增删改查所有功能

    MVC现在版本已经是5了   EF现在最新的应该是6.0.2了 开发工具是 Visual Studio2013 数据库是 SQL Server 2012 这些需要.NET Framework4.5 的 ...

  5. [TimLinux] JavaScript 中循环执行和定时执行

    1. 两对函数 // 循环执行 // 在每个毫秒数之后,调用函数 var timeid = window.setInterval(函数名, 毫秒数); window.clearInterval(tim ...

  6. nitacm20317 来自张司机的挑战书

    题目:让你求从x到y中(1<=x<=y<=10^18),二进制一的个数最多的数是哪个,如果有多个相同的答案,输出最小的. 题目链接:https://www.nitacm.com/pr ...

  7. InputStream 读取中文乱码 扩展

    对于InputStream读取中文乱码,下面这段话给出了很好的解释,以及后续编码上的扩展. BufferedInputStream和BufferedOutputStream是过滤流,需要使用已存在的节 ...

  8. Day 05 作业

    目录 作业 输入姑娘的年龄后,进行以下判断: 复习while循环,打印1-100之间的奇数和 复习while循环,猜年龄游戏升级版,有以下三点要求: 作业 输入姑娘的年龄后,进行以下判断: 如果姑娘小 ...

  9. 选择排序 C&&C++

    选择排序 选择排序即在每一步中选取最小值重新排列,从而达到排序的目的   流程: (1)先从原始数组选择一个最小数据和第一个位置交换 (2)剩下的n-1个数据选择最小的和第二个位置交换 (3)不断重复 ...

  10. 你不知道的JavaScript(中)读书笔记(二)

    第三章 原生函数 常用的原生函数(内建函数)有: String() Number() Boolean Array() Object() Function() RegExp() Date() Erroe ...