c#之上下滚动播放

来源:互联网 发布:最新营销软件 编辑:程序博客网 时间:2024/05/10 15:30
一、功能需求
添加曾经对于XXX系统作出贡献的人和合作伙伴,要求动态播放。
二、实现步骤
1,新增textBox并修改属性
主要修改点如下:
multiline 修改为true --可以跨行
scrollBars 修改为 Both--显示滚动条


2,新增timer

3,添加代码和方法
在初始化窗体处添加如下代码:
this.timer1.Interval = 1000;//播放速度
this.timer1.Enabled = true;
this.timer1.Start();
双击timer1控件新增timer1_Tick方法,代码如下
// 发送消息
[DllImport("user32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
// 获取滚动条位置
[DllImport("user32.dll")]
public static extern int GetScrollPos(IntPtr hWnd, int nBar);
// 设置滚动条位置
[DllImport("user32.dll")]
static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);

public const int EM_LINESCROLL = 0xb6;

private void timer1_Tick(object sender, EventArgs e)
{
//this.txtInfo.Top -= 5;
//txtInfo.SelectionStart = txtInfo.Text.Length;
//txtInfo.ScrollToCaret();
int i = GetScrollPos(this.txtInfo.Handle, 1);

// 向下滚动一行
SendMessage(this.txtInfo.Handle, EM_LINESCROLL, 0, 1); // 0,1代表垂直滚动条向下滚动

// 判断是否有位置变化,如果没有则说明到了底部,返回开始处
if (i == GetScrollPos(this.txtInfo.Handle, 1))
{
// 回到顶部,这里用SetScrollPos似乎有问题,滚动条和文字不是同步更新
this.txtInfo.SelectionStart = 0;
this.txtInfo.SelectionLength = 1;
this.txtInfo.ScrollToCaret();
this.txtInfo.SelectionLength = 0;
}
Console.WriteLine(i);
}


原创粉丝点击