横向菜单动画:
public partial class Animation : Control { #region /// <summary> /// 鼠标进入动画播放定时器 /// </summary> private System.Timers.Timer mouseEnterTimer = new System.Timers.Timer(); /// <summary> /// 鼠标离开动画播放定时器 /// </summary> private System.Timers.Timer mouseLeaveTimer = new System.Timers.Timer(); /// <summary> /// elc列表 /// </summary> private List<EnterLeaveClass> elcList = new List<EnterLeaveClass>(); /// <summary> /// PictureBox选项 /// </summary> private List<PictureBox> carouselPictureBox = new List<PictureBox>(); /// <summary> /// 是否播放鼠标进入动画 /// </summary> private bool mouseEnterRun = false; /// <summary> /// 是否播放鼠标离开动画 /// </summary> private bool mouseLeaveRun = false; /// <summary> /// 容器大小 /// </summary> private Size panelSize; /// <summary> /// pictureBox默认大小 /// </summary> private Size pictureBoxMinSize; /// <summary> /// pictureBox动画后大小 /// </summary> private Size pictureBoxMaxSize; /// <summary> /// pictureBox左右边距 /// </summary> private int pictureBoxMargin = 10; #endregion #region private List<Image> carouselImages = new List<Image>(); /// <summary> /// 要播放的图片 /// </summary> [Description("要播放的图片")] public List<Image> CarouselImages { get { return carouselImages; } set { carouselImages = value; } } private double animationTime = 150; /// <summary> /// 动画播放的总时间 /// </summary> [DefaultValue(150)] [Description("动画播放的总时间(默认150毫秒)")] public double AnimationTime { get { return animationTime; } set { animationTime = value; } } #endregion public Animation() { InitializeComponent(); this.mouseEnterTimer.Interval = 10; this.mouseEnterTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.mouseEnterTimer_Elapsed); this.mouseLeaveTimer.Interval = 10; this.mouseLeaveTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.mouseLeaveTimer_Elapsed); this.mouseEnterTimer.Start(); this.mouseLeaveTimer.Start(); } /// <summary> /// 放大定时器 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void mouseEnterTimer_Elapsed(object sender, ElapsedEventArgs e) { if (!this.mouseEnterRun) return; EnterLeaveClass elc = (EnterLeaveClass)this.elcList.Where(a => a.type == "in").FirstOrDefault(); if (elc == null) return; double seconds = ((DateTime.Now - elc.strattime).TotalMilliseconds);//动画已运行时间 if (seconds > this.animationTime)//动画结束 { elc.type = ""; elc.isyushu_w = 0; elc.isyushu_h = 0; elc.isyushu_x = 0; elc.isyushu_y = 0; this.mouseEnterRun = false; } else { int width = 0; setRemainder(ref elc.isyushu_w, TweenHelper.easeBoth((double)elc.size.Width, ((double)this.pictureBoxMaxSize.Width - (double)elc.size.Width), seconds, this.animationTime), ref width); int height = 0; setRemainder(ref elc.isyushu_h, TweenHelper.easeBoth((double)elc.size.Height, ((double)this.pictureBoxMaxSize.Height - (double)elc.size.Height), seconds, this.animationTime), ref height); int x = 0; setRemainder(ref elc.isyushu_x, TweenHelper.easeBoth(elc.point.X, -(this.pictureBoxMaxSize.Width - elc.size.Width) / 2, seconds, this.animationTime), ref x); int y = 0; setRemainder(ref elc.isyushu_y, this.panelSize.Height - (this.pictureBoxMargin + height) + 5, ref y); this.BeginInvoke(new Action<EnterLeaveClass, int, int, int, int>(delegate(EnterLeaveClass obj, int _width, int _height, int _x, int _y) { obj.pb.Size = new Size(_width, _height); obj.pb.Location = new Point(_x, _y); }), elc, width, height, x, y); } } /// <summary> /// 缩小定时器 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void mouseLeaveTimer_Elapsed(object sender, ElapsedEventArgs e) { if (!this.mouseLeaveRun) return; List<EnterLeaveClass> itemlist = this.elcList.Where<EnterLeaveClass>(a => a.type == "out").ToList<EnterLeaveClass>(); for (int i = 0; i < itemlist.Count; i++) { double seconds = ((DateTime.Now - itemlist[i].strattime).TotalMilliseconds);//动画已运行时间 if (seconds > this.animationTime)//动画结束 { itemlist[i].type = ""; itemlist[i].isyushu_w = 0; itemlist[i].isyushu_h = 0; itemlist[i].isyushu_x = 0; itemlist[i].isyushu_y = 0; this.BeginInvoke(new Action<EnterLeaveClass, int>(delegate(EnterLeaveClass obj, int index) { obj.pb.Location = new Point(this.pictureBoxMargin * (index + 2) + this.pictureBoxMinSize.Width * index, this.panelSize.Height - (this.pictureBoxMargin + this.pictureBoxMinSize.Height)); }), itemlist[i], itemlist[i].pb.Tag); if (this.elcList.Where<EnterLeaveClass>(a => a.type == "out").Count() < 1) { this.mouseEnterRun = false; } } else { int width = 0; setRemainder(ref itemlist[i].isyushu_w, TweenHelper.easeBoth(itemlist[i].size.Width, (this.pictureBoxMinSize.Width - itemlist[i].size.Width), seconds, this.animationTime), ref width); int height = 0; setRemainder(ref itemlist[i].isyushu_h, TweenHelper.easeBoth(itemlist[i].size.Height, (this.pictureBoxMinSize.Height - itemlist[i].size.Height), seconds, this.animationTime), ref height); int x = 0; setRemainder(ref itemlist[i].isyushu_x, TweenHelper.easeBoth(itemlist[i].point.X, +(itemlist[i].size.Width - this.pictureBoxMinSize.Width) / 2, seconds, this.animationTime), ref x); int y = this.panelSize.Height - (this.pictureBoxMargin + height); this.BeginInvoke(new Action<EnterLeaveClass, int, int, int, int>(delegate(EnterLeaveClass obj, int _min_width, int _min_height, int _min_x, int _min_y) { obj.pb.Size = new Size(_min_width, _min_height); obj.pb.Location = new Point(_min_x, _min_y); }), itemlist[i], width, height, x, y); } } } /// <summary> /// 加载PictureBox大小位置 /// </summary> public void LoadPictureBox() { this.Controls.Clear(); if (this.carouselImages.Count > 0) { this.panelSize = this.Size; this.pictureBoxMinSize = new Size(((this.panelSize.Width - (this.CarouselImages.Count + 1) * this.pictureBoxMargin) - this.pictureBoxMargin * 2) / this.CarouselImages.Count, this.panelSize.Height / 3 * 2); this.pictureBoxMaxSize = new Size(this.pictureBoxMinSize.Width + this.pictureBoxMargin * 4, this.pictureBoxMinSize.Height + this.pictureBoxMargin * 4); for (int i = 0; i < carouselImages.Count; i++) { PictureBox box = new PictureBox() { SizeMode = PictureBoxSizeMode.StretchImage, Image = carouselImages[i], Tag = i }; box.MouseEnter += new EventHandler(this.p_MouseEnter); box.MouseLeave += new EventHandler(this.p_MouseLeave); this.carouselPictureBox.Add(box); this.elcList.Add(new EnterLeaveClass() { pb = box }); this.Controls.Add(this.carouselPictureBox[i]); this.carouselPictureBox[i].Size = this.pictureBoxMinSize; this.carouselPictureBox[i].Location = new Point(this.pictureBoxMargin * (i + 2) + this.pictureBoxMinSize.Width * i, this.panelSize.Height - (this.pictureBoxMargin + this.pictureBoxMinSize.Height)); } } } /// <summary> /// 鼠标进入 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void p_MouseEnter(object sender, EventArgs e) { EnterLeaveClass elc = null; PictureBox box = (PictureBox)sender; for (int i = 0; i < elcList.Count; i++) { if (this.elcList[i].pb.Tag == box.Tag) { elc = this.elcList[i]; break; } } elc.size = new Size(elc.pb.Size.Width, elc.pb.Size.Height); elc.point = new Point(elc.pb.Location.X, elc.pb.Location.Y); elc.strattime = DateTime.Now; elc.type = "in"; box.BringToFront(); if (!this.mouseEnterRun) { this.mouseEnterRun = true; } } /// <summary> /// 鼠标离开 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void p_MouseLeave(object sender, EventArgs e) { EnterLeaveClass elc = null; PictureBox box = (PictureBox)sender; for (int i = 0; i < elcList.Count; i++) { if (this.elcList[i].pb.Tag == box.Tag) { elc = this.elcList[i]; break; } } elc.size = new Size(elc.pb.Size.Width, elc.pb.Size.Height); elc.point = new Point(elc.pb.Location.X, elc.pb.Location.Y); elc.strattime = DateTime.Now; elc.type = "out"; if (!this.mouseLeaveRun) { this.mouseLeaveRun = true; } } /// <summary> /// 余数处理 /// </summary> /// <param name="remainder">存放余数累加值</param> /// <param name="value">计算带余数值</param> /// <param name="result">返回余数累加处理后的值</param> private static void setRemainder(ref double remainder, double value, ref int result) { result = (int)value; remainder += value % 1; if (remainder >= 1) { result += 1; remainder -= 1; } } /// <summary> /// 选项 /// </summary> public class EnterLeaveClass { /// <summary> /// 上一次w计算是否有余数 /// </summary> public double isyushu_w; /// <summary> /// 上一次h计算是否有余数 /// </summary> public double isyushu_h; /// <summary> /// 上一次x计算是否有余数 /// </summary> public double isyushu_x; /// <summary> /// 上一次y计算是否有余数 /// </summary> public double isyushu_y; /// <summary> /// 播放动画类型 /// </summary> public string type; /// <summary> /// 动画播放开始时间 /// </summary> public DateTime strattime; /// <summary> /// PictureBox /// </summary> public PictureBox pb; /// <summary> /// PictureBox当时大小 /// </summary> public Size size; /// <summary> /// PictureBox当时位置 /// </summary> public Point point; } }
垂直菜单动画:
public partial class Animation : FlowLayoutPanel { #region /// <summary> /// 鼠标进入动画播放定时器 /// </summary> private System.Timers.Timer mouseEnterTimer = new System.Timers.Timer(); /// <summary> /// 鼠标离开动画播放定时器 /// </summary> private System.Timers.Timer mouseLeaveTimer = new System.Timers.Timer(); /// <summary> /// elc列表 /// </summary> private List<EnterLeaveClass> elcList = new List<EnterLeaveClass>(); /// <summary> /// PictureBox选项 /// </summary> private List<PictureBox> carouselPictureBox = new List<PictureBox>(); /// <summary> /// 是否播放鼠标进入动画 /// </summary> private bool mouseEnterRun = false; /// <summary> /// 是否播放鼠标离开动画 /// </summary> private bool mouseLeaveRun = false; /// <summary> /// 容器大小 /// </summary> private Size panelSize; /// <summary> /// pictureBox默认大小 /// </summary> private Size pictureBoxMinSize; /// <summary> /// pictureBox动画后大小 /// </summary> private Size pictureBoxMaxSize; /// <summary> /// 选项高度 /// </summary> private int pictureBoxHeight = 60; #endregion #region private List<Image> carouselImages = new List<Image>(); /// <summary> /// 要播放的图片 /// </summary> [Description("要播放的图片")] public List<Image> CarouselImages { get { return carouselImages; } set { carouselImages = value; } } private double animationTime = 150; /// <summary> /// 动画播放的总时间 /// </summary> [DefaultValue(150)] [Description("动画播放的总时间(默认150毫秒)")] public double AnimationTime { get { return animationTime; } set { animationTime = value; } } #endregion public Animation() { InitializeComponent(); this.mouseEnterTimer.Interval = 10; this.mouseEnterTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.mouseEnterTimer_Elapsed); this.mouseLeaveTimer.Interval = 10; this.mouseLeaveTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.mouseLeaveTimer_Elapsed); this.mouseEnterTimer.Start(); this.mouseLeaveTimer.Start(); } /// <summary> /// 放大定时器 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void mouseEnterTimer_Elapsed(object sender, ElapsedEventArgs e) { if (!this.mouseEnterRun) return; EnterLeaveClass elc = (EnterLeaveClass)this.elcList.Where(a => a.type == "in").FirstOrDefault(); if (elc == null) return; double seconds = ((DateTime.Now - elc.strattime).TotalMilliseconds);//动画已运行时间 if (seconds > this.animationTime)//动画结束 { elc.type = ""; elc.isyushu_h = 0; this.BeginInvoke(new Action<EnterLeaveClass>(delegate(EnterLeaveClass obj) { elc.pb.Size = this.pictureBoxMaxSize; }), elc); this.mouseEnterRun = false; } else { int height = 0; setRemainder(ref elc.isyushu_h, TweenHelper.easeBoth((double)elc.size.Height, ((double)this.pictureBoxMaxSize.Height - (double)elc.size.Height), seconds, this.animationTime), ref height); this.BeginInvoke(new Action<EnterLeaveClass, int>(delegate(EnterLeaveClass obj, int _height) { obj.pb.Size = new Size(this.panelSize.Width, _height); }), elc, height); } } /// <summary> /// 缩小定时器 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void mouseLeaveTimer_Elapsed(object sender, ElapsedEventArgs e) { if (!this.mouseLeaveRun) return; List<EnterLeaveClass> itemlist = this.elcList.Where<EnterLeaveClass>(a => a.type == "out").ToList<EnterLeaveClass>(); for (int i = 0; i < itemlist.Count; i++) { double seconds = ((DateTime.Now - itemlist[i].strattime).TotalMilliseconds);//动画已运行时间 if (seconds > this.animationTime)//动画结束 { itemlist[i].type = ""; itemlist[i].isyushu_h = 0; int y = 0; if ((int)itemlist[i].pb.Tag > 0) { EnterLeaveClass p = (EnterLeaveClass)this.elcList.Where(a => (int)(a.pb.Tag) == (int)itemlist[i].pb.Tag - 1).FirstOrDefault(); y = p.pb.Location.Y + p.pb.Size.Height + 1; } this.BeginInvoke(new Action<EnterLeaveClass>(delegate(EnterLeaveClass obj) { obj.pb.Size = this.pictureBoxMinSize; }), itemlist[i]); if (this.elcList.Where<EnterLeaveClass>(a => a.type == "out").Count() < 1) { this.mouseEnterRun = false; } } else { int height = 0; setRemainder(ref itemlist[i].isyushu_h, TweenHelper.easeBoth(itemlist[i].size.Height, (this.pictureBoxMinSize.Height - itemlist[i].size.Height), seconds, this.animationTime), ref height); this.BeginInvoke(new Action<EnterLeaveClass, int>(delegate(EnterLeaveClass obj, int _height) { obj.pb.Size = new Size(this.panelSize.Width, _height); }), itemlist[i], height); } } } /// <summary> /// 加载PictureBox大小位置 /// </summary> public void LoadPictureBox() { this.Controls.Clear(); if (this.carouselImages.Count > 0) { this.panelSize = this.Size; this.pictureBoxMinSize = new Size(this.panelSize.Width, pictureBoxHeight); this.pictureBoxMaxSize = new Size(this.panelSize.Width, pictureBoxHeight * 2); for (int i = 0; i < carouselImages.Count; i++) { PictureBox box = new PictureBox() { SizeMode = PictureBoxSizeMode.StretchImage, Image = carouselImages[i], Tag = i, Padding = new Padding(0), Margin = new Padding(0) }; box.MouseEnter += new EventHandler(this.p_MouseEnter); box.MouseLeave += new EventHandler(this.p_MouseLeave); this.carouselPictureBox.Add(box); this.elcList.Add(new EnterLeaveClass() { pb = box }); this.Controls.Add(this.carouselPictureBox[i]); this.carouselPictureBox[i].Size = this.pictureBoxMinSize; } } } /// <summary> /// 鼠标进入 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void p_MouseEnter(object sender, EventArgs e) { EnterLeaveClass elc = null; PictureBox box = (PictureBox)sender; for (int i = 0; i < elcList.Count; i++) { if (this.elcList[i].pb.Tag == box.Tag) { elc = this.elcList[i]; break; } } elc.size = new Size(elc.pb.Size.Width, elc.pb.Size.Height); elc.strattime = DateTime.Now; elc.type = "in"; if (!this.mouseEnterRun) { this.mouseEnterRun = true; } } /// <summary> /// 鼠标离开 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void p_MouseLeave(object sender, EventArgs e) { EnterLeaveClass elc = null; PictureBox box = (PictureBox)sender; for (int i = 0; i < elcList.Count; i++) { if (this.elcList[i].pb.Tag == box.Tag) { elc = this.elcList[i]; break; } } elc.size = new Size(elc.pb.Size.Width, elc.pb.Size.Height); elc.strattime = DateTime.Now; elc.type = "out"; if (!this.mouseLeaveRun) { this.mouseLeaveRun = true; } } /// <summary> /// 余数处理 /// </summary> /// <param name="remainder">存放余数累加值</param> /// <param name="value">计算带余数值</param> /// <param name="result">返回余数累加处理后的值</param> private static void setRemainder(ref double remainder, double value, ref int result) { result = (int)value; remainder += value % 1; if (remainder >= 1) { result += 1; remainder -= 1; } } /// <summary> /// 选项 /// </summary> public class EnterLeaveClass { /// <summary> /// 上一次h计算是否有余数 /// </summary> public double isyushu_h; /// <summary> /// 播放动画类型 /// </summary> public string type; /// <summary> /// 动画播放开始时间 /// </summary> public DateTime strattime; /// <summary> /// PictureBox /// </summary> public PictureBox pb; /// <summary> /// PictureBox当时大小 /// </summary> public Size size; } }