C#WinForm的TextBox自动循环滚动(转)
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 WindowsApplication1
{
    public partial class Form1 : Form
    {
        //发送消息
        [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")]
        public static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);
        public const int EM_LINESCROLL = 0xb6;
        public Form1()
        {
            InitializeComponent();
            this.textBox1.Clear();
            for (int i = 0; i <= 100; i++)
            {
                this.textBox1.Text += string.Format("{0}:zhyleo__{1} 	", i, i);
            }
            this.textBox1.SelectionStart = 0;
            this.timer1.Interval = 100;
            this.timer1.Start();
        }
        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;
            }
        }
        private void textBox1_MouseEnter(object sender, EventArgs e)
        {
            this.timer1.Stop();
        }
        private void textBox1_MouseLeave(object sender, EventArgs e)
        {
            this.timer1.Start();
        }
    }
}
