自定义Image自动切换图像控件

 做这么一个控件,图片自动切换,形成动画效果。

自定义Image自动切换图像控件

 随便的码码,码完发现东西太少了,不过还算完善。

 public class MyPictureBox : PictureBox
{ Timer timer = new Timer (); private int curPicIndex = ; private bool run = false; public bool Run
{
get { return run; }
set
{
run = value;
if (value)
timer.Start();
else
timer.Stop();
}
} private List<Image> imgList = new List<Image>(); private string[] imagepaths = null; public string[] ImagePaths
{
get { return imagepaths; }
set
{
imagepaths = value; if (imagepaths == null || imagepaths.Length == )
return; imgList.Clear(); foreach (string v in imagepaths)
{
try
{
imgList.Add(Image.FromFile(v));
}
catch
{ }
}
}
} //private map private int autoTimeInterval = ; public int AutoTimeInterval
{
get { return autoTimeInterval; }
set
{
autoTimeInterval = value;
if (value > && value < )
timer.Interval = value;
}
} public MyPictureBox()
{
base.SizeMode = PictureBoxSizeMode.StretchImage; timer = new Timer();
timer.Interval = autoTimeInterval;
timer.Tick += (s, e) =>
{
timer.Stop(); if (imgList != null && imgList.Count > )
{
int imgcount = imgList.Count;
if (imgcount > )
{
curPicIndex = (curPicIndex + ) % imgcount;
base.Image = imgList[curPicIndex];
} timer.Start();
}
};
}
}

MyPictureBox

自定义Image自动切换图像控件 

控件写好了,只有把它放到窗体上才能正常使用。

在一个form窗体里这样写:

  //loadingPic : 加载本地图片
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.ValidateNames = true;
dlg.Multiselect = true;
dlg.ShowDialog();
this.myPictureBox1.ImagePaths = dlg.FileNames;
} //stop : 图片停止切换
private void button2_Click(object sender, EventArgs e)
{
this.myPictureBox1.Run = false;
} //run : 图片开启切换
private void button3_Click(object sender, EventArgs e)
{
this.myPictureBox1.Run = true;
} // 设置图片切换的时间间隔
private void trackBar1_ValueChanged(object sender, EventArgs e)
{
this.myPictureBox1 .AutoTimeInterval = this.trackBar1.Value * ;
}

Form窗体内事件

  代码很简单,但是这种模式用的地方很多,比如,自己做一个相册,可以自动切换照片;或者做一个简单的小游戏的时候,控制人物的行为,行走啊,战斗时从出手到战斗结束回归原状态的一系列动作啊等等(其实就是一帧一帧的图片连续播放,和flash一样的);或者是做水流的效果图(两三张就够了,水面的纹理发生改变时,就认为水是动的了),也可以说能想象到的一切简单的动态效果或者行为,都可以用Image控件实现。

比较简单,就这么多了。

最牛的工程,也是通过简单的模块堆积出来的,呵呵。

随手打,未仔细检查,如果有什么疑难杂症,见谅!


  到了我这个年龄,已经进入特殊状态了:工作、女朋友,两手抓,两手都要硬,一个都不能放弃。本想偷空的时候好好码一篇,结果还是没时间,毕竟这么大了,也该找个伴了,有句话不是这么说嘛:时间要用在刀刃上。呵呵,我胡扯。

上一篇:JDK1.7中HashMap底层实现原理


下一篇:c – 如何在Linux中使用COM ATL项目?