推荐

官网

http://www.hzhcontrols.com/

NetWinform自定义控件

English README.md(github)

English README.md(gitee)

根据该控件库重写 https://www.cnblogs.com/bfyx/p/11641874.html

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace 命名空间名称
{
   /// <summary>
    /// Class UCTimeLine.
    /// Implements the <see cref="System.Windows.Forms.UserControl" />
    /// </summary>
    /// <seealso cref="System.Windows.Forms.UserControl" />
    public partial class UCTimeLine : UserControl
    {
        /// <summary>
        /// The line color
        /// </summary>
        private Color lineColor = ColorTranslator.FromHtml("#909399");

        /// <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 = ColorTranslator.FromHtml("#303133");

        /// <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("微软雅黑", 10);

        /// <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 = ColorTranslator.FromHtml("#909399");

        /// <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 static bool IsDesignMode()
        {
            bool returnFlag = false;

            if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
            {
                returnFlag = true;
            }
            else if (Process.GetCurrentProcess().ProcessName == "devenv")
            {
                returnFlag = true;
            }

            return returnFlag;
        }

        #region 冻结控件
        /// <summary>
        /// The m LST freeze control
        /// </summary>
        static Dictionary<Control, bool> m_lstFreezeControl = new Dictionary<Control, bool>();
        /// <summary>
        /// 功能描述:停止更新控件
        /// 作  者:HZH
        /// 创建日期:2019-07-13 11:11:32
        /// 任务编号:POS
        /// </summary>
        /// <param name="control">control</param>
        /// <param name="blnToFreeze">是否停止更新</param>
        public static void FreezeControl(Control control, bool blnToFreeze)
        {
            if (blnToFreeze && control.IsHandleCreated && control.Visible && !control.IsDisposed && (!m_lstFreezeControl.ContainsKey(control) || (m_lstFreezeControl.ContainsKey(control) && m_lstFreezeControl[control] == false)))
            {
                m_lstFreezeControl[control] = true;
                control.Disposed += control_Disposed;
                SendMessage(control.Handle, 11, 0, 0);
            }
            else if (!blnToFreeze && !control.IsDisposed && m_lstFreezeControl.ContainsKey(control) && m_lstFreezeControl[control] == true)
            {
                m_lstFreezeControl.Remove(control);
                SendMessage(control.Handle, 11, 1, 0);
                control.Invalidate(true);
            }
        }

        /// <summary>
        /// Sends the message.
        /// </summary>
        /// <param name="hWnd">The h WND.</param>
        /// <param name="msg">The MSG.</param>
        /// <param name="wParam">The w parameter.</param>
        /// <param name="lParam">The l parameter.</param>
        [DllImport("user32.dll")]
        public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

        /// <summary>
        /// Handles the Disposed event of the control control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        static void control_Disposed(object sender, EventArgs e)
        {
            try
            {
                if (m_lstFreezeControl.ContainsKey((Control)sender))
                    m_lstFreezeControl.Remove((Control)sender);
            }
            catch { }
        }
        #endregion 冻结控件

        /// <summary>
        /// Initializes a new instance of the <see cref="UCTimeLine"/> class.
        /// </summary>
        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 (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();
            }
        }

        /// <summary>
        /// Reloads the items.
        /// </summary>
        private void ReloadItems()
        {
            try
            {
                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
            {
                FreezeControl(this, false);
            }
        }

        /// <summary>
        /// Handles the SizeChanged event of the item control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        void item_SizeChanged(object sender, EventArgs e)
        {
            Label lbl = (Label)sender;
            lbl.Parent.Height = lbl.Height + 10;
        }

        /// <summary>
        /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
        /// </summary>
        /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            var g = e.Graphics;
            SetGDIHigh(g);
            var lst = ControlsToArray(Controls).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));
                }
            }
        }

        /// <summary>
        /// 设置GDI高质量模式抗锯齿
        /// </summary>
        /// <param name="g">The g.</param>
        public static void SetGDIHigh(Graphics g)
        {
            g.SmoothingMode = SmoothingMode.AntiAlias;  //使绘图质量最高,即消除锯齿
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = CompositingQuality.HighQuality;
        }
        /// <summary>
        /// Converts to array.
        /// </summary>
        /// <param name="controls">The controls.</param>
        /// <returns>Control[].</returns>
        public static Control[] ControlsToArray(System.Windows.Forms.Control.ControlCollection controls)
        {
            if (controls == null || controls.Count <= 0)
                return new Control[0];
            List<Control> lst = new List<Control>();
            foreach (Control item in controls)
            {
                lst.Add(item);
            }
            return lst.ToArray();
        }
    }

    /// <summary>
    /// Class TimeLineItem.
    /// </summary>
    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; }
    }
}

C# Winform 自定义 时间线 控件的更多相关文章

  1. C# WinForm自定义通用分页控件

    大家好,前几天因工作需要要开发一个基于WinForm的小程序.其中要用到分页,最开始的想法找个第三方的dll用一下,但是后来想了想觉得不如自己写一个玩一下 之前的web开发中有各式各样的列表组件基本都 ...

  2. C# Winform 通过FlowLayoutPanel及自定义的编辑控件,实现快速构建C/S版的编辑表单页面

    个人理解,开发应用程序的目的,不论是B/S或是C/S结构类型,无非就是实现可供用户进行查.增.改.删,其中查询用到最多,开发设计的场景也最为复杂,包括但不限于:表格记录查询.报表查询.导出文件查询等等 ...

  3. Winform 通过FlowLayoutPanel及自定义的编辑控件,实现快速构建C/S版的编辑表单页面 z

    http://www.cnblogs.com/zuowj/p/4504130.html 不论是B/S或是C/S结构类型,无非就是实现可供用户进行查.增.改.删,其中查询用到最多,开发设计的场景 也最为 ...

  4. (转)sl简单自定义win窗体控件

    sl简单自定义win窗体控件      相信大家接触过不少win窗体控件ChildWin子窗口就的sl自带的一个  而且网上也有很多类似的控件,而今天我和大家分享下自己制作个win窗体控件,希望对初学 ...

  5. 在 Visual C++ 中开发自定义的绘图控件

    本文讨论的重点介于两者 之间 — 公共控件赋予您想要的大部分功能,但控件的外观并不是您想要的.例如,列表视图控件提供在许多视图风格中显示数据列表的方式 — 小图标.大图标.列表和详细列表(报告).然而 ...

  6. C# winform项目中ListView控件使用CheckBoxes属性实现单选功能

    C# winform项目中ListView控件使用CheckBoxes属性实现单选功能 在做项目时需要使用ListView控件的CheckBoxes属性显示,还要在点击行时自动选中CheckBoxes ...

  7. Winform跨窗体操作控件(使用委托)

    Winform跨窗体操作控件是winform开发中很常见的形式,最常见且简单有效的方式便是使用委托的方式来进行操作,下面我将通过一个小实例来说明如何使用委托跨窗体实现控件操作. 实例介绍:两个窗体,F ...

  8. Angular19 自定义表单控件

    1 需求 当开发者需要一个特定的表单控件时就需要自己开发一个和默认提供的表单控件用法相似的控件来作为表单控件:自定义的表单控件必须考虑模型和视图之间的数据怎么进行交互 2 官方文档 -> 点击前 ...

  9. C#在WinForm中重写ProgressBar控件(带%的显示)

    废话少说,直接上码: namespace csPublish { [ToolboxItem(true)] class textProgressBar : System.Windows.Forms.Pr ...

  10. winform 自定义控件:半透明Loading控件

    winform  自定义控件:半透明Loading控件 by wgscd date:2015-05-05 效果: using System;using System.Drawing;using Sys ...

随机推荐

  1. 移除任务栏右端"显示桌面"按钮-AutoIt

    核心代码 $hwnd = WinGetHandle("[CLASS:Shell_TrayWnd]", "") ControlHide($hwnd, " ...

  2. Eclipse 中 JAVA AWT相关包不提示问题(解决)

    原因: 由于在2021年7月15日 OpenJDK管理委员会全票通过批准成立由Phil Race担任初始负责人的 Client Libraries Group(客户端类库工作组). 新的工作组将继续赞 ...

  3. Visual Studio 2022 v17.13新版发布:强化稳定性和安全,助力 .NET 开发提效!

    前言 今天大姚带领大家一起来看看 Visual Studio 2022 v17.13 新版发布都更新了哪些新功能,为我们开发工作带来了哪些便利,是否真的值得我们花费时间把 Visual Studio ...

  4. 为什么不推荐在 MySQL 中直接存储图片、音频、视频等大容量内容?

    在MySQL中直接存储图片.音频.视频等大容量内容(通常称为BLOB数据)通常不被推荐,主要原因包括以下几点: 1. 性能问题 存储效率:存储大容量文件(如图片.音频.视频等)会大幅增加数据库的存储负 ...

  5. Java 的 G1 垃圾回收流程

    Java 的 G1 垃圾回收流程 G1(Garbage-First)垃圾收集器 是一种区域化.并发.低延迟的垃圾回收器,适合大堆内存和对暂停时间有严格要求的应用程序.G1 的垃圾回收流程主要包括以下阶 ...

  6. 从零实现富文本编辑器#3-基于Delta的线性数据结构模型

    数据模型的设计是编辑器的核心基础,其直接影响了选区模型.DOM模型.状态管理等模块的设计.例如在quill中的选区模型是index + len的表达,而slate中则是anchor + focus的表 ...

  7. ArcGIS Desktop 10.7 完美汉化安装教程

    1,下载文件并解压缩,双击[Esri ArcGIS Desktop v10.7.0.exe] 2.在安装向导中选择[Next] 3.选中[I accept the master agreement]我 ...

  8. 【工具】VScode|Linux下 VScode 调试 Python 项目、模块、包的方法

    使用过 Anaconda.Jupyter.Pycharm.VScode.VS2022.pdb 这几个 IDE 去编写 python 项目或者维护 python 环境,各有各的优缺点,但 VScode ...

  9. vue3 基础-non-props 特性

    本篇探讨当父组件通过属性给子组件传数据时, 子组件如果不通过 props 属性进行接收, 那数据会挂载到哪里, 以及子组件如何能使用这些数据. 正常的父子组件传值 <!DOCTYPE html& ...

  10. 手把手教你使用C#创建一个WebSearchAgent

    PocketFlowSharp介绍 最近我对PocketFlow比较感兴趣,不仅是因为它是一个极简的LLM框架,更加让我觉得很不错的地方在于作者提供了很多方便学习的例子,就算没有LLM应用开发经验,也 ...