• winform之自定义控件


    这样的一个控件 肯定得通过自定义控件来实现了

     public class ProcessLabel : Control
        {
    
            public ProcessLabel()
            {
    
                //InitializeComponent();
                this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);
    
            }
    
            private int distance = 16;
    
            public int Distance
            {
                get { return distance; }
                set
                {
                    distance = value;
                    Invalidate();
                }
            }
    
            private ImageList imagelist = new ImageList();
    
            public ImageList ImageList
            {
                get { return imagelist; }
                set
                {
                    imagelist = value;
                    Invalidate();
                }
            }
    
            private List<KeyValuePair<string, string>> links = new List<KeyValuePair<string, string>>();
    
            public List<KeyValuePair<string, string>> Links
            {
                get { return links; }
                set
                {
                    links = value;
                    Invalidate();
                }
            }
    
            protected override void OnPaint(PaintEventArgs pe)
            {
                base.OnPaint(pe);
                Font enFont = new Font("新宋体", 10, FontStyle.Bold);
                Rectangle rect = pe.ClipRectangle;
                Graphics g = pe.Graphics;
                float x = 4;
                float y = 4;
                float x1 = 0;
                float y1 = 0;
                float x2 = 0;
                float y2 = 0;
                int index = 0;
                if (Links.Count > 0)
                {
                    foreach (KeyValuePair<string, string> kv in Links)
                    {
                        g.DrawString(kv.Key, enFont, new SolidBrush(Color.DodgerBlue), x, y);
                        SizeF sf = g.MeasureString(kv.Key, enFont);
                        x += (sf.Width + distance);
                        if (imagelist.Images.Count > 0)
                        {
                            Pen blackPenLeft;
                            Pen blackPenRight;
                            if (!string.IsNullOrEmpty(kv.Value))
                            {
                                x1 = x - distance - sf.Width / 2 - imagelist.Images[0].Width / 2;
                                y1 = sf.Height;
                                g.DrawImage(imagelist.Images[0], x1, y1);
                                blackPenLeft = new Pen(Color.Orange, 2);
                                x2 = x1 - 12;
                                y2 = y1 + imagelist.Images[0].Height;
                                g.DrawString(kv.Value, enFont, new SolidBrush(Color.DodgerBlue), x2, y2);
                            }
                            else
                            {
                                x1 = x - distance - sf.Width / 2 - imagelist.Images[1].Width / 2;
                                y1 = sf.Height;
                                g.DrawImage(imagelist.Images[1], x1, y1);
                                blackPenLeft = new Pen(Color.Black, 2);
                            }
                            if (index + 1 < Links.Count && !string.IsNullOrEmpty(Links[index + 1].Value))
                            {
                                blackPenRight = new Pen(Color.Orange, 2);
                            }
                            else
                            {
                                blackPenRight = new Pen(Color.Black, 2);
                            }
                            if (index == 0)
                            {
                                g.DrawLine(blackPenRight, x1 + imagelist.Images[0].Width, y1 + imagelist.Images[0].Height / 2, x1 + imagelist.Images[0].Width / 2 + sf.Width / 2 + distance / 2, y1 + imagelist.Images[0].Height / 2);
                            }
                            else if (index == Links.Count - 1)
                            {
                                g.DrawLine(blackPenLeft, x1 - 2, y1 + imagelist.Images[0].Height / 2, x1 - sf.Width / 2 - distance / 2, y1 + imagelist.Images[0].Height / 2);
                            }
                            else
                            {
                                g.DrawLine(blackPenRight, x1 + imagelist.Images[0].Width, y1 + imagelist.Images[0].Height / 2, x1 + imagelist.Images[0].Width / 2 + sf.Width / 2 + distance / 2, y1 + imagelist.Images[0].Height / 2);
                                g.DrawLine(blackPenLeft, x1 - 2, y1 + imagelist.Images[0].Height / 2, x1 - sf.Width / 2 - distance / 2, y1 + imagelist.Images[0].Height / 2);
                            }
                            index++;
                        }
                    }
                    if (x - distance + 4 < this.Parent.Width)
                    {
                        this.Width = this.Parent.Width;
                    }
                    else
                    {
                        this.Width = Convert.ToInt32(x - distance + 8);
                    }
                }
    
            }
    
        }

    使用

     ImageList myImageList = new ImageList();
                string filePath1 = Application.StartupPath + "\Images\orangelink.png";
                string filePath2 = Application.StartupPath + "\Images\blacklink.png";
                myImageList.Images.Add(Image.FromFile(filePath1));
                myImageList.Images.Add(Image.FromFile(filePath2));
                processLabel1.ImageList = myImageList;
    
                List<KeyValuePair<string, string>> source = new List<KeyValuePair<string, string>>();
                source.Add(new KeyValuePair<string,string>("下单","08:20"));
                source.Add(new KeyValuePair<string,string>("接单","14:20"));
                source.Add(new KeyValuePair<string,string>("配送","14:20"));
                source.Add(new KeyValuePair<string, string>("收货", "15:20"));
                source.Add(new KeyValuePair<string, string>("退货", "17:20"));
                source.Add(new KeyValuePair<string, string>("退款", "19:20"));
                source.Add(new KeyValuePair<string, string>("完毕", "21:20"));
              
                processLabel1.Links = source;
  • 相关阅读:
    Python2 和 Python3的区别 更新中
    CentOS下图形界面安装_Orcaale 11g
    Nmap_使用介绍
    shell_innobackup增量备份步骤
    shell_跳板机推送公钥
    shell_clean_log
    shell_xtrabackup_backup_mysql
    gitlab免密登录
    gitlab安装与部署
    git合并分支
  • 原文地址:https://www.cnblogs.com/njcxwz/p/4720379.html
Copyright © 2020-2023  润新知