0

Here is the class that I Use and my declaration for initialization my problem is that i already made a button on my design. So what will i revise in my code so that, the class that i made will apply to the current button

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);
        }
    }
}
Gab
  • 35
  • 7

1 Answers1

0

The simplest way would be to create a UserControl and put the RoundButton class there, then the designer would allow you to add one to the form in the usual way.

Otherwise you will need to double click the form in the designer so that it adds:

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

to your code. Then you can add:

    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
    }

As it is, the button will be at the top left of its container (the form in this case), so you will need to move it to somewhere sensible:

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

There are any number of things that you can set - see:

https://learn.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

Edit:

I assume the exisiting button is just a standard Button, so you will need to delete that one if you aren't going to use it.

Kevin Ford
  • 260
  • 1
  • 7