C#中实现label中文字循环滚动

东方旭东
2023-12-01
首先放一个timer控件,让其Interval属性为300,enable属性为true
然后拖一个Label,ID为lblMessage,Text写欢迎光临.
准备工作做好了,接下来在timer的timer_Tick事件里写:
int left = lblMessage.Left;//获得该Label距离窗体左边缘的距离
int right=lblMessage.Right;//获得Label距离窗体右边缘的距离
left = left - 3;//每执行一次timer_Tick距离窗体的左距离减3像素,这个操作只是控制像左移动的幅度,可以更改
if (right <= 0)//如果右边的距离为0,也就是Label完全退出窗体左边的时候,设置左边距为窗体宽度,此时Label会从右边出来
{
left = this.Width;
}
lblMessage.Left = left;//把每次左边距变化后的值设置给左边距属性,以使Label改变,达到滚动的效果
把你要显示的字符串赋给lblMessage.Text就可以了

Label中文字左右滚动
这个是自己拼起来来的,也不知道怎么简单,就写成这样了,不过可以实现在label中左右滚动

在这之前要定义一个用来控制滚动的线程(可以改一下方法为传入参数,参数是文字滚动的label)
public void Roll()
        {
            string a = "左边";
            do
            {
                if (a == "左边")
                {
                    do
                    {
                        roll++;
                        this.lblrollfont.Location = new System.Drawing.Point(roll, 10);
                        if (this.lblrollfont.Location.X + 1 == 349)          
                        //349是lable的x座标(就是移到要换方向滚动向右边时的X座标)
                        {
                            a = "右边";
                            break;
                        }
                        Thread.Sleep(10);
                    } while (a == "左边");
                }
                if (a == "右边")
                {
                    do
                    {
                        roll--;
                        this.lblrollfont.Location = new System.Drawing.Point(roll, 10);
                        if (this.lblrollfont.Location.X + 1 == 0)
                        {
                            a = "左边";
                            break;
                        }
                        Thread.Sleep(10);
                    } while (1 == 1);
                }
            } while (1 == 1);     
        }



转自:http://blog.csdn.net/watershuimiao/article/details/5722540

 类似资料: