c#怎么让textbox里的多行文字实现自动上下滚动播出,谢谢

2025-04-14 14:17:12
推荐回答(3个)
回答1:

在窗体上添加txtBox,设置为multiline,添加timer控件,然后:

public Form1()
        {
            InitializeComponent();
             this.timer1.Interval = 1000;//以毫秒为单位
            this.timer1.Start();
        }
        
        // 发送消息 
        [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;

        /// 
        /// 定时器
        /// 

        /// object
        /// EventArgs
        private void timer1_Tick(object sender, EventArgs e)
        {
            int i = GetScrollPos(this.txtBoxMultiLine.Handle, 1);

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

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

回答2:

位置属性 y=y+n timer控制

回答3:

自上而下自动滚动?