110,571
社区成员
发帖
与我相关
我的任务
分享
#region 属性
private bool IsPress = false;
private Color borderColor = Color.Black;
/// <summary>
/// 边框颜色
/// </summary>
public Color BorderColor
{
get { return borderColor; }
set {
borderColor = value;
Invalidate();
}
}
private int borderSize = 1;
/// <summary>
/// 边框大小
/// </summary>
public int BorderSize
{
get { return borderSize; }
set {
borderSize = value;
Invalidate();
}
}
private int roundCornerSize = 0;
/// <summary>
/// 圆角大小
/// </summary>
public int RoundCornerSize
{
get { return roundCornerSize; }
set {
roundCornerSize = value;
Invalidate();
}
}
private Color impressBackColor = Color.Black;
/// <summary>
/// 点击下颜色
/// </summary>
public Color ImpressBackColor
{
get {
return impressBackColor;
}
set {
impressBackColor = value;
}
}
private Color impressForeColor = Color.Black;
/// <summary>
/// 点击下前景色
/// </summary>
public Color ImpressForeColor
{
get
{
return impressForeColor;
}
set
{
impressForeColor = value;
}
}
#endregion
protected override void OnPaint(PaintEventArgs pe)
{
Graphics g = pe.Graphics;
if (!IsPress)
{
Pen pen = new Pen(BorderColor);
pen.Width = BorderSize;
GraphicsExtension.DrawRoundedRect(g, pen, BackColor, new Rectangle(0, 0, this.Width - 1, this.Height - 1), new Size(RoundCornerSize, RoundCornerSize));
//渐变
//GraphicsExtension.FillGradientRectangle(g, new Rectangle(0, 0, this.Width - 1, this.Height - 1), Color.White, Color.Black, FillDirection.TopToBottom);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
g.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new Rectangle(0, 0, this.Width - 1, this.Height - 1), sf);
}
else
{
Pen pen = new Pen(BorderColor);
pen.Width = BorderSize;
GraphicsExtension.DrawRoundedRect(g, pen, impressBackColor, new Rectangle(0, 0, this.Width - 1, this.Height - 1), new Size(RoundCornerSize, RoundCornerSize));
//渐变
//GraphicsExtension.FillGradientRectangle(g, new Rectangle(0, 0, this.Width - 1, this.Height - 1), Color.White, Color.Black, FillDirection.TopToBottom);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
g.DrawString(this.Text, this.Font, new SolidBrush(this.ImpressForeColor), new Rectangle(0, 0, this.Width - 1, this.Height - 1), sf);
}
base.OnPaint(pe);
}
private void ExButton_MouseDown(object sender, MouseEventArgs e)
{
IsPress = true;
Invalidate();
}
private void ExButton_MouseUp(object sender, MouseEventArgs e)
{
IsPress = false;
Invalidate();
}
public Color ImpressBackColor
{
get {
return impressBackColor;
}
set {
impressBackColor = value;
Invalidate();
}
}
public Color ImpressForeColor
{
get
{
return impressForeColor;
}
set
{
impressForeColor = value;
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
Graphics g = pe.Graphics;
if (!IsPress)
{
Pen pen = new Pen(BorderColor);
pen.Width = BorderSize;
GraphicsExtension.DrawRoundedRect(g, pen, BackColor, new Rectangle(0, 0, this.Width - 1, this.Height - 1), new Size(RoundCornerSize, RoundCornerSize));
//渐变
//GraphicsExtension.FillGradientRectangle(g, new Rectangle(0, 0, this.Width - 1, this.Height - 1), Color.White, Color.Black, FillDirection.TopToBottom);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
g.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new Rectangle(0, 0, this.Width - 1, this.Height - 1), sf);
}
else
{
Pen pen = new Pen(BorderColor);
pen.Width = BorderSize;
GraphicsExtension.DrawRoundedRect(g, pen, impressBackColor, new Rectangle(0, 0, this.Width - 1, this.Height - 1), new Size(RoundCornerSize, RoundCornerSize));
//渐变
//GraphicsExtension.FillGradientRectangle(g, new Rectangle(0, 0, this.Width - 1, this.Height - 1), Color.White, Color.Black, FillDirection.TopToBottom);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
g.DrawString(this.Text, this.Font, new SolidBrush(this.ImpressForeColor), new Rectangle(0, 0, this.Width - 1, this.Height - 1), sf);
}
}