winform之自定义控件
2015-08-11 10:51
344 查看
这样的一个控件 肯定得通过自定义控件来实现了
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;
相关文章推荐
- CodeForces 344A Magnets
- MySQL出现同步延迟如何解决?优化?
- apache的扩展模块安装
- hdu 2795(单点修改)
- 检测CPU利用率的Shell脚本
- Num 31 : HDOJ : 1863 畅通工程 [ kruskal( 克鲁斯卡尔 )算法 ] [ 最小生成树 ]
- X的追求道路 SDUT 3027
- hdoj 1863 畅通工程 【最小生成树】
- 21-IO流-15-IO流(字符流-缓冲区-复制文本文件)
- 21-IO流-16-IO流(字符流-缓冲区-自定义MyBufferedReader-read方法)
- 本地推送的使用方法
- 手机端开发
- 浅析Android的横竖屏切换
- hdu 4109(拓扑排序 关键路径)
- SSL心跳漏洞的检测
- IOS开发 @property中assign、copy 、retain等关键字的理解
- 算法竞赛入门经典: 第三章 数组和字符串 3.3蛇形填数
- 21-IO流-14-IO流(字符流-缓冲区-BufferedReader-readLine方法原理)
- Oracle 12C -- Invisible Columns
- Error: L6218E: Undefined symbol main (referred from entry9a.o).