2014-02-10 29 views
0

我试着做在C#窗口形式的一些custombuttons,我已经有一个pieshaped按钮,这是源代码Pieshaped按钮形成C#

using System.Windows.Forms; 
    using System.Linq; 
    using System.Text; 
    using System.Drawing; 

    namespace WindowsFormsApplication1 
    { 
     public class RoundButton : Button 
     { 
      protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) 
      { 
       GraphicsPath grPath = new GraphicsPath(); 
       grPath.AddPie(new Rectangle(10, 10, 500, 500), 180, 18); 
       this.Region = new System.Drawing.Region(grPath); 
       base.OnPaint(e); 
      } 
     } 

    } 

的问题是位置和大小修复,但我希望他们能够变化,如果有人能帮助我,那将会很棒。

+0

当然,它只是将尺寸转换为RoundButton类中属性的一种情况,以便您可以将它们设置为其他内容? –

+0

是的,但我不知道如何去做它我想它像 PSEUDOCODE grPath.AddPie(新的矩形(PositionX,PositionY,SizeX,SizeY),StartingAngle,大小),它应该可以通过Mousedrag – user2518891

+1

OnPaint()应该**从不**包含导致生成另一个油漆的代码。你应该在构造函数中设置Region属性。或者在OnResize()中。当然,您可以通过使用新的矩形(Point.Empty,this.Size)来确定饼图的大小。 –

回答

0

而不是一个新的矩形对象,如何使用“e.ClipRectangle”?我试过它在我的结尾工作(有点),但不是很确定,如果这是你想要的。

此外,当鼠标进入/离开按钮时,按钮消失,所以我不得不重写上述事件。你也需要处理。

以下是代码。

public class RoundButton : Button 
{ 
    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) 
    { 
     var rect = e.ClipRectangle; 

     System.Drawing.Drawing2D.GraphicsPath grPath = new System.Drawing.Drawing2D.GraphicsPath(); 
     //grPath.AddPie(new Rectangle(1, 1, 10, 10), 180, 18); 
     grPath.AddPie(rect, 0, 360); 

     this.Region = new System.Drawing.Region(grPath); 
     base.OnPaint(e); 
    } 

    protected override void OnMouseEnter(EventArgs e) 
    { 
     //Need to handle this more elegantly 
     //base.OnMouseEnter(e); 
    } 

    protected override void OnMouseLeave(EventArgs e) 
    { 
     //Need to handle this more elegantly 
     //base.OnMouseLeave(e); 
    } 
} 

希望这会有所帮助。