发个自定义圆形Button(希望对新手有用)
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;
}
}
}
...全文
相关推荐
圆形Button(源码+Demo)
**文字对齐**:由于我们希望按钮的文字位于圆形内,因此需要调整文字的位置。可以使用`setGravity(Gravity.CENTER)`来让文字居中显示。5.
C#自定义控件:一个简单的圆形按钮源码
圆形按钮"是一种自定义控件,它的外观和行为与标准的Button控件不同,它不是矩形,而是呈现圆形的形状。这种设计可能适用于那些希望界面更具吸引力或需要独特交互方式的应用程序。
android相机圆形框拍照,自定义拍照界面
注意,由于我们希望保持圆形拍摄框,所以可能需要调整预览尺寸以适应圆形视图。4.
Android自定义圆形SeekBar
然而,系统默认的SeekBar是矩形的,有时为了追求更美观或者符合特定设计需求,开发者可能希望对其进行自定义,使其呈现出圆形的效果。
Android布局自定义Shap圆形ImageView可以单独设置背景与图片
实现这样的自定义控件,可以让应用界面更加丰富和个性化,提升用户体验。希望这篇文章能为正在学习Android开发的朋友们提供帮助。


