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;
时间: 2024-07-31 02:33:23

winform之自定义控件的相关文章

winform制作自定义控件(入门)

原文链接:http://blog.csdn.net/bychentufeiyang/article/details/7081402   与原文基本一致,只是例子变成VS2012环境,语言采用博主常用的VB.NET 一 .概述Windows 窗体控件是可再次使用的组件,它们封装了用户界面功能,并且可以用于客户端 Windows 应用程序.“Windows 窗体”不仅提供了许多现成控件,还提供了自行开发控件的基础结构.可以组合现有控件.扩展现有控件或创作自己的自定义控件.Windows 窗体控件是从

WinForm GDI+自定义控件总结(一)

前言 由于项目的原因好久没写博客了,也正是项目的原因开始系统的学习WinForm,从而接触到自定义控件的开发.自定义控件的开发有一定的难度,对开发者要求比较高,需要了解Windows运行的机制,熟悉win32Api和GDI+.下面是我收集的一些资料,挺不错的. 资料 .NET组件编程http://www.cnblogs.com/mapserver/category/57177.html .NET组件编程(1) 基础.NET组件编程(2) PropertyAttribute和EventAttrib

WinForm(C#)自定义控件之——RoundButton(圆形按钮)

最近需要做一个圆形的按钮,去CodeProject找了一下,发现有现成的可用,但不能完全满足我的需求.因此自己试着利用WinForm中的自定义组件功能,制作一个圆形按钮.圆形按钮小制作即将开始之前,先来看看最终效果(Demo程序下载链接:http://download.csdn.net/detail/keypig_zz/9440806) 下面分两步制作这个按钮. A. 目标 想了一下,即将制作的圆形按钮需要满足几个要求: i. 按钮呈现圆形或椭圆形,具体形状参数可调: ii. 按钮用不同的填充色

winform制作自定义控件

一 .概述Windows 窗体控件是可再次使用的组件,它们封装了用户界面功能,并且可以用于客户端 Windows 应用程序.“Windows 窗体”不仅提供了许多现成控件,还提供了自行开发控件的基础结构.可以组合现有控件.扩展现有控件或创作自己的自定义控件.Windows 窗体控件是从 System.Windows.Forms.Control 直接或间接派生的类.以下列表描述了开发 Windows 窗体控件的常见方案: ·  组合现有控件来创作一个复合控件. 复合控件封装有一个可以作为控件重复使

WinForm创建自定义控件

虽然VS为我们提供了很多控件可以使用,但有时候这些控件仍然不能满足我们的要求,比如我们要对部分控件进行一些个性化的定制,例如美化控件,这时候就需要自己绘制控件,或是在原有控件的基础上进行修改 自定义控件分为三种 1.组合控件(CompositeControls):在原有控件的基础上根据需要进行组合 2.扩展控件(ExtendedControls):继承自原有控件,添加一些新的属性和方法,绘制一些新元素 3.自定义控件(CustomControls):控件的绘制全部由用户定义 1.组合控件 新建项

winform窗体自定义控件

先上代码!!! 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Drawing; 5 using System.Data; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 using System.Reflection; 11 namespace

WinForm用户自定义控件,在主窗体加载时出现闪烁;调用用户控件出现闪烁,需要鼠标才能够显示

转载自:http://www.dotblogs.com.tw/rainmaker/archive/2012/02/22/69811.aspx 解决方案: 在调用用户控件的窗体里面添加一下代码: protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED return cp

在WPF中自定义控件

一, 不一定需要自定义控件在使用WPF以前,动辄使用自定义控件几乎成了惯性思维,比如需要一个带图片的按钮,但在WPF中此类任务却不需要如此大费周章,因为控件可以嵌套使用以及可以为控件外观打造一套新的样式就可以了.是否需要我们来自定义控件,这需要你考虑目前已有控件的真正逻辑功能而不要局限于外观,如果目前的控件都不能直觉地表达你的想法,那么你可以自己来打造一个控件,否则,也许我们仅仅改变一下目前控件的模板等就可以完成任务.很多人在自定义控件上经常犯的错误是:重复撰写已有的逻辑 二,UserContr

Java进击C#——应用开发之Asp.net

本章简言 上一章中笔者讲到关于Linq和EF的用法.并以hibernate来进行讲解.那么本章笔者来讲一下C#的Asp.Net.即是在B/S模式下开发.现在企业大部分的业务都是面向B/S模式的.所以对于Asp.Net的了解变得必不可少的知识点.笔者在从事JAVA开发的时候,很少看到有关于Awt和Swing开发的企业.更多是Servlet和JSP开发.这也是没有办法的事情.因为用Awt和Swing来开发软件不是说不能.只是怕吃力不讨好.笔者不是说JAVA不好.有一些方面JAVA的确存在不适合的情况