图像幻灯片控件






4.96/5 (21投票s)
一个 Windows Forms 控件,
引言
作为一名Web开发者,我经常使用各种图像轮播插件来轮换图片。由于有各种各样的插件可供选择,我从来不需要自己开发一个。然而,在最近开发一个Windows Forms应用程序时,我无法找到类似的控件,或者至少是具有我想要的一些功能的控件。不想花太多时间搜索,我决定自己开发一个。
控件的工作原理
ImageSlider
控件继承自Panel
控件,并且重写了它的OnPaint
方法,以便可以在Panel
的表面绘制图像。图像和标题在内部维护在一个类型为string
和Image
的List
集合中。使用List
集合可以很容易地使用索引访问一个元素。ImageSlider
导航按钮(如上图所示),只是简单地递增或递减一个内部索引。每次单击导航按钮时,都会调用Panel
的Invalidate()
方法,这将执行OnPaint()
方法。OnPaint
方法使用新的索引值从各自的集合中查找图像路径和标题,并加载新的图像和标题。
此外,OnPaint()
方法负责使用Timer
来动画标题文本。当单击导航按钮时启动计时器,并在动画完成时释放计时器。
下面是方法和属性的列表。
属性
CaptionHeight
- 设置标题区域的高度CaptionTextLeft
- 设置标题文本相对于标题区域的左侧位置CaptionBackgrounColor
- 设置标题背景颜色CaptionOpacity
- 设置标题背景透明度CaptionAnimationSpeed
- 设置标题文本的滚动速度Animation
- 布尔值,用于开启或关闭动画LeftButton
- 返回左侧导航按钮,以便可以配置按钮的属性RightButton
- 返回右侧导航按钮,以便可以配置按钮的属性
方法
AddImage(string path)
AddImage(string path, string caption)
AddImage(string path, string caption, Color captionBackgroundColor)
AddImage(Image path)
AddImage(Image path, string caption)
AddImage(Image path, string caption, Color captionBackgroundColor)
Using the Code
创建一个ImageSlider
控件的新实例。使用AddImage
方法将图像添加到ImageSlider
。AddImage()
方法有三个重载。您可以添加没有标题的图像,也可以为图像设置标题。第三个参数允许您设置标题区域的背景颜色。
imageSlider1.AddImage("Chrysanthemum.jpg",
"A caption for the image goes here", Color.Maroon);
imageSlider1.AddImage("Desert.jpg", "What an amazing photo.", Color.Gold);
imageSlider1.AddImage("Hydrangeas.jpg", "For me?...blush blush", Color.LimeGreen);
imageSlider1.AddImage("Jellyfish.jpg",
"So... what sort of fish are you again?", Color.Orange);
imageSlider1.AddImage("Koala.jpg", "Will you be my friend?", Color.Gray);
imageSlider1.AddImage("Lighthouse.jpg", "Must be a great view from up there");
imageSlider1.AddImage("Penguins.jpg", "Fish anyone?", Color.Navy);
imageSlider1.AddImage(Image.FromFile("Tulips.jpg"),
"You cant go wrong with tulips", Color.LightSkyBlue);
这标志着本文的结束。 请随时留下您的评论和建议。
历史
- 2014年6月4日:初始版本