c#中WinForm的TextBox循环自动滚动示例
2008-12-22 14:48
465 查看
这个问题来自论坛提问,演示代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication27
{
/**//// <summary>
/// 演示如何在TextBox中让文字循环滚动:
/// </summary>
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.textBox1.Clear();
for (int i = 0; i <= 20;i++ )
{
this.textBox1.Text += string.Format("{0}:jinjazz__{1} ", i,i);
}
this.timer1.Interval = 200;
this.timer1.Start();
}
//发送消息
[DllImport("user32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
//获取滚动条位置
[DllImport("user32")]
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)
{
int i= GetScrollPos(this.textBox1.Handle,1);
//向下滚动一行
SendMessage(this.textBox1.Handle, EM_LINESCROLL, 0, 1);//0,1代表垂直滚动条向下滚动
//判断是否有位置变化,如果没有则说明到了底部,返回开始处
if (i == GetScrollPos(this.textBox1.Handle, 1))
{
//回到顶部,这里用SetScrollPos似乎有问题,滚动条和文字不是同步更新
this.textBox1.SelectionStart = 0;
this.textBox1.SelectionLength = 1;
this.textBox1.ScrollToCaret();
this.textBox1.SelectionLength = 0;
}
Console.WriteLine(i);
}
private void textBox1_MouseEnter(object sender, EventArgs e)
{
this.timer1.Stop();
}
private void textBox1_MouseLeave(object sender, EventArgs e)
{
this.timer1.Start();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication27
{
/**//// <summary>
/// 演示如何在TextBox中让文字循环滚动:
/// </summary>
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.textBox1.Clear();
for (int i = 0; i <= 20;i++ )
{
this.textBox1.Text += string.Format("{0}:jinjazz__{1} ", i,i);
}
this.timer1.Interval = 200;
this.timer1.Start();
}
//发送消息
[DllImport("user32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
//获取滚动条位置
[DllImport("user32")]
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)
{
int i= GetScrollPos(this.textBox1.Handle,1);
//向下滚动一行
SendMessage(this.textBox1.Handle, EM_LINESCROLL, 0, 1);//0,1代表垂直滚动条向下滚动
//判断是否有位置变化,如果没有则说明到了底部,返回开始处
if (i == GetScrollPos(this.textBox1.Handle, 1))
{
//回到顶部,这里用SetScrollPos似乎有问题,滚动条和文字不是同步更新
this.textBox1.SelectionStart = 0;
this.textBox1.SelectionLength = 1;
this.textBox1.ScrollToCaret();
this.textBox1.SelectionLength = 0;
}
Console.WriteLine(i);
}
private void textBox1_MouseEnter(object sender, EventArgs e)
{
this.timer1.Stop();
}
private void textBox1_MouseLeave(object sender, EventArgs e)
{
this.timer1.Start();
}
}
}
相关文章推荐
- c#中WinForm的TextBox循环自动滚动示例
- c#中WinForm的TextBox循环自动滚动示例
- c#中WinForm的TextBox循环自动滚动示例
- c#中WinForm的TextBox循环自动滚动示例
- WinForm中TextBox控件循环自动滚动示例
- (转)c#中WinForm的TextBox循环自动滚动
- WPF C# TextBox自动滚动到最户一行
- C# winform中,动态绑定textbox,combox,datagridview的示例
- [WinForm] C#避免Panel(ScrollableControl)的子控件获取焦点时滚动条自动滚动
- C#之WinForm基础 给textbox空间添加水平,垂直滚动条
- vue实现动态添加数据滚动条自动滚动到底部的示例代码
- 在WinForm中使用Web Services 来实现 软件 自动升级( Auto Update ) (C#)
- [C#]Winform下回车或Tab键自动切换下一个控件焦点
- WPF TextBox自动滚动到最户一行
- html图片左右无缝循环滚动示例
- 循环为TextBox赋值 (asp.net C#)
- c# winform 全角自动转化半角问题(C#中ImeMode的值)
- Android自动滚动 轮播循环的ViewPager
- Js实现双击鼠标自动滚动屏幕的示例代码
- C# WinForm开发系列 - TextBox