【转】C#使用GDI+制作背景颜色淡入淡出效果的按钮

C#使用GDI+制作背景颜色淡入淡出效果的按钮

2010-04-14  来自:CNBLOG  字体大小:【 大  中  小】
  • 摘要:本文介绍C#使用GDI+制作背景颜色淡入淡出效果的按钮,并提供完整的示例和源码供下载。

 

用过QQ2009的网友都知道QQ主面板的界面非常炫丽,特别好看,鼠标移上去还有淡入淡出的效果。那这样效果是怎么做出来的呢?其实不难,只要自定义一个用户控件的外怪就可以了,用到GDI+技术和时钟控件来操作

首先我们在VS2008里面新建一个Windows窗体控件库的项目,系统会自动生成一个用户控件UserControl1.cs出来,我们就用默认的名字吧~~

本例子下载地址:http://files.cnblogs.com/mengxin523/自定义按钮控件.rar

程序所有代码如下:


using System;

using System.Data;

using System.Drawing;

using System.Collections;

using System.Windows.Forms;

using System.ComponentModel;

using System.Drawing.Drawing2D;



namespace MyButton

... {

public partial class UserControl1 : UserControl

...{

private bool calledbykey = false;

private State mButtonState = State.None; //按钮的状态

private Timer mFadeIn = new Timer(); //淡入的时钟

private Timer mFadeOut = new Timer(); //淡出的时钟

private int mGlowAlpha = 0; //透明度

private System.ComponentModel.Container components = null;

public UserControl1()

...{

InitializeComponent();

//一下几个语句是对控件进行设置和对GDI+进行优化

this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);

this.SetStyle(ControlStyles.DoubleBuffer, true);

this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

this.SetStyle(ControlStyles.ResizeRedraw, true);

this.SetStyle(ControlStyles.Selectable, true);

this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

this.SetStyle(ControlStyles.UserPaint, true);

this.UpdateStyles();

this.BackColor = Color.Transparent; //设置控件背景色透明

mFadeIn.Interval
= 20; //淡入速度

mFadeOut.Interval
= 20; //淡出速度

}


protected override void Dispose(bool disposing)

...{

if (disposing)

...{

if (components != null)

...{

components.Dispose();

}


}


base.Dispose(disposing);

}


private void InitializeComponent()

...{

this.Name = "MySystemButton";

this.Size = new System.Drawing.Size(100, 32);

this.Paint += new System.Windows.Forms.PaintEventHandler(this.VistaButton_Paint);

this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.VistaButton_KeyUp);

this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.VistaButton_KeyDown);

this.MouseEnter += new System.EventHandler(this.VistaButton_MouseEnter);

this.MouseLeave += new System.EventHandler(this.VistaButton_MouseLeave);

this.MouseUp += new MouseEventHandler(VistaButton_MouseUp);

this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.VistaButton_MouseDown);

this.GotFocus += new EventHandler(VistaButton_MouseEnter);

this.LostFocus += new EventHandler(VistaButton_MouseLeave);

this.mFadeIn.Tick += new EventHandler(mFadeIn_Tick);

this.mFadeOut.Tick += new EventHandler(mFadeOut_Tick);

this.Resize += new EventHandler(VistaButton_Resize);

}


enum State ...{ None, Hover, Pressed };

/**////

/// 按钮的样式

///


public enum Style

...{

/**////

/// Draw the button as normal

///


Default,

/**////

/// Only draw the background on mouse over.

///


Flat

}
;

/**////

/// 用于设置按钮的用处

///


public enum UseTo

...{

Min, Close

}
;

UseTo Ut
= UseTo.Close; //默认作为关闭按钮

[Category(
"UseTo"),

DefaultValue(UseTo.Close),

Browsable(
true),

Description(
"设置按钮的用处")]

public UseTo UT

...{

get

...{

return Ut;

}


set

...{

Ut
= value;

this.Invalidate();

}


}


private string mText;

/**////

/// 按钮上显示的文本

///


[Category(
"Text"),

Description(
"按钮上显示的文本.")]

public string ButtonText

...{

get ...{ return mText; }

set ...{ mText = value; this.Invalidate(); }

}


private Color mForeColor = Color.White;

/**////

/// 文本颜色

你可能感兴趣的:(c#)