using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace WindowsControlLibrary1
{
public partial class transparentButton : UserControl
{
public transparentButton()
{
InitializeComponent();
}
#region 公共字段
public static SmoothingMode sMode;
public static bool btClick = true;
public static int pub_degree = 20;//四个角弧度的大小范围
#endregion
#region 属性
private int TcornerDegree = 2;
[Browsable(true), Category("按钮的属性设置"), Description("按钮四个角的弧度")]
public int cornerDegree
{
get { return TcornerDegree; }
set
{
TcornerDegree = value;
if (this.Width > this.Height)
pub_degree = (int)(this.Height / 2);
else
pub_degree = (int)(this.Width) / 2;
if (TcornerDegree <= 0)
TcornerDegree = 1;
if (TcornerDegree > pub_degree)
TcornerDegree = pub_degree;
if (TcornerDegree > 0)
this.Invalidate();
}
}
private Color TshineColor = Color.Red;
[Browsable(true), Category("按钮的属性设置"), Description("按钮的光泽度颜色")]
public Color shineColor
{
get { return TshineColor; }
set
{
TshineColor = value;
this.Invalidate();
}
}
private int TtranparentDegree = 2;
[Browsable(true), Category("按钮的属性设置"), Description("按钮的透明度数")]
public int transparentDegree
{
get { return TtranparentDegree; }
set
{
TtranparentDegree = value;
if (TtranparentDegree > 20)
TtranparentDegree = 20;
if (TtranparentDegree < 0)
TtranparentDegree = 0;
if (TtranparentDegree > 0)
this.Invalidate();
}
}
private string TbtText = "Text";
[Browsable(true), Category("按钮的属性设置"), Description("设置显示的文本")]
public string btText
{
get { return TbtText; }
set
{
TbtText = value;
if (TbtText.Length > 0)
this.Invalidate();
}
}
#endregion
#region 事件
protected override void OnPaint( PaintEventArgs e)
{
transparentButton_Paint( this , e);
}
private void transparentButton_Paint(object sender, PaintEventArgs e)
{
this.BackColor = Color.Transparent;
sMode = e.Graphics.SmoothingMode;
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
if (this.transparentDegree == 0)
{
backgroundColorClicked(rect, e.Graphics);
backGroundColorUnclicked(rect, e.Graphics, this.shineColor);
}
else
{
if (this.transparentDegree > 0)
{
backgroundColorClicked(rect, e.Graphics);
for (int i = 0; i < transparentDegree; i++)
{
backGroundColorUnclicked(rect, e.Graphics, this.shineColor);
}
}
}
if (btClick == false)
backgroundColorClicked(rect, e.Graphics);
if (this.btText.Length > 0)
{
drawText(e.Graphics);
}
}
private void transparentButton_SizeChanged(object sender, EventArgs e)
{
this.Invalidate();
}
private void transparentButton_MouseDown(object sender, EventArgs e)
{
btClick = false;
this.Invalidate();
}
private void transparentButton_MouseUp(object sender, EventArgs e)
{
btClick = true;
this.Invalidate();
}
#endregion
#region 自定义方法
//未按下时按钮的背景色
private void backGroundColorUnclicked(Rectangle rect, Graphics g, Color fillColor)
{
using (GraphicsPath path = createCircleCornerRectangle(rect, this.cornerDegree))
{
int opacity = 255;
opacity = (int)(0.4f * opacity + 0.5f);
using (LinearGradientBrush brush = new LinearGradientBrush(rect, Color.FromArgb(opacity / 5, fillColor), Color.FromArgb(opacity, fillColor), LinearGradientMode.Vertical))
{
g.FillPath(brush, path);
}
g.SmoothingMode = sMode;
}
}
//按钮按下时背景色
private void backgroundColorClicked(Rectangle rect, Graphics g)
{
int opacity = 255;
Color tempColor = Color.Green;
if (btClick == true)
{
opacity = 125;
tempColor = Color.Blue;
}
using (GraphicsPath path = createCircleCornerRectangle(rect, this.cornerDegree))
{
using (LinearGradientBrush brush = new LinearGradientBrush(rect, Color.FromArgb(opacity / 5, tempColor), Color.FromArgb(opacity, tempColor), LinearGradientMode.Vertical))
{
g.FillPath(brush, path);
}
g.SmoothingMode = sMode;
}
}
private void drawText(Graphics g)
{
Graphics txtG = CreateGraphics();
string text = this.btText;
SizeF size = txtG.MeasureString(text, this.Font);
float txtWidth = size.Width;
float txtHeight = size.Height;
float x = 0;
float y = 0;
if (this.Height > txtHeight)
y = (this.Height - txtHeight) / 2;
else
y = this.cornerDegree;
if (this.Width > txtWidth)
x = (this.Width - txtWidth) / 2;
else
x = this.cornerDegree;
Rectangle rect = new Rectangle((int)Math.Floor(x), (int)Math.Floor(y), (int)Math.Ceiling(txtWidth), (int)Math.Ceiling(txtHeight));
int opacity = 255;
opacity = (int)(0.4f * opacity + 0.5f);
using (LinearGradientBrush brush = new LinearGradientBrush(rect, Color.FromArgb(opacity / 5, Color.Black), Color.FromArgb(opacity, Color.Black), LinearGradientMode.Vertical))
{
g.DrawString(text, this.Font, brush, new PointF(x, y));
}
}
//创建圆角矩形,radius是圆角的度数
private static GraphicsPath createCircleCornerRectangle(Rectangle rect, int radius)
{
GraphicsPath path = new GraphicsPath();
int l = rect.Left;
int t = rect.Top;
int w = rect.Width;
int h = rect.Height;
path.AddArc(l, t, 2 * radius, 2 * radius, 0, 0);
path.AddLine(l, t, l + w, t);
path.AddArc(l + w, t, 2 * radius, 2 * radius, 0, 0);
path.AddLine(l + w, t + radius, l + w, t + h - radius);
path.AddArc(l + w - 2 * radius, t + h - 2 * radius, 2 * radius, 2 * radius, 0, 90);
path.AddLine(l + radius, t + h, l + w - radius, t + h);
path.AddArc(l, t + h - 2 * radius, 2 * radius, 2 * radius, 90, 90);
path.AddLine(l, t + radius, l, t + h - radius);
return path;
}
#endregion
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace WindowsControlLibrary1
{
public partial class transparentButton : UserControl
{
public transparentButton()
{
InitializeComponent();
}
#region 公共字段
public static SmoothingMode sMode;
public static bool btClick = true;
public static int pub_degree = 20;//四个角弧度的大小范围
#endregion
#region 属性
private int TcornerDegree = 2;
[Browsable(true), Category("按钮的属性设置"), Description("按钮四个角的弧度")]
public int cornerDegree
{
get { return TcornerDegree; }
set
{
TcornerDegree = value;
if (this.Width > this.Height)
pub_degree = (int)(this.Height / 2);
else
pub_degree = (int)(this.Width) / 2;
if (TcornerDegree <= 0)
TcornerDegree = 1;
if (TcornerDegree > pub_degree)
TcornerDegree = pub_degree;
if (TcornerDegree > 0)
this.Invalidate();
}
}
private Color TshineColor = Color.Red;
[Browsable(true), Category("按钮的属性设置"), Description("按钮的光泽度颜色")]
public Color shineColor
{
get { return TshineColor; }
set
{
TshineColor = value;
this.Invalidate();
}
}
private int TtranparentDegree = 2;
[Browsable(true), Category("按钮的属性设置"), Description("按钮的透明度数")]
public int transparentDegree
{
get { return TtranparentDegree; }
set
{
TtranparentDegree = value;
if (TtranparentDegree > 20)
TtranparentDegree = 20;
if (TtranparentDegree < 0)
TtranparentDegree = 0;
if (TtranparentDegree > 0)
this.Invalidate();
}
}
private string TbtText = "Text";
[Browsable(true), Category("按钮的属性设置"), Description("设置显示的文本")]
public string btText
{
get { return TbtText; }
set
{
TbtText = value;
if (TbtText.Length > 0)
this.Invalidate();
}
}
#endregion
#region 事件
protected override void OnPaint( PaintEventArgs e)
{
transparentButton_Paint( this , e);
}
private void transparentButton_Paint(object sender, PaintEventArgs e)
{
this.BackColor = Color.Transparent;
sMode = e.Graphics.SmoothingMode;
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
if (this.transparentDegree == 0)
{
backgroundColorClicked(rect, e.Graphics);
backGroundColorUnclicked(rect, e.Graphics, this.shineColor);
}
else
{
if (this.transparentDegree > 0)
{
backgroundColorClicked(rect, e.Graphics);
for (int i = 0; i < transparentDegree; i++)
{
backGroundColorUnclicked(rect, e.Graphics, this.shineColor);
}
}
}
if (btClick == false)
backgroundColorClicked(rect, e.Graphics);
if (this.btText.Length > 0)
{
drawText(e.Graphics);
}
}
private void transparentButton_SizeChanged(object sender, EventArgs e)
{
this.Invalidate();
}
private void transparentButton_MouseDown(object sender, EventArgs e)
{
btClick = false;
this.Invalidate();
}
private void transparentButton_MouseUp(object sender, EventArgs e)
{
btClick = true;
this.Invalidate();
}
#endregion
#region 自定义方法
//未按下时按钮的背景色
private void backGroundColorUnclicked(Rectangle rect, Graphics g, Color fillColor)
{
using (GraphicsPath path = createCircleCornerRectangle(rect, this.cornerDegree))
{
int opacity = 255;
opacity = (int)(0.4f * opacity + 0.5f);
using (LinearGradientBrush brush = new LinearGradientBrush(rect, Color.FromArgb(opacity / 5, fillColor), Color.FromArgb(opacity, fillColor), LinearGradientMode.Vertical))
{
g.FillPath(brush, path);
}
g.SmoothingMode = sMode;
}
}
//按钮按下时背景色
private void backgroundColorClicked(Rectangle rect, Graphics g)
{
int opacity = 255;
Color tempColor = Color.Green;
if (btClick == true)
{
opacity = 125;
tempColor = Color.Blue;
}
using (GraphicsPath path = createCircleCornerRectangle(rect, this.cornerDegree))
{
using (LinearGradientBrush brush = new LinearGradientBrush(rect, Color.FromArgb(opacity / 5, tempColor), Color.FromArgb(opacity, tempColor), LinearGradientMode.Vertical))
{
g.FillPath(brush, path);
}
g.SmoothingMode = sMode;
}
}
private void drawText(Graphics g)
{
Graphics txtG = CreateGraphics();
string text = this.btText;
SizeF size = txtG.MeasureString(text, this.Font);
float txtWidth = size.Width;
float txtHeight = size.Height;
float x = 0;
float y = 0;
if (this.Height > txtHeight)
y = (this.Height - txtHeight) / 2;
else
y = this.cornerDegree;
if (this.Width > txtWidth)
x = (this.Width - txtWidth) / 2;
else
x = this.cornerDegree;
Rectangle rect = new Rectangle((int)Math.Floor(x), (int)Math.Floor(y), (int)Math.Ceiling(txtWidth), (int)Math.Ceiling(txtHeight));
int opacity = 255;
opacity = (int)(0.4f * opacity + 0.5f);
using (LinearGradientBrush brush = new LinearGradientBrush(rect, Color.FromArgb(opacity / 5, Color.Black), Color.FromArgb(opacity, Color.Black), LinearGradientMode.Vertical))
{
g.DrawString(text, this.Font, brush, new PointF(x, y));
}
}
//创建圆角矩形,radius是圆角的度数
private static GraphicsPath createCircleCornerRectangle(Rectangle rect, int radius)
{
GraphicsPath path = new GraphicsPath();
int l = rect.Left;
int t = rect.Top;
int w = rect.Width;
int h = rect.Height;
path.AddArc(l, t, 2 * radius, 2 * radius, 0, 0);
path.AddLine(l, t, l + w, t);
path.AddArc(l + w, t, 2 * radius, 2 * radius, 0, 0);
path.AddLine(l + w, t + radius, l + w, t + h - radius);
path.AddArc(l + w - 2 * radius, t + h - 2 * radius, 2 * radius, 2 * radius, 0, 90);
path.AddLine(l + radius, t + h, l + w - radius, t + h);
path.AddArc(l, t + h - 2 * radius, 2 * radius, 2 * radius, 90, 90);
path.AddLine(l, t + radius, l, t + h - radius);
return path;
}
#endregion
}
}