C# Windows窗体C中的图形按钮#
我正在尝试用c#为windows窗体制作一些自定义按钮,我已经有了一个圆形按钮。这是源代码C# Windows窗体C中的图形按钮#,c#,winforms,button,C#,Winforms,Button,我正在尝试用c#为windows窗体制作一些自定义按钮,我已经有了一个圆形按钮。这是源代码 using System.Windows.Forms; using System.Linq; using System.Text; using System.Drawing; namespace WindowsFormsApplication1 { public class RoundButton : Button {
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);
}
}
}
问题是位置和大小是固定的,但我希望它们是可变的,如果有人能帮助我,那就太好了。用“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);
}
}
希望这有帮助。当然,这只是一个将维度转换为RoundButton类上的属性的例子,以便您可以将它们设置为其他属性?是的,但我不知道如何做,我希望它像伪代码grPath.AddPie(新矩形(PositionX、PositionY、SizeX、SizeY)、StartingAngle、Size)它应该可以通过MousedragOnPaint()进行更改。它不应该包含导致生成另一个绘制的代码。您应该在构造函数中设置Region属性。或者在OnResize()中。当然,这为您提供了一个很好的方法来计算饼图的大小,只需使用新的矩形(Point.Empty,this.size)。