[英]c# Resize parent control when a child pictureBox changes the Image
我有一個包含圖片框和標簽的UserControl。 控件在不同事件(例如onMouseEnter,OnMouseLeave)中在PictureBox中加載三個不同的圖像。 由於圖像可以具有不同的大小,因此我無需調整pictureBox和控件本身的大小。 下面提供了控件的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在控件的構造函數中設置:
this.pictureBox.SizeMode = PictureBoxSizeMode.AutoSize;
很久以前,我上次使用WinForms時,但是...
我的第一個想法是:您是否曾嘗試將父控件的AutoSize
屬性的值設置為'true'並將AutoSizeMode
為GrowAndShrink
並在將新圖像加載到圖片框時調用父控件的Refresh()
方法?
@ Alexey,pictureBox調整大小事件很有幫助!
private void pictureBox_Resize(object sender, EventArgs e)
{
this.Width = this.pictureBox.Image.Size.Width;
this.Height = this.pictureBox.Image.Size.Height;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.