2012-03-13 54 views
1

在试图创建一个圆角矩形的过程中,但决定从一些简单的事情开始,在这种情况下是一个椭圆。不幸的是,当我将自定义控件拖放到Fprm1.cs [设计]上并尝试调整其大小时,实际椭圆没有任何反应。只有当我进入usercontrol [设计]并调整它的大小时,它才会改变。如果有人能指出我出错的地方,我将不胜感激。谢谢。调整用户控件的大小

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

尝试从Resize事件调用它:

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

    base.OnResize(e); 
} 
+0

非常感谢。 – 2012-03-13 17:30:48