官网

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. mysql那些事(3)小数如何存储

    创建mysql数据表的时候,经常会遇到存储小数(浮点数)的情况,如:价格,重量,身高等. 目前大的公司流行三种存储方案: 1.将数据扩大10的倍数达到使用整数类型存储目的. 比如价格,我们经常以分为单 ...

  2. c++关于multiset的头文件包含问题

    最近在Bilibili上看到不少侯捷老师C++的视频教程,侯捷老师翻译了很多C++的经典书籍,比如<Essential C++中文版>.<STL源码剖析>,也写了<深入浅 ...

  3. 【开发者portal在线开发插件系列二】多条上下行消息(messageId的使用)【华为云技术分享】

    前言和基本操作请参考[开发者portal在线开发插件系列一]profile和基本上下行消息,此处不再复述,没操作过的小伙伴一定要先去看看哦~ 话不多说,开始今天的演(表)示(演) 场景说明: 假设一: ...

  4. replace find join

    >>> s='spam' >>> s=s.replace('m','111') >>> s 'spa111' >>> where ...

  5. Internet History,Technology,and Security - The Web Makes it Easy to Use(Week3)

    时间如白驹过隙,又到了新的一周的慕课学习啦.这周内容较为简单,主要讲述互联网内部的发展状况. The Early World-Wide-Web Getting to the Web 谈到万维网,我们不 ...

  6. 洛谷 题解 CF1151D 【Stas and the Queue at the Buffet】

    本蒟蒻又双叒叕被爆踩辣!!! 题目链接 这道题我个人觉得没有紫题的水平. 步入正题 先看题: 共有n个人,每个人2个属性,a,b; 窝们要求的是总的不满意度最小,最满意度的公式是什么? \(ai * ...

  7. ecosystem.config

    ecosystem.config.js module.exports = { apps : [{ name : 'TOB_NODE', script : 'app.js', // 开发环境变量 env ...

  8. 如何打造个人km知识管理系统

    经常有朋友会遇到这样一种情况,在网络中看到一篇很好的文章,但后来因为关键字想不起来,结果怎么都搜索不到.还有些朋友虽然平时也会做一些记录,把有用的资料进行保存,但他们往往将保存的资料分散在不同的地方, ...

  9. JavaEE基础(04):会话跟踪技术,Session和Cookie详解

    本文源码:GitHub·点这里 || GitEE·点这里 一.会话跟踪 1.场景描述 比如登录某个购物网站,身份识别成功后,在网站下单,支付 等操作,这些操作中当前登录用户信息必须是共享的,这样这些操 ...

  10. 可扩展的Java线程池执行器

    分享一下最近优锐课学习笔记. Java线程池执行程序偏向于排队而不是产生新线程.从好的方面来说,我们有两种解决方法. 理想情况下,对任何线程池执行程序而言,期望如下: 预先创建了一组初始线程(核心线程 ...