前段時間做一個項目要添加按鈕以及pictureBox的動態效果,所以自己動手編了一個類實現了控制項的自由縮放以及抖動。其中自由縮放有兩種方式:1.滑鼠進入後該控制項逐漸層大滑鼠移開後控制項逐漸回複原來大小2.滑鼠在控制項上按下時控制項縮小滑鼠UP時控制項回複大小;控制項的自由抖動也有兩種:1.滑鼠進入該控制項後控制項猛然向上跳動隨後有震顫效果滑鼠移開後控制項下落回原位置。2.滑鼠進入該控制項時控制項來回抖動滑鼠移開後控制項回複靜止。 綜上所述該類提供了4種動態效果。源碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Timers ;
namespace controlChangeDemo
{
public class ControlChange
{
#region 屬性及建構函式
private string filename; //圖片路徑
private Control control; //控制項
private Size osize; //控制項原大小
private Size nsize; //控制項新大小
private Point eachChangeP; //每次移動的大小
private int time; //變動次數
private int timetick; //時鐘時間間隔
private int count =0; //記錄變化次數
private System.Timers.Timer timer; //時鐘
private Point controlLocation; //控制項的開始位置
public ControlChange(Control con, string filename)
{
//將檢查非同步呼叫控制項這個屬性設為false後即可通過此類控制控制項
Control.CheckForIllegalCrossThreadCalls = false;
//本類要控制的控制項
this.control = con;
//控制項的背景圖片路徑
this.filename = filename;
con.BackgroundImage = Image.FromFile(filename);
//圖片隨控制項大小伸展
this.control.BackgroundImageLayout = ImageLayout.Stretch;
//控制項原始位置
this.controlLocation = this.control.Location;
}
public ControlChange(Control con)
{
//將檢查非同步呼叫控制項這個屬性設為false後即可通過此類控制控制項
Control.CheckForIllegalCrossThreadCalls = false;
this.control = con;
this.control.BackgroundImageLayout = ImageLayout.Stretch;
this.controlLocation = this.control.Location;
//變動次數預設為8次
this.time = 8;
}
public int getTime()
{
return this.time;
}
public void setTime(int t)
{
this.time = t;
}
#endregion
#region 控制控制項大小
#region 調用此函數但滑鼠移上控制項時控制項逐漸層大,移開時控制項逐漸恢複至原來大小
public void pictureBecomeBigger(int timetick, int time, Size os, Size ns)
{
//響應滑鼠進入事件
this.control.MouseEnter += new EventHandler(pictureBiggerEvent);
// 響應滑鼠離開事件
this.control.MouseLeave += new EventHandler(pictureLittlerEvent);
this.timetick = timetick;
this.time = time;
this.osize = os;
this.nsize = ns;
}
private void pictureBiggerEvent(object sender, EventArgs e)
{
pictureBigging();
}
private void pictureLittlerEvent(object sender, EventArgs e)
{
if (this.timer.Enabled == true)
{
this.timer.Close();
}
litterChange();
}
#endregion
#region 調用此函數當單擊控制項時控制項逐漸層小然後逐漸回複原來大小
public void pictureBecomeLitter(int timetick, int time, Size os, Size ns)
{
this.count =time;
this.control.MouseDown+=new MouseEventHandler (lbMouseDownEvent);
this.control.MouseUp += new MouseEventHandler(lbMouseUpEvent);
this.timetick = timetick;
this.time = time;
this.osize = os;
this.nsize = ns;
}
private void lbMouseDownEvent(object s, MouseEventArgs e)
{
this.count = this.time;
//調用litterChange函數控制控制項逐漸層小
litterChange ();
}
private void lbMouseUpEvent(object s, MouseEventArgs e)
{
//this.count = this.time;
////調用litterChange函數控制控制項逐漸層小
//litterChange();
if (this.timer.Enabled == true)
{
this.timer.Close();
//調用picturesBigging函數控制控制項回複原來大小
pictureBigging();
}
}
#endregion
private void pictureBigging()
{
eachChangeP = new Point((nsize.Width - osize.Width) / time, (nsize.Height - osize.Height ) / time);
timer = new System.Timers.Timer(timetick);
timer.Elapsed += new System.Timers.ElapsedEventHandler(pictureBigertimer);
timer.Start();
}
private void pictureBigertimer(object source, System.Timers.ElapsedEventArgs e)
{
if (this.count >= this.time)
{
this.timer.Close();
}
else
{
this.control.Location = new Point(this.control.Location.X - eachChangeP.X / 2, this.control.Location.Y - eachChangeP.Y / 2);
this.control.Size += new Size(eachChangeP.X, eachChangeP.Y);
this.count += 1;
}
}
private void litterChange()
{
eachChangeP = new Point((nsize.Width - osize.Width) / time, (nsize.Height - osize.Height ) / time);
timer = new System.Timers.Timer(timetick);
timer.Elapsed += new System.Timers.ElapsedEventHandler(pictureLitterTimer);
timer.Start();
}
private void pictureLitterTimer(object source, System.Timers.ElapsedEventArgs e)
{
try
{
if (this.count <=0)
{
this.timer.Close();
this.count = 0;
}
else
{
this.control.Location = new Point(this.control.Location.X + eachChangeP.X / 2, this.control.Location.Y + eachChangeP.Y / 2);
this.control.Size -= new Size(eachChangeP.X, eachChangeP.Y);
this.count -= 1;
}
}
catch
{
}
}
#endregion
#region 讓控制項抖動
public void shakeControl(int ti,int titick)//time表示變化的次數;
{
this.time = ti;
this.timetick = titick;
this.control.MouseEnter += new EventHandler(shakeMouseEnterEvent);
this.control.MouseLeave += new EventHandler(shakeMouseLeaveEvent);
}
private void shakeMouseEnterEvent(object source, EventArgs e)
{
this.count = 0;
timer = new System.Timers.Timer(this.timetick);
timer.Elapsed += new ElapsedEventHandler(shakeMouseEnterTimer);
timer.Start();
}
private void shakeMouseLeaveEvent(object source, EventArgs e)
{
if (this.timer.Enabled == true)
{
this.timer.Close();
}
this.count = 0;
control.Location = controlLocation;
}
private void shakeMouseEnterTimer(object source,System .Timers .ElapsedEventArgs e)
{
int changeLength=this.control .Size.Height/80*2^(count +1) ;
if(this.count%2==0)
{
if (this.count == 0)
{
this.control.Location = new Point(this.control.Location.X, this.control.Location.Y - changeLength-10);
}
else
{
this.control.Location = new Point(this.control.Location.X, this.control.Location.Y - changeLength);
}
}
else
{
this.control.Location = new Point (this.control .Location .X ,this.control .Location .Y +changeLength) ;
}
count =count+1;
if (count >= time)
{
this.timer.Close();
this.count = 0;
}
}
#endregion
#region 讓控制項來回抖動
public void moveControl(int change,int titick)
{
this.time = change;
this.timetick = titick;
this.control.MouseEnter += new EventHandler(moveMouseEnterEvent);
this.control.MouseLeave += new EventHandler(moveMouseLeaveEvent);
}
private void moveMouseEnterEvent(object source, EventArgs e)
{
this.count = 0;
timer = new System.Timers.Timer(this.timetick);
timer.Elapsed += new ElapsedEventHandler(moveMouseEnterTimer);
timer.Start();
}
private void moveMouseLeaveEvent(object source, EventArgs e)
{
if (this.timer.Enabled == true)
{
this.timer.Close();
}
this.count = 0;
control.Location = controlLocation;
}
private void moveMouseEnterTimer(object source, System.Timers.ElapsedEventArgs e)
{
if (count < this.time * 2)
{
int i = count / 2;
if (0 == count % 2)
{
this.control.Location = new Point(this.controlLocation.X, this.controlLocation.Y - this.time + i);
}
else
{
this.control.Location = new Point(this.controlLocation.X, this.controlLocation.Y + this.time - i);
}
}
else
{
this.timer.Close();
}
++count;
}
#endregion
}
}
使用者使用此類事只需初始化一個該類對象並調用:pictureBecomeBigger,pictureBecomeLitter,shakeControl或moveControl中的一個函數即可實現一種動態效果。而為了各位更好的使用此類本人還寫了一個demo程式展示這4個動態效果,大家可以在MSDN上用“C#控制項動態效果執行個體”這一標題來搜尋這一源碼並下載。