您的位置:首页 > 其它

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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: