GDI+Visual Studio .NET 2002Visual Studio .NET 2003.NET 1.1Visual Studio 2005.NET 2.0Windows Forms中级开发Visual StudioWindows.NETC#
滚动字幕控件






4.12/5 (11投票s)
2005 年 6 月 15 日
2分钟阅读

70384

1568
一个带平滑、无闪烁移动和鼠标干预功能的滚动文本和图像控件。
引言
这是我第一次提交到 CodeProject,因为多年来我一直把我的所有优秀控件留给自己。 最近我改变了主意,认为我应该分享我的代码。
我最近在 CodeProject 上看到一个滚动框控件,我认为我可以改进它的性能,所以这是我对它的演绎。
使用代码
您可以将滚动文本/图像的对齐方式设置为靠前
/居中
/靠后
。 可以对控件的左侧和右侧应用填充(顶部和底部未使用)。
通过创建新的ScrollingBoxText
或ScrollingBoxImage
(从ScrollingBoxItem
继承)来添加文本或图像,通过Items属性。
scrollingBox1.Alignment = StringAlignment.Center;
scrollingBox1.Padding = new Padding(10, 10, 0, 0);
scrollingBox1.Items.Add(new
ScrollingBox.ScrollingBoxText("Hello this is a test"));
scrollingBox1.Items.Add(new
ScrollingBox.ScrollingBoxText("Hello this also is a test"));
scrollingBox1.Items.Add(new
ScrollingBox.ScrollingBoxImage(Image.FromFile("C:\\image.gif")));
scrollingBox1.Items.Add(new
ScrollingBox.ScrollingBoxText("Try having a long string to test wrapping"));
scrollingBox1.Items.Add(new
ScrollingBox.ScrollingBoxText("You can use new line switch\nHello"));
此控件的附加功能是能够用鼠标向后或向前滚动,尽管使用起来有点笨拙。
您还可以更改字体、背景颜色,甚至可以使用背景图像。
关注点
首先,我用 Visual Studio 2005 Beta 2 编写了这个程序,我想我会被打低分并受到严厉的批评。 好吧,降级到较低版本非常简单,所以如果你需要更早的版本,请发表评论,我将不得不转换它。
由于此控件将不断在界面上更新,我们必须将闪烁降到最低。 我通过三种方式做到这一点。
- 确保我为控件设置了正确的样式。
SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true); //ControlStyles.DoubleBuffer if not using .NET v2.0
- 仅更新需要更新的控件区域,方法是使用剪辑(实际上我在此控件中尚未执行此操作)并检查需要更新控件的位置。
if (clipRectF.IntersectsWith(item.rectF))
- 在
OnPaint
函数中编写完成工作所需的最少量的代码,确保所有计算都在其他地方完成。
已知问题
- 鼠标实际上并没有控制上下移动,我只是在鼠标移动时调用计算文本/图像的函数。
- 不支持添加文本或图像的设计。
历史
- 以上传为 Visual Studio 2005 Beta 2 解决方案 - 2005 年 6 月 15 日。
- 以上传为 Visual Studio 2003 解决方案 - 2005 年 6 月 21 日。