FlatStyle按钮自己做
和C或VB比起来,使用C#写自定义控件,特别是从零开始写自定义控件,不知简化了多少倍,没有复杂而晦涩的消息映射,思路就像写应用程序一样如行云流水,一路畅通。学过delphi的人都知道有一套FlatStyle控件,平面式的简约风格给用户带来了良好的视觉效果。但是看看他的源码,如果没有一定的Windows底层功底,看起来可不是件容易的事。现在好了,有了C#这个异常强大的工具,我们就可以轻轻松松的写一个自己的FlatStyle控件了。
本文以平面按钮为例,写一个按钮颜色和边框颜色能任意更改的自定义按钮控件,希望能起到抛砖引玉的作用。
如果我们只是改变已有的控件的外观(如本例),那么最好继承原有的控件。如:
public class ButtonLzh : System.Windows.Forms.Button
定义一个枚举类型,用来描述鼠标的状态:
protected enum Status
{
MouseEnter,
MouseLeave,
MouseDown,
MouseUp
}
写一个画文字的函数:
protected void DrawString(string strText,Graphics g)
{
StringFormat sf=new StringFormat();
sf.LineAlignment=StringAlignment.Center;
sf.Alignment=StringAlignment.Center;
g.DrawString(strText,this.Font,new SolidBrush(Color.Blue),this.ClientRectangle,sf);
}
填充按钮颜色及画边框颜色:
protected void DrawBorder(Status status,Graphics g)
{
Pen[] MousePen=new Pen[4];//定义四支笔
MousePen[(int)Status.MouseDown]=new Pen(_MouseDownBorderColor,2);
MousePen[(int)Status.MouseEnter]=new Pen(_MouseEnterBorderColor,2);
MousePen[(int)Status.MouseLeave]=new Pen(_MouseLeaveBorderColor,2);
MousePen[(int)Status.MouseUp]=new Pen(_MouseUpBorderColor,2);
SolidBrush[] MouseBrush=new SolidBrush[4];//定义四个笔刷
MouseBrush[(int)Status.MouseDown]=new SolidBrush(_MouseDownColor);
MouseBrush[(int)Status.MouseEnter]=new SolidBrush(_MouseEnterColor);
MouseBrush[(int)Status.MouseLeave]=new SolidBrush(_MouseLeaveColor);
MouseBrush[(int)Status.MouseUp]=new SolidBrush(_MouseUpColor);
switch(status)
{
case Status.MouseDown:
g.FillRectangle(MouseBrush[(int)Status.MouseDown],this.ClientRectangle);
g.DrawRectangle(MousePen[(int)Status.MouseDown],this.ClientRectangle);
break;
case Status.MouseEnter:
g.FillRectangle(MouseBrush[(int)Status.MouseEnter],this.ClientRectangle);
g.DrawRectangle(MousePen[(int)Status.MouseEnter],this.ClientRectangle);
break;
case Status.MouseLeave:
g.FillRectangle(MouseBrush[(int)Status.MouseLeave],this.ClientRectangle);
g.DrawRectangle(MousePen[(int)Status.MouseLeave],this.ClientRectangle);
break;
case Status.MouseUp:
g.FillRectangle(MouseBrush[(int)Status.MouseUp],this.ClientRectangle);
g.DrawRectangle(MousePen[(int)Status.MouseUp],this.ClientRectangle);
break;
}
for(int i=0;i<4;i++)
{
MousePen[i].Dispose();
MouseBrush[i].Dispose();
}
}
重载OnPaint()事件:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
Graphics g=e.Graphics;
switch(MouseStatus)
{
case Status.MouseDown:
this.DrawBorder(Status.MouseDown,g);
this.DrawString(this.Text,g);
break;
case Status.MouseEnter:
this.DrawBorder(Status.MouseEnter,g);
this.DrawString(this.Text,g);
break;
case Status.MouseLeave:
this.DrawBorder(Status.MouseLeave,g);
this.DrawString(this.Text,g);
break;
case Status.MouseUp:
this.DrawBorder(Status.MouseUp,g);
this.DrawString(this.Text,g);
break;
}
}
基本的函数就是这样了,无外乎就是画边框,然后填充颜色。当我们重载了OnPaint()事件后,也就等于把按钮的外观绘制权交到了自己的手里,你想怎么画就怎么画啦。
以下是程序的所有源码,新建一个Windows控件库,把代码复制过去就OK!
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace LzhButton
{
/// <summary>
/// UserControl1 的摘要说明。
/// </summary>
public class ButtonLzh : System.Windows.Forms.Button
{
private Color _MouseEnterColor;
private Color _MouseLeaveColor;
private Color _MouseDownColor;
private Color _MouseUpColor;
private Color _MouseEnterBorderColor;
private Color _MouseLeaveBorderColor;
private Color _MouseDownBorderColor;
private Color _MouseUpBorderColor;
private Status MouseStatus;
[Description("鼠标进入控件内的颜色"),Category("Appearance")]
public Color MouseEnterColor
{
get
{
return _MouseEnterColor;
}
set
{
_MouseEnterColor=value;
}
}
[Description("鼠标移出控件外的颜色"),Category("Appearance")]
public Color MouseLeaveColor
{
get
{
return _MouseLeaveColor;
}
set
{
_MouseLeaveColor=value;
}
}
[Description("鼠标按下时按钮的颜色"),Category("Appearance")]
public Color MouseDownColor
{
get
{
return _MouseDownColor;
}
set
{
_MouseDownColor=value;
}
}
[Description("鼠标弹起时按钮的颜色"),Category("Appearance")]
public Color MouseUpColor
{
get
{
return _MouseUpColor;
}
set
{
_MouseUpColor=value;
}
}
/////////////////////////////////////////////
[Description("鼠标进入按钮时的边框颜色"),Category("Appearance")]
public Color MouseEnterBorderColor
{
get
{
return _MouseEnterBorderColor;
}
set
{
_MouseEnterBorderColor=value;
}
}
[Description("鼠标移出按钮的边框颜色"),Category("Appearance")]
public Color MouseLeaveBorderColor
{
get
{
return _MouseLeaveBorderColor;
}
set
{
_MouseLeaveBorderColor=value;
}
}
[Description("鼠标按下按钮的边框颜色"),Category("Appearance")]
public Color MouseDownBorderColor
{
get
{
return _MouseDownBorderColor;
}
set
{
_MouseDownBorderColor=value;
}
}
[Description("鼠标弹起时按钮的边框颜色"),Category("Appearance")]
public Color MouseUpBorderColor
{
get
{
return _MouseUpBorderColor;
}
set
{
_MouseUpBorderColor=value;
}
}
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public ButtonLzh()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// TODO: 在 InitComponent 调用后添加任何初始化
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// ButtonLzh
//
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.ButtonLzh_MouseUp);
this.MouseEnter += new System.EventHandler(this.ButtonLzh_MouseEnter);
this.MouseLeave += new System.EventHandler(this.ButtonLzh_MouseLeave);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ButtonLzh_MouseDown);
//初始化
_MouseEnterColor=Color.Yellow;
_MouseLeaveColor=Color.Pink;
_MouseDownColor=Color.Red;
_MouseUpColor=Color.Blue;
_MouseEnterBorderColor=Color.GreenYellow;
_MouseLeaveBorderColor=Color.DeepPink;
_MouseDownBorderColor=Color.PaleVioletRed;
_MouseUpBorderColor=Color.CadetBlue;
}
#endregion
protected enum Status
{
MouseEnter,
MouseLeave,
MouseDown,
MouseUp
}
/// <summary>
/// 画文字
/// </summary>
/// <param name="strText">要画的文字</param>
/// <param name="g">画布</param>
protected void DrawString(string strText,Graphics g)
{
StringFormat sf=new StringFormat();
sf.LineAlignment=StringAlignment.Center;
sf.Alignment=StringAlignment.Center;
g.DrawString(strText,this.Font,new SolidBrush(Color.Blue),this.ClientRectangle,sf);
}
/// <summary>
/// 画边框
/// </summary>
/// <param name="status">鼠标状态</param>
/// <param name="g">画布</param>
protected void DrawBorder(Status status,Graphics g)
{
Pen[] MousePen=new Pen[4];//定义四支笔
MousePen[(int)Status.MouseDown]=new Pen(_MouseDownBorderColor,2);
MousePen[(int)Status.MouseEnter]=new Pen(_MouseEnterBorderColor,2);
MousePen[(int)Status.MouseLeave]=new Pen(_MouseLeaveBorderColor,2);
MousePen[(int)Status.MouseUp]=new Pen(_MouseUpBorderColor,2);
SolidBrush[] MouseBrush=new SolidBrush[4];//定义四个笔刷
MouseBrush[(int)Status.MouseDown]=new SolidBrush(_MouseDownColor);
MouseBrush[(int)Status.MouseEnter]=new SolidBrush(_MouseEnterColor);
MouseBrush[(int)Status.MouseLeave]=new SolidBrush(_MouseLeaveColor);
MouseBrush[(int)Status.MouseUp]=new SolidBrush(_MouseUpColor);
switch(status)
{
case Status.MouseDown:
g.FillRectangle(MouseBrush[(int)Status.MouseDown],this.ClientRectangle);
g.DrawRectangle(MousePen[(int)Status.MouseDown],this.ClientRectangle);
break;
case Status.MouseEnter:
g.FillRectangle(MouseBrush[(int)Status.MouseEnter],this.ClientRectangle);
g.DrawRectangle(MousePen[(int)Status.MouseEnter],this.ClientRectangle);
break;
case Status.MouseLeave:
g.FillRectangle(MouseBrush[(int)Status.MouseLeave],this.ClientRectangle);
g.DrawRectangle(MousePen[(int)Status.MouseLeave],this.ClientRectangle);
break;
case Status.MouseUp:
g.FillRectangle(MouseBrush[(int)Status.MouseUp],this.ClientRectangle);
g.DrawRectangle(MousePen[(int)Status.MouseUp],this.ClientRectangle);
break;
}
for(int i=0;i<4;i++)
{
MousePen[i].Dispose();
MouseBrush[i].Dispose();
}
}
/// <summary>
/// 重载OnPaint事件
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
Graphics g=e.Graphics;
switch(MouseStatus)
{
case Status.MouseDown:
this.DrawBorder(Status.MouseDown,g);
this.DrawString(this.Text,g);
break;
case Status.MouseEnter:
this.DrawBorder(Status.MouseEnter,g);
this.DrawString(this.Text,g);
break;
case Status.MouseLeave:
this.DrawBorder(Status.MouseLeave,g);
this.DrawString(this.Text,g);
break;
case Status.MouseUp:
this.DrawBorder(Status.MouseUp,g);
this.DrawString(this.Text,g);
break;
}
}
private void ButtonLzh_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
MouseStatus=Status.MouseDown;
this.Invalidate();
}
private void ButtonLzh_MouseEnter(object sender, System.EventArgs e)
{
MouseStatus=Status.MouseEnter ;
this.Invalidate();
}
private void ButtonLzh_MouseLeave(object sender, System.EventArgs e)
{
MouseStatus=Status.MouseLeave;
this.Invalidate();
}
private void ButtonLzh_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
MouseStatus=Status.MouseUp;
this.Invalidate();
}
}
}