【问题标题】:c# Resize parent control when a child pictureBox changes the Imagec#当子pictureBox改变Image时调整父控件大小
【发布时间】:2012-07-16 16:19:36
【问题描述】:

我有一个包含图片框和标签的用户控件。控件根据不同的事件(例如 onMouseEnter、OnMouseLeave)在 ​​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;

【问题讨论】:

    标签: c# user-controls picturebox autoresize


    【解决方案1】:

    我上次使用 WinForms 是很久以前的事了,但是 ...
    我的第一个想法是:您是否尝试过将父控件的AutoSize 属性值设置为'true',将AutoSizeMode 设置为GrowAndShrink,并在新图像加载到图片框时调用父控件的Refresh() 方法?

    【讨论】:

      【解决方案2】:

      @Alexey,pictureBox 调整大小事件有帮助!

          private void pictureBox_Resize(object sender, EventArgs e)
          {
              this.Width = this.pictureBox.Image.Size.Width;
              this.Height = this.pictureBox.Image.Size.Height;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-29
        • 1970-01-01
        • 2023-04-10
        • 1970-01-01
        相关资源
        最近更新 更多