C#的label文字标签左右滚动公告

项目需要在页面增加一行文字滚动的公告 网上没找到合适的 反正需求不大所以自己写了一个简单的,有注释看一下应该就懂了,调用方法前需要创建一个label标签并设置好内容 代码创建和拖控件都可以,方法只需要传入label的对象就行.

  public static void setTips(Label obj)
        {
            //初始化对象从右侧开始
            obj.Location = new Point(obj.Parent.Width, obj.Location.Y);
            Timer tipsTimer = new Timer();
            tipsTimer.Enabled = true;
            tipsTimer.Interval = 100;
            //鼠标放上去停止滚动
            obj.MouseMove += new MouseEventHandler((s, x) =>
            {
                tipsTimer.Enabled = false;
            });
            //鼠标离开继续滚动
            obj.MouseLeave += new EventHandler((s, x) =>
            {
                tipsTimer.Enabled = true;
            });
            //滚动
            tipsTimer.Tick += new EventHandler((s, x) =>
            {
                //如果当前标签定位已经完全滚动完了就恢复到右侧重新滚动
                if (obj.Location.X <= (obj.Width - (obj.Width * 2)))
                {
                    obj.Location = new Point(obj.Parent.Width, obj.Location.Y);
                }
                else
                {
                    obj.Location = new Point(obj.Location.X - 10, obj.Location.Y);

                }
            });
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章