我有一个包含一个PictureBox和标签的用户控件。 控制上的不同事件加载在图片框三个不同的图像(例如前OnMouseEnter在,OnMouseLeave在)。 由于图像可以有不同的尺寸,我NEET来调整图片框和控制本身。 下面提供了控件的OnPaint事件,但这不起作用。
protected override void OnPaint(PaintEventArgs pe)
{
if (pictureBox.Image != null)
{
this.Width = this.pictureBox.Image.Size.Width;
this.Height = this.pictureBox.Image.Size.Height;
CutRoundedRectangle2(pictureBox, cornerRadius);
}
else
{
Bitmap DrawArea = new Bitmap(pictureBox.Size.Width, pictureBox.Size.Height);
Graphics g = Graphics.FromImage(DrawArea);
Pen mypen = new Pen(Color.Black);
pictureBox.Image = DrawArea;
System.Drawing.Pen pen = new Pen(new SolidBrush(this.ForeColor));
g.DrawRectangle(pen, 0, 0, this.Width-1, this.Height-1);
g.Dispose();
}
this.labelText.ocation = new Point((this.pictureBox.Width - this.labelText.Width) / 2,
(this.pictureBox.Height - this.labelText.Height) / 2);
base.OnPaint(pe);
}
PictureBox的SizeMode设置控制的constuctor:
this.pictureBox.SizeMode = PictureBoxSizeMode.AutoSize;