C#Winform圆形窗体和圆形按钮改变button按钮的形状
圆形窗体
到窗体的Paint事件,写⼊以下代码:
private void form1_Paint(object sender, PaintEventArgs e)
{
#region 圆形窗体
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();//创建⼀条线 path.AddEllipse(0,0, Width, Height);//画⼀个椭圆(x,y,宽,⾼)
Graphics g =CreateGraphics();//为窗体创建画布
g.DrawEllipse(new Pen(Color.Black,2),1,1, Width -2, Height -2);//为画布画⼀个椭圆(笔,x,y,宽,⾼)
Region = new Region(path);//设置控件的窗⼝区域
#endregion
}
效果:
圆形按钮代码:
public class CircleButton : Button//继承按钮类重新⽣成解决⽅案就能看见我啦
{
protected override void OnPaint(PaintEventArgs e)//重新设置控件的形状 protected 保护 override重新{
base.OnPaint(e);//递归每次重新都发⽣此⽅法,保证其形状为⾃定义形状
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddEllipse(2,2, this.Width -6, this.Height -6);
Graphics g = e.Graphics;
g.DrawEllipse(new Pen(Color.Black,2),2,2, Width -6, Height -6);
Region = new Region(path);
}
}
重新⽣成解决⽅案后⼯具箱出现我们的⾃定义按钮
将他拖到界⾯上,效果:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论