[C.C++] C#实现跑马灯效果的示例代码

3237 0
黑夜隐士 2022-11-5 09:19:17 | 显示全部楼层 |阅读模式
目录

    文章描述开发环境开发工具实现代码实现效果


文章描述

跑马灯效果,功能效果大家应该都知道,就是当我们的文字过长,整个页面放不下的时候(一般用于公告等),可以让它自动实现来回滚动,以让客户可以看到完整的信息(虽然要多等一会儿时间)。
其实对于Winform这种技术,实现任何的动态效果相对来说都比较麻烦。而且一般都需要搭配定时器使用,当然,这次要写的跑马灯效果也是一样的,使用了System.Timers.Timer来实现,关于其他定时器以及用法,之前文章有写过,有兴趣的可以翻一下。
因为使用麻烦,所以要进行封装,所以要不断的造轮子(尽管是重复的),但重复也是一个加强记忆以及不断深入的过程,我认为这并不是多余的。因此,为了方便调用,还是用自定义控件封装一下属性,使用的时候只要设置属性即可。



开发环境

.NET Framework版本:4.5

开发工具

Visual Studio 2013

实现代码
  1. public partial class CustomLable : Label
  2.     {
  3.         System.Timers.Timer timer = new System.Timers.Timer(200);
  4.         int offset = 5;//偏移量
  5.         PointF textPoint;
  6.         public CustomLable()
  7.         {
  8.             InitializeComponent();
  9.             textPoint = new PointF(this.Width, 0);
  10.             timer.Elapsed += (s, e) =>
  11.             {
  12.                 try
  13.                 {
  14.                     if (!IsDisposed)
  15.                     {
  16.                         Graphics g = CreateGraphics();
  17.                         SizeF textSize = g.MeasureString(Text, Font);
  18.                         textPoint.X -= offset;
  19.                         if (textPoint.X <= -textSize.Width)
  20.                         {
  21.                             textPoint.X = Width;
  22.                         }
  23.                         g.Clear(BackColor);
  24.                         g.DrawString(Text,Font, new SolidBrush(ForeColor), textPoint);
  25.                     }
  26.                 }
  27.                 catch { }
  28.             };
  29.         }
  30.         protected override void OnPaint(PaintEventArgs pe)
  31.         {
  32.             base.OnPaint(pe);
  33.         }
  34.         private bool _IsMarquee;
  35.         [Browsable(true)]
  36.         [Description("是否以跑马灯效果显示")]
  37.         public bool IsMarquee
  38.         {
  39.             get { return _IsMarquee; }
  40.             set
  41.             {
  42.                 _IsMarquee = value;
  43.                 Marquee();
  44.             }
  45.         }
  46.         public void Marquee()
  47.         {
  48.             if (IsMarquee)
  49.             {
  50.                 timer.Start();
  51.             }
  52.             else
  53.             {
  54.                 timer.Stop();
  55.                 textPoint = new PointF(0, 0);
  56.                 try
  57.                 {
  58.                     if (!IsDisposed)
  59.                     {
  60.                         Graphics g = CreateGraphics();
  61.                         g.Clear(BackColor);
  62.                         g.DrawString(Text, Font, new SolidBrush(ForeColor), textPoint);
  63.                     }
  64.                 }
  65.             }
  66.         }
  67.     }
复制代码
  1. private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             customLable1.IsMarquee = !customLable1.IsMarquee;
  4.         }
复制代码
实现效果



代码解析:由于我们直接是在IsMarquee的set属性中就调用了Timer事件;所以即便不运行,在设计窗体时改变属性就可以直接看到效果。
到此这篇关于C#实现跑马灯效果的示例代码的文章就介绍到这了,更多相关C#跑马灯内容请搜索中国红客联盟以前的文章或继续浏览下面的相关文章希望大家以后多多支持中国红客联盟!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2026 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行