// *********************************************************************** // Assembly : HZH_Controls // Created : 2019-10-09 // // *********************************************************************** // // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com // // // Blog: https://www.cnblogs.com/bfyx // GitHub:https://github.com/kwwwvagaa/NetWinformControl // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git // // If you use this code, please keep this note. // *********************************************************************** 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; namespace HZH_Controls.Controls { /// /// Class UCTimeLine. /// Implements the /// /// public partial class UCTimeLine : UserControl { /// /// The line color /// private Color lineColor = TextColors.Light; /// /// Gets or sets the color of the line. /// /// The color of the line. [Description("连接线颜色"), Category("自定义")] public Color LineColor { get { return lineColor; } set { lineColor = value; Invalidate(); } } /// /// The title font /// private Font titleFont = new Font("微软雅黑", 14f); /// /// Gets or sets the title font. /// /// The title font. [Description("标题字体"), Category("自定义")] public Font TitleFont { get { return titleFont; } set { titleFont = value; ReloadItems(); } } /// /// The title forcolor /// private Color titleForcolor = TextColors.MoreDark; /// /// Gets or sets the title forcolor. /// /// The title forcolor. [Description("标题颜色"), Category("自定义")] public Color TitleForcolor { get { return titleForcolor; } set { titleForcolor = value; ReloadItems(); } } /// /// The details font /// private Font detailsFont = new Font("微软雅黑", 10); /// /// Gets or sets the details font. /// /// The details font. [Description("详情字体"), Category("自定义")] public Font DetailsFont { get { return detailsFont; } set { detailsFont = value; ReloadItems(); } } /// /// The details forcolor /// private Color detailsForcolor = TextColors.Light; /// /// Gets or sets the details forcolor. /// /// The details forcolor. [Description("详情颜色"), Category("自定义")] public Color DetailsForcolor { get { return detailsForcolor; } set { detailsForcolor = value; ReloadItems(); } } /// /// The items /// TimeLineItem[] items; /// /// Gets or sets the items. /// /// The items. [Description("项列表"), Category("自定义")] public TimeLineItem[] Items { get { return items; } set { items = value; ReloadItems(); } } /// /// Initializes a new instance of the class. /// 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[0]; if (ControlHelper.IsDesignMode()) { items = new TimeLineItem[4]; for (int i = 0; i < 4; i++) { items[i] = new TimeLineItem() { Title = DateTime.Now.AddMonths(-1 * (3 - i)).ToString("yyyy年MM月"), Details = DateTime.Now.AddMonths(-1 * (3 - i)).ToString("yyyy年MM月") + "发生了一件大事,咔嚓一声打了一个炸雷,咔嚓一声打了一个炸雷,咔嚓一声打了一个炸雷,咔嚓一声打了一个炸雷,咔嚓一声打了一个炸雷,咔嚓一声打了一个炸雷,咔嚓一声打了一个炸雷,咔嚓一声打了一个炸雷,咔嚓一声打了一个炸雷,然后王二麻子他爹王咔嚓出生了。" }; } ReloadItems(); } } /// /// Reloads the items. /// 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(5); 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(5); 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); } } /// /// Handles the SizeChanged event of the item control. /// /// The source of the event. /// The instance containing the event data. void item_SizeChanged(object sender, EventArgs e) { Label lbl = (Label)sender; lbl.Parent.Height = lbl.Height + 10; } /// /// 引发 事件。 /// /// 包含事件数据的 。 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 = 0; i < lst.Count; i++) { //画圆 g.DrawEllipse(new Pen(new SolidBrush(lineColor)), new Rectangle(7, lst[i].Location.Y + 10, 16, 16)); //划线 if (i != lst.Count - 1) { g.DrawLine(new Pen(new SolidBrush(lineColor)), new Point(7 + 8, lst[i].Location.Y + 10 - 2), new Point(7 + 8, lst[i + 1].Location.Y + 10 + 16 + 2)); } } } } /// /// Class TimeLineItem. /// public class TimeLineItem { /// /// Gets or sets the title. /// /// The title. public string Title { get; set; } /// /// Gets or sets the details. /// /// The details. public string Details { get; set; } } }