winform 自定义按钮的实现
按钮效果图如下所示。
通过颜色渐变渲染,让控件产生立体效果,上述的按钮就是通过线性渐变画刷二次渐变填充出来的。
详看代码,后面有时间再加入类似win7计算器的淡入淡出效果。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
namespace RogerBar
{
[ToolboxItem(true)]
public partial class BottonEx : Button
{
#region Constructor
public BottonEx()
{
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.UserPaint, true);
this.BackColor = Color.Transparent;
this.FlatAppearance.BorderSize = 0;
this.FlatStyle = FlatStyle.Flat;
InitializeComponent();
}
#endregion
#region Private
//按钮状态
enum ButtonState
{
//正常
Normal,
//鼠标进入
Hover,
//鼠标按下
Pressed
};
private ButtonState State = ButtonState.Normal;
#endregion
#region 属性
#region 颜色表
[TypeConverter(typeof(ExpandableObjectConverter))]
public class RenderColorTable
{
public Color TopStart { set; get; }
public Color TopEnd { set; get; }
public float Scale { set; get; }
public Color BottomStart { set; get; }
public Color BottomEnd { set; get; }
public Color BorderColor { set; get; }
public RenderColorTable(
Color topStart, Color topEnd,
Color bottomStart, Color bottomEnd,
Color bor