winform之自定义控件

时间:2022-03-15 23:05:26

winform之自定义控件

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

 public class ProcessLabel : Control
{ public ProcessLabel()
{ //InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true); } private int distance = ; 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("新宋体", , FontStyle.Bold);
Rectangle rect = pe.ClipRectangle;
Graphics g = pe.Graphics;
float x = ;
float y = ;
float x1 = ;
float y1 = ;
float x2 = ;
float y2 = ;
int index = ;
if (Links.Count > )
{
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 > )
{
Pen blackPenLeft;
Pen blackPenRight;
if (!string.IsNullOrEmpty(kv.Value))
{
x1 = x - distance - sf.Width / - imagelist.Images[].Width / ;
y1 = sf.Height;
g.DrawImage(imagelist.Images[], x1, y1);
blackPenLeft = new Pen(Color.Orange, );
x2 = x1 - ;
y2 = y1 + imagelist.Images[].Height;
g.DrawString(kv.Value, enFont, new SolidBrush(Color.DodgerBlue), x2, y2);
}
else
{
x1 = x - distance - sf.Width / - imagelist.Images[].Width / ;
y1 = sf.Height;
g.DrawImage(imagelist.Images[], x1, y1);
blackPenLeft = new Pen(Color.Black, );
}
if (index + < Links.Count && !string.IsNullOrEmpty(Links[index + ].Value))
{
blackPenRight = new Pen(Color.Orange, );
}
else
{
blackPenRight = new Pen(Color.Black, );
}
if (index == )
{
g.DrawLine(blackPenRight, x1 + imagelist.Images[].Width, y1 + imagelist.Images[].Height / , x1 + imagelist.Images[].Width / + sf.Width / + distance / , y1 + imagelist.Images[].Height / );
}
else if (index == Links.Count - )
{
g.DrawLine(blackPenLeft, x1 - , y1 + imagelist.Images[].Height / , x1 - sf.Width / - distance / , y1 + imagelist.Images[].Height / );
}
else
{
g.DrawLine(blackPenRight, x1 + imagelist.Images[].Width, y1 + imagelist.Images[].Height / , x1 + imagelist.Images[].Width / + sf.Width / + distance / , y1 + imagelist.Images[].Height / );
g.DrawLine(blackPenLeft, x1 - , y1 + imagelist.Images[].Height / , x1 - sf.Width / - distance / , y1 + imagelist.Images[].Height / );
}
index++;
}
}
if (x - distance + < this.Parent.Width)
{
this.Width = this.Parent.Width;
}
else
{
this.Width = Convert.ToInt32(x - distance + );
}
} } }

使用

 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;