繁体   English   中英

Windows窗体C#中的Pieshaped按钮

[英]Pieshaped Buttons in Windows forms C#

我正在尝试在C#中为Windows窗体制作一些custombutton,我已经有一个饼形按钮,这是源代码

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

    }

问题是位置和大小是固定的,但我希望它们是可变的,如果有人可以帮助我,那就太好了。

代替新的Rectangle对象,如何使用“ 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);
    }
}

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM