WinForm实现仿视频播放器左下角滚动新闻效果的方法
本文实例讲述了WinForm实现仿视频播放器左下角滚动新闻效果的方法。分享给大家供大家参考。具体实现方法如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing.Imaging; using System.IO; using System.Xml.Linq; namespace App { public partial class Form7 : Form { private int Index { get; set; } private DataTable dataTable { get; set; } private System.Timers.Timer timer { get; set; } public Form7() { InitializeComponent(); dataTable = new DataTable(); dataTable.Columns.AddRange(new DataColumn[] { new DataColumn("Id",typeof(System.Int32)), new DataColumn("DisplayName",typeof(System.String)), new DataColumn("URL",typeof(System.String)) }); DataRow row = dataTable.NewRow(); row["Id"] = 1; row["DisplayName"] = "百度百科欢迎你!"; row["URL"] = "http://www.baidu.com"; dataTable.Rows.Add(row); row = dataTable.NewRow(); row["Id"] = 2; row["DisplayName"] = "刘XX明星已经现身搜狐网站,赶快去围观!"; row["URL"] = "http://www.sohu.com"; dataTable.Rows.Add(row); row = dataTable.NewRow(); row["Id"] = 3; row["DisplayName"] = "新浪新闻出现重大新闻,点我快速查看!"; row["URL"] = "http://www.sina.com.cn"; dataTable.Rows.Add(row); row = dataTable.NewRow(); row["Id"] = 4; row["DisplayName"] = "网易客户端出现新版本啦,赶快去体验把!"; row["URL"] = "http://www.126.com"; dataTable.Rows.Add(row); label1.Text = lblContent.Top.ToString(); timer = new System.Timers.Timer(2000); timer.Elapsed += delegate { CharsMouse(); }; timer.Start(); button1.Click += delegate { CharsMouse(); }; button2.Click += delegate { timer.Dispose(); }; lblContent.LinkClicked += delegate { label2.Text = dataTable.Rows[Index]["URL"].ToString(); }; } void CharsMouse() { this.lblContent.Invoke(new MethodInvoker(delegate { if (Index >= dataTable.Rows.Count) Index = 0; while (lblContent.Top > -lblContent.Height) { lblContent.Top = lblContent.Top - 2; label1.Text = "1.Top=" + lblContent.Top.ToString(); System.Threading.Thread.Sleep(100); Application.DoEvents(); } lblContent.Text = dataTable.Rows[Index]["DisplayName"].ToString(); lblContent.Top = 2; timer.Enabled = false; Application.DoEvents(); System.Threading.Thread.Sleep(2000); timer.Enabled = true; Index++; })); } } }
希望本文所述对大家的C#程序设计有所帮助。
赞 (0)