2017-09-24 64 views
0

下面是我使用的类和我的初始化问题的声明,我已经在我的设计上创建了一个按钮。那么,我将在我的代码修改,这样,我所做的类将应用到当前的按钮在当前按钮上应用RoundButton

public Form1() 
{ 
    InitializeComponent(); 
    Application.DoEvents(); 
    RoundButton _btn = new RoundButton(); 
    EventHandler myHandler = new EventHandler(_btnLog_Click); 
    _btnLog = _btn; 
} 

class RoundButton:Button 
{ 
    GraphicsPath GetRoundPath(RectangleF _rect, int radius) 
    { 
     float r2 = radius/2f; 
     GraphicsPath _gp = new GraphicsPath(); 

     _gp.AddArc(_rect.X, _rect.Y, radius, radius, 180, 90); 
     _gp.AddLine(_rect.X + r2, _rect.Y, _rect.Width - r2, _rect.Y); 
     _gp.AddArc(_rect.X + _rect.Width - radius, _rect.Y, radius, radius, 270, 290); 
     _gp.AddLine(_rect.Width, _rect.Y + r2, _rect.Width, _rect.Height - r2); 
     _gp.AddArc(_rect.X + _rect.Width - radius, 
      _rect.Y + _rect.Height - radius, radius, radius, 0, 90); 
     _gp.AddLine(_rect.Width - r2, _rect.Height, _rect.X + r2, _rect.Height); 
     _gp.AddArc(_rect.X, _rect.Y + _rect.Height - radius, radius, radius, 90, 90); 
     _gp.AddLine(_rect.X, _rect.Height - r2, _rect.X, _rect.Y + r2); 

     _gp.CloseFigure(); 
     return _gp; 
    } 
    protected override void OnPaint(PaintEventArgs e) 
    { 
     base.OnPaint(e); 
     RectangleF _rect = new RectangleF(0, 0, this.Width, this.Height); 
     GraphicsPath _gp = GetRoundPath(_rect, 1); 

     this.Region = new Region(_gp); 
     using (Pen _pen = new Pen(Color.CadetBlue, 1.75f)) 
     { 
      _pen.Alignment = PenAlignment.Inset; 
      e.Graphics.DrawPath(_pen, _gp); 
     } 
    } 
} 
+0

[按钮C#(WinForms)中的圆形边缘]的可能的重复(https://stackoverflow.com/questions/28486521/rounded-edges-in-button-c-sharp-winforms) – Plutonix

+0

没有回答我的问题问题先生 – Gab

+0

如果您通过设计人员添加了“RoundButton”,则在第一个片段中创建的“RoundButton”是不同的。那一个永远不会添加tot他控制集合,所以它不清楚你在 – Plutonix

回答

0

最简单的方法是创建一个用户控件,并把RoundButton的班上有,那么设计师将允许你以通常的方式添加一个到表单。

否则,你将需要双击在设计形式,以便它补充说:

private void Form1_Load(object sender, EventArgs e) 
    { 
    } 

你的代码。然后,你可以添加:

private void Form1_Load(object sender, EventArgs e) 
    { 
     RoundButton _btn = new RoundButton(); 
     _btn.Text = "Log"; 
     _btn.Click += new EventHandler(_btnLog_Click); 
     this.Controls.Add(_btn); 
    } 

    void _btnLog_Click(object sender, EventArgs e) 
    { 
     // do somethng here 
    } 

正因为如此,该按钮将在其容器(在这种情况下的形式)的左上角,所以你需要将它移动到某处明智的:

 _btn.Location = new Point(30, 50); 

有任何数量的东西,你可以在设置 - 看到:

https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/button-control-windows-forms

https://msdn.microsoft.com/en-us/library/system.windows.forms.button(v=vs.110).aspx

编辑:

我假设现有的按钮只是一个标准的按钮,所以如果你不打算使用它,你将需要删除那个按钮。

相关问题