繁体   English   中英

c#在子PictureBox更改Image时调整父控件的大小

[英]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'并将AutoSizeModeGrowAndShrink并在将新图像加载到图片框时调用父控件的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM