调整用户控件的大小



Im正在尝试创建一个圆角矩形,但决定从更简单的东西开始,在这种情况下是一个椭圆。不幸的是,当我将自定义控件拖动到Fprm1.cs[design]上并尝试调整其大小时,实际的椭圆没有发生任何变化。只有当我进入用户控件[设计]并调整其大小时,它才会改变。如果有人能指出我哪里出了问题,我将不胜感激。谢谢

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace CustomPbar
{
    public partial class Pbar : UserControl
    {
        GraphicsPath path = new GraphicsPath();
        public Pbar()
        {
            InitializeComponent();
            path.AddEllipse(0, 0, this.ClientSize.Width, this.ClientSize.Height);
            this.Region = new Region(path);
            this.BackColor = SystemColors.ControlDarkDark;
        }
        private void MyForm_Layout(object sender, System.Windows.Forms.LayoutEventArgs e)
        {
            if (this.Region != null)
            {
                this.Region.Dispose();
                this.Region = null;
            }
            path.AddEllipse(0, 0, this.Width, this.Height);
            this.Region = new Region(path);
        }
    }
}

尝试从Resize事件调用它:

protected override void OnResize(EventArgs e) {
  using (GraphicsPath gp = new GraphicsPath()) {
    gp.AddEllipse(this.ClientRectangle);
    this.Region = new Region(gp);
  }
  base.OnResize(e);
}

最新更新