本文實例講述了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#程序設計有所幫助。
新聞熱點
疑難解答