📄 ellipsebutton.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CustomWinControls;
using System.Drawing.Drawing2D;
using System.Windows.Forms.VisualStyles;
namespace ExtensibleControls
{
public partial class EllipseButton : CustomButton
{
private PushButtonState state = PushButtonState.Normal;
private Color startColor = Color.White; //渐变起始色
private Color endColor = Color.Red; //渐变终止色
private Rectangle rect = new Rectangle();
[Description("设定渐变的起始色"), Category("Appearance")]
public Color StartColor
{
get
{
return startColor;
}
set
{
startColor = value;
OnPaint(new PaintEventArgs(this.CreateGraphics(), ClientRectangle));
}
}
[Description("设定渐变的终止色"), Category("Appearance")]
public Color EndColor
{
get
{
return endColor;
}
set
{
endColor = value;
OnPaint(new PaintEventArgs(this.CreateGraphics(), ClientRectangle));
}
}
public EllipseButton()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs pe)
{
// 调用基类 OnPaint
base.OnPaint(pe);
pe.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
pe.Graphics.Clear(this.BackColor);
rect = (state == PushButtonState.Pressed) ? this.ClickRectangle : this.ClientRectangle;
LinearGradientBrush myBrush = new LinearGradientBrush(rect, StartColor, EndColor, LinearGradientMode.ForwardDiagonal);
ButtonRenderer.DrawParentBackground(pe.Graphics, ClientRectangle, this);
pe.Graphics.FillEllipse(myBrush, rect);
myBrush.Dispose();
StringFormat alignFormat = new StringFormat();
alignFormat.LineAlignment = StringAlignment.Center;
alignFormat.Alignment = StringAlignment.Center;
pe.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), rect, alignFormat);
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
//设置按钮视觉状态为按下状态
state = PushButtonState.Pressed;
Invalidate();
}
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
//设置按钮状态为激活状态
state = PushButtonState.Hot;
Invalidate();
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
state = PushButtonState.Normal;
Invalidate();
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
OnMouseEnter(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
// 鼠标左键按下并且光标从按下的按钮上离开时,
// 恢复按钮的正常外观。
if ((e.Button & MouseButtons.Left) == MouseButtons.Left &&
!ClientRectangle.Contains(e.Location) &&
state == PushButtonState.Pressed)
{
OnMouseLeave(e);
}
}
protected override void OnTextChanged(EventArgs e)
{
OnPaint(new PaintEventArgs(this.CreateGraphics(), ClientRectangle));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -