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

hhc123 2010-01-13 01:16:39

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



}
}

...全文
79 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ysz89757 2010-01-13
  • 打赏
  • 举报
回复
收藏了,谢谢
ztenv 2010-01-13
  • 打赏
  • 举报
回复
做了一个可以根据图片形状生成相应形状的button,,,,,,自已没事儿瞎做的
卧_槽 2010-01-13
  • 打赏
  • 举报
回复
我发个自定义ImageButton好了。
详细过两天见我的博客
antony1029 2010-01-13
  • 打赏
  • 举报
回复
UP
wartim 2010-01-13
  • 打赏
  • 举报
回复

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧