调整用户控件的大小

时间:2012-03-13 17:13:25

标签: c++ winforms c#-4.0

我正在尝试创建一个圆角矩形,但决定从更简单的东西开始,在这种情况下是一个椭圆。不幸的是,当我将自定义控件拖到Fprm1.cs [design]上并尝试调整它时,实际的椭圆没有任何反应。只有当我进入usercontrol [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);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

尝试从Resize事件中调用它:

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

  base.OnResize(e);
}
相关问题