在C# WinForm 轻松实现滚动字幕特效的关于窗体
原理很简单,Timer与集合、Label配合就可以轻松实现。
截图(记得在一些共享软件注册机上也看到这样的效果):

看看下面的C#源码就知道了:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Text;
- using System.Windows.Forms;
- using System.Diagnostics;
- namespace CodingMouse.CMCSharpSDK.UI.Forms
- {
- /// <summary>
- /// 关于本软件界面
- /// </summary>
- public partial class frmAbout : Form
- {
- #region Private Members
- List<Label> lblTxt = new List<Label>();
- List<Label> lblScroll = new List<Label>();
- int index = 0;
- /// <summary>
- /// 保存窗体旧坐标的X轴值和Y轴值
- /// </summary>
- int _x, _y;
- /// <summary>
- /// 保存窗体是否可移动标识
- /// </summary>
- bool isMove = false;
- /// <summary>
- /// 保存鼠标指针指向的坐标点
- /// </summary>
- Point _mouseLocation;
- #endregion
- #region Public Methods
- /// <summary>
- /// 无参构造
- /// </summary>
- public frmAbout()
- {
- // 构建设计器控件
- InitializeComponent();
- for (int i = 0; i < 13; i++)
- {
- Label lbl = new Label();
- lblTxt.Add(lbl);
- lblTxt[i].Top = this.Height - 24;
- lblTxt[i].Left = 24;
- lblTxt[i].Visible = false;
- lblTxt[i].BackColor = Color.Transparent;
- lblTxt[i].ForeColor = Color.White;
- lblTxt[i].AutoSize = true;
- this.Controls.Add(lblTxt[i]);
- }
- lblTxt[0].Text = "产品名称: CMCSharpSDK";
- lblTxt[1].Text = "产品全称: CodingMouse C# 开发工具包";
- lblTxt[2].Text = "当前版本: 1.0.0.0";
- lblTxt[3].Text = "";
- lblTxt[4].Text = "程式设计: 邓超 (网络用名: CodingMouse)";
- lblTxt[5].Text = "";
- lblTxt[6].Text = " Qicq: 454811990";
- lblTxt[7].Text = " E-mail: CodingMouse@gmail.com";
- lblTxt[8].Text = " Blog: http://blog.csdn.net/CodingMouse";
- lblTxt[9].Text = "";
- lblTxt[10].Text = "测试环境: Microsoft Windows Server 2003 Enterprise Edition";
- lblTxt[11].Text = " Microsoft Visual Studio 2005 Team Suite";
- lblTxt[12].Text = " Microsoft SQL Server 2005 Enterprise Edition";
- // 添加事件监听
- lblTxt[7].Click += new EventHandler(frmAbout_Email_Click);
- lblTxt[8].Click += new EventHandler(frmAbout_WebSite_Click);
- lblTxt[7].MouseMove += new MouseEventHandler(frmAbout_Link_MouseMove);
- lblTxt[8].MouseMove += new MouseEventHandler(frmAbout_Link_MouseMove);
- lblTxt[7].MouseLeave += new EventHandler(frmAbout_Link_MouseLeave);
- lblTxt[8].MouseLeave += new EventHandler(frmAbout_Link_MouseLeave);
- lblScroll.Add(lblTxt[0]);
- }
- #endregion
- #region Event Handlers
- /// <summary>
- /// 链接标签鼠标移动事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void frmAbout_Link_MouseMove(object sender, MouseEventArgs e)
- {
- Label currentLabel = (sender as Label);
- currentLabel.ForeColor = Color.Yellow;
- currentLabel.Cursor = Cursors.Hand;
- }
- /// <summary>
- /// 链接标签鼠标移开事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void frmAbout_Link_MouseLeave(object sender, EventArgs e)
- {
- Label currentLabel = (sender as Label);
- currentLabel.ForeColor = Color.White;
- currentLabel.Cursor = Cursors.Default;
- }
- /// <summary>
- /// [E-mail]滚动标签点击事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void frmAbout_Email_Click(object sender, EventArgs e)
- {
- Process.Start("mailto:CodingMouse@gmail.com");
- }
- /// <summary>
- /// [Blog]滚动标签点击事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void frmAbout_WebSite_Click(object sender, EventArgs e)
- {
- Process.Start("http://blog.csdn.net/CodingMouse");
- }
- /// <summary>
- /// 计时器事件(调度信息字幕显示)
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void tmrShow_Tick(object sender, EventArgs e)
- {
- // 控制字幕滚动
- for (int i = 0; i < lblScroll.Count; i++)
- {
- lblScroll[i].Visible = true;
- lblScroll[i].Top -= 1;
- if (lblScroll[i] == lblTxt[(lblTxt.Count - 1)]
- && lblScroll[i].Top == this.Height - 84)
- {
- index = 0;
- lblScroll.Add(lblTxt[0]);
- }
- if (lblScroll[i].Top < (lblTxt.Count - 1))
- {
- lblScroll[i].Visible = false;
- lblScroll[i].Top = this.Height - (lblTxt.Count - 1) * 2;
- lblScroll.RemoveAt(i);
- }
- }
- if (index < (lblTxt.Count - 1))
- {
- if ((lblTxt[index].Top + lblTxt[index].Height) - lblTxt[index + 1].Top
- == -(lblTxt.Count - 1))
- {
- lblScroll.Add(lblTxt[index + 1]);
- index++;
- }
- }
- }
- /// <summary>
- /// 窗体双击事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void frmAbout_DoubleClick(object sender, EventArgs e)
- {
- this.Close();
- }
- /// <summary>
- /// 鼠标指针在窗体上方并按下按键事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void frmAbout_MouseDown(object sender, MouseEventArgs e)
- {
- // 仅响应鼠标左键点击事件
- if (e.Button == MouseButtons.Left)
- {
- // 保存旧坐标
- this._x = e.X;
- this._y = e.Y;
- // 标识窗体可移动
- this.isMove = true;
- }
- }
- /// <summary>
- /// 鼠标指针在窗体上方并移动鼠标指针事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void frmAbout_MouseMove(object sender, MouseEventArgs e)
- {
- // 如果可移动
- if (this.isMove)
- {
- // 根据旧坐标的相对偏移位置移动窗体
- // 方法一:
- // this.Left += e.X - this._x;
- // this.Top += e.Y - this._y;
- // 方法二:
- this.SetDesktopLocation(this.Left + e.X - this._x, this.Top + e.Y - this._y);
- }
- // 保存鼠标指针坐标点
- _mouseLocation = e.Location;
- }
- /// <summary>
- /// 鼠标指针在窗体上方并释放按键事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void frmAbout_MouseUp(object sender, MouseEventArgs e)
- {
- // 标识窗体不可移动
- this.isMove = false;
- }
- /// <summary>
- /// 鼠标指针在窗体上移过并保持静止状态一段时间事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void frmAbout_MouseHover(object sender, EventArgs e)
- {
- this.toolTip.ToolTipIcon = ToolTipIcon.Info;
- this.toolTip.ToolTipTitle = "关闭提示";
- this.toolTip.Show(
- "请双击鼠标键来关闭此窗体 ...",
- this, new Point(_mouseLocation.X + 6, _mouseLocation.Y + 6),
- 5000);
- }
- #endregion
- }
- }
2008年12月22日
By CodingMouse
这是一个使用C# WinForm创建关于窗体的示例,包含滚动字幕特效,展示了如何在窗体中展示产品信息,并实现邮件和网站链接的点击事件,以及窗体的拖动功能。
2744

被折叠的 条评论
为什么被折叠?



