在当前按钮上应用RoundButton

时间:2017-09-24 16:18:59

标签: c# winforms

这是我使用的类和我的初始化声明我的问题是我已经在我的设计上做了一个按钮。那么我将在我的代码中进行修改,以便我所创建的类将应用于当前按钮

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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

最简单的方法是创建一个UserControl并将RoundButton类放在那里,然后设计师允许你以通常的方式在表单中添加一个。

否则,您需要双击设计器中的表单,以便添加:

 public virtual async Task<ActionResult> Deposit(decimal money, string customerId)
{
     var customer = await _userManager.FindByIdAsync(customerId);
    customer.Fund+= money;  // a few customers does not get the money !!
    await _userManager.UpdateAsync(customer);  // asp.net identity 2.0 usermanager
    await UpdateOrderMoneyCollect(customer, money, debt);
}

代码。然后你可以添加:

    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
    }

您可以设置任意数量的内容 - 请参阅:

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

编辑:

我认为exisiting按钮只是一个标准按钮,所以如果你不打算使用它,你需要删除它。