发个自定义圆形Button(希望对新手有用)

hhc123 hhc123 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Data;
namespace IPlayControl
{
public class RoundButton:System.Windows.Forms.Button
{
public bool hasoutline = false;
protected bool down = false;
GraphicsPath path=new GraphicsPath();
private Image graphics;
private String text;
private Font Font = new Font("Arial", 16);
StringFormat format = StringFormat.GenericDefault;
FontFamily family = new FontFamily("Arial");

/* public Font Font
{
get { return font; }
set { font = value; Invalidate(); }
}*/
public override String Text
{
get { return text; }
set { text = value; Invalidate(); }
}

protected virtual Color GetColor()
{
Color[] colors ={ Color.Silver , Color.Gray };
return colors[Convert.ToInt32(down)];
}
protected virtual Brush GetBrush(bool buttonState)
{
return new LinearGradientBrush(new Point(2,2),new Point(Width -1,Height-1),Color.White,GetColor());
}
private int GetPenWidth()
{
return 1 + Convert.ToInt32(down);
}
private Pen GetPen()
{
return new Pen(Brushes.Black,GetPenWidth());
}

public void DrawButton(Graphics graphics)
{
graphics.FillEllipse(GetBrush(down),0,0,Width,Height);
//graphics.DrawEllipse(GetPen(),0,0,Width,Height);
}
public void DrawText(GraphicsPath path)
{
path.AddString(Text,family ,(int)FontStyle.Regular,Bounds.Height,new Point(0,0),format );
// graphics.DrawString(text,Font,Brushes.SpringGreen,new PointF(Width,Height));

}
public void DrawResize(GraphicsPath graphicspath)
{
graphicspath.AddEllipse(0,0,Width,Height);

}
public void DrawButtonImage(Graphics graphics)
{

}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);

DrawButton(e.Graphics);
DrawGraphic(e.Graphics);
DrawText(path);

}
protected override void OnResize(EventArgs e)
{
path.AddEllipse(0,0,Bounds.Width,Bounds.Height);
Region = new Region(path);
Invalidate();
base.OnResize(e);
}

private void InitializeComponent()
{
this.SuspendLayout();
this.ResumeLayout(false);

}

protected override void OnMouseUp(MouseEventArgs mevent)
{

base.OnMouseUp(mevent);
down = false;
Invalidate();
}

protected override void OnMouseDown(MouseEventArgs mevent)
{
base.OnMouseDown(mevent);
down = true;
Invalidate();
}

protected virtual void DrawDownGraphic(Graphics graphics)
{
Matrix m = new Matrix();
m.Scale(1.03f,1.03f);
graphics.Transform = m;
}
protected virtual void DrawGraphic(Graphics graphics)
{
if (down)
DrawDownGraphic(graphics);
}
protected virtual Brush GraphicBrush()
{
return Enabled ? Brushes.Black : Brushes.Silver;
}



}
}

...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部
hhc123
C# 111,128 成员 642,536 内容