1.首先添加一个类库项目,项目里的类继承于Panel控件。
2.构造函数里添加
public MyPanel()
{
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;
}
3.使用时 在Paint事件里添加
private void myPanel1_Paint(object sender, PaintEventArgs e)
{
ControlPaint.DrawBorder(e.Graphics, myPanel1.ClientRectangle, Color.Blue, 3,
ButtonBorderStyle.Solid, Color.Blue, 3,
ButtonBorderStyle.Solid, Color.Blue, 3,
ButtonBorderStyle.Solid, Color.Blue, 3,
ButtonBorderStyle.Solid);
}
测试代码:
Image img = Image.FromFile(@"C:/Documents and Settings/All Users/Documents/My Pictures/示例图片/Blue hills.jpg");
this.pictureBox1.Image = img;
this.myPanel1.Parent = this.pictureBox1;
OK,到这里.这个扩展的控件就已经完成了。