明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

FlatStyle按鈕自己做

[摘要]FlatStyle按鈕自己做 和C或VB比起來,使用C#寫自定義控件,特別是從零開始寫自定義控件,不知簡化了多少倍,沒有復(fù)雜而晦澀的消息映射,思路就像寫應(yīng)用程序一樣如行云流水,一路暢通...
FlatStyle按鈕自己做



和C或VB比起來,使用C#寫自定義控件,特別是從零開始寫自定義控件,不知簡化了多少倍,沒有復(fù)雜而晦澀的消息映射,思路就像寫應(yīng)用程序一樣如行云流水,一路暢通。學(xué)過delphi的人都知道有一套FlatStyle控件,平面式的簡約風(fēng)格給用戶帶來了良好的視覺效果。但是看看他的源碼,如果沒有一定的Windows底層功底,看起來可不是件容易的事,F(xiàn)在好了,有了C#這個(gè)異常強(qiáng)大的工具,我們就可以輕輕松松的寫一個(gè)自己的FlatStyle控件了。



本文以平面按鈕為例,寫一個(gè)按鈕顏色和邊框顏色能任意更改的自定義按鈕控件,希望能起到拋磚引玉的作用。



如果我們只是改變已有的控件的外觀(如本例),那么最好繼承原有的控件。如:

public class ButtonLzh : System.Windows.Forms.Button



定義一個(gè)枚舉類型,用來描述鼠標(biāo)的狀態(tài):

protected enum Status

{

MouseEnter,

MouseLeave,

MouseDown,

MouseUp

}



寫一個(gè)畫文字的函數(shù):

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];//定義四個(gè)筆刷

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;

}



}



基本的函數(shù)就是這樣了,無外乎就是畫邊框,然后填充顏色。當(dāng)我們重載了OnPaint()事件后,也就等于把按鈕的外觀繪制權(quán)交到了自己的手里,你想怎么畫就怎么畫啦。



以下是程序的所有源碼,新建一個(gè)Windows控件庫,把代碼復(fù)制過去就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("鼠標(biāo)進(jìn)入控件內(nèi)的顏色"),Category("Appearance")]

public Color MouseEnterColor

{

get

{

return _MouseEnterColor;

}

set

{

_MouseEnterColor=value;

}

}



[Description("鼠標(biāo)移出控件外的顏色"),Category("Appearance")]

public Color MouseLeaveColor

{

get

{

return _MouseLeaveColor;

}

set

{

_MouseLeaveColor=value;

}

}



[Description("鼠標(biāo)按下時(shí)按鈕的顏色"),Category("Appearance")]

public Color MouseDownColor

{

get

{

return _MouseDownColor;

}

set

{

_MouseDownColor=value;

}

}



[Description("鼠標(biāo)彈起時(shí)按鈕的顏色"),Category("Appearance")]

public Color MouseUpColor

{

get

{

return _MouseUpColor;

}

set

{

_MouseUpColor=value;

}

}





/////////////////////////////////////////////

[Description("鼠標(biāo)進(jìn)入按鈕時(shí)的邊框顏色"),Category("Appearance")]

public Color MouseEnterBorderColor

{

get

{

return _MouseEnterBorderColor;

}

set

{

_MouseEnterBorderColor=value;

}

}

[Description("鼠標(biāo)移出按鈕的邊框顏色"),Category("Appearance")]

public Color MouseLeaveBorderColor

{

get

{

return _MouseLeaveBorderColor;

}

set

{

_MouseLeaveBorderColor=value;

}

}



[Description("鼠標(biāo)按下按鈕的邊框顏色"),Category("Appearance")]

public Color MouseDownBorderColor

{

get

{

return _MouseDownBorderColor;

}

set

{

_MouseDownBorderColor=value;

}

}



[Description("鼠標(biāo)彈起時(shí)按鈕的邊框顏色"),Category("Appearance")]

public Color MouseUpBorderColor

{

get

{

return _MouseUpBorderColor;

}

set

{

_MouseUpBorderColor=value;

}

}





/// <summary>

/// 必需的設(shè)計(jì)器變量。

/// </summary>

private System.ComponentModel.Container components = null;



public ButtonLzh()

{

// 該調(diào)用是 Windows.Forms 窗體設(shè)計(jì)器所必需的。

InitializeComponent();



// TODO: 在 InitComponent 調(diào)用后添加任何初始化



}



/// <summary>

/// 清理所有正在使用的資源。

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if( components != null )

components.Dispose();

}

base.Dispose( disposing );

}



#region 組件設(shè)計(jì)器生成的代碼

/// <summary>

/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器

/// 修改此方法的內(nèi)容。

/// </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">鼠標(biāo)狀態(tài)</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];//定義四個(gè)筆刷

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();

}



}

}




標(biāo)簽:FlatStyle按鈕自己做