【问题标题】:Applying RoundButton on a current button在当前按钮上应用 RoundButton
【发布时间】:2017-09-24 16:18:59
【问题描述】:

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

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

【问题讨论】:

  • 先生没有回答我的问题
  • 如果您通过设计器添加了RoundButton,那么在第一个sn-p 中创建的RoundButton 是不同的。那个永远不会添加到他控制收藏中,所以不清楚你在玩什么
  • 所以我应该摆脱? RoundButton 类?
  • 无论如何它都不会正常工作,因为按钮和事件是表单加载方法的本地,所以它们可能在返回后随时消失。

标签: c# winforms


【解决方案1】:

最简单的方法是创建一个 UserControl 并将 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

编辑:

我假设现有按钮只是一个标准按钮,因此如果您不打算使用它,则需要删除它。

【讨论】:

    猜你喜欢
    • 2013-03-15
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多