C#的textbox的自动滚屏

来源:互联网 发布:辐射4艾达王捏脸数据 编辑:程序博客网 时间:2024/05/17 07:53
 

在一个窗口中实现:

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 AutomaticText
{
    public partial class Form1 : Form
    {
        private int min, max;
        private int pos = 0;
        private int endPos = 0;
        private const int SB_HORZ = 0x0;
        private const int SB_VERT = 0x1;
        private const int WM_HSCROLL = 0x114;
        private const int WM_VSCROLL = 0x115;
        private const int SB_THUMBPOSITION = 4;
        [DllImport("user32.dll")]
        private static extern int SetScrollPos(IntPtr hwnd, int nBar, int nPos, bool bRedraw);

        [DllImport("user32.dll")]
        private static extern int GetScrollPos(IntPtr hwnd, int nBar);
        [DllImport("user32.dll")]
        private static extern bool PostMessage(IntPtr hWnd, int nBar, int wParam, int lParam);
        [DllImport("user32", CharSet = CharSet.Auto)]
        private static extern bool GetScrollRange(IntPtr hWnd, int nBar, out int lpMinPos, out int lpMaxPos);

        public Form1()
        {
            InitializeComponent();
        }

        private void rt_DoubleClick(object sender, EventArgs e)
        {
            timeAutoScroll.Interval = 100;
            //得到滚动条的最大最小值  
            GetScrollRange(rt.Handle, SB_VERT, out min, out max);
            //得到滚动条到最底下的实际位置  
            endPos = max - rt.ClientRectangle.Height;
            this.timeAutoScroll.Enabled = true;
        }

        private void timeAutoScroll_Tick(object sender, EventArgs e)
        {
            pos = GetScrollPos(rt.Handle, SB_VERT);//加上这句是为了如果用户手动拖拽滚动条,可以保证滚动条继续从拖拽的位置走  
            pos++;
            //如果已经到底,那么从头开始  
            if (pos > endPos)
            {
                pos = 0;
            }
            SetScrollPos(rt.Handle, SB_VERT, pos, true);
            PostMessage(rt.Handle, WM_VSCROLL, SB_THUMBPOSITION + 0x10000 * pos, 0);

        }

        private void btn_okcancel_Click(object sender, EventArgs e)
        {
            if(txt_span .Text .Length !=0)
            timeAutoScroll.Interval = Convert .ToInt32 (txt_span .Text .Trim ());
            //得到滚动条的最大最小值  
            GetScrollRange(rt.Handle, SB_VERT, out min, out max);
            //得到滚动条到最底下的实际位置  
            endPos = max - rt.ClientRectangle.Height;
            this.timeAutoScroll.Enabled = true;
        }
        [DllImport("Dll.dll")]
        private static extern void  m();
        //private static extern void Me();
        private void btn_shezhi_Click(object sender, EventArgs e)
        {
            timeAutoScroll.Enabled = false;
          
           
           
        }       
    }
}

 

//textbox左右滚动字幕
        private void timer1_Tick(object sender, EventArgs e)
        {
            int i = strNum;
            //不存在的话
            if (message == null)
            {
                lab_scroll.Text = "";
                return;
            }
            string messageStr = "              " + message;
            int leng = messageStr.Length;
            if (leng >= 25 + i)
            {
                lab_scroll.Text = messageStr.Substring(i, 25);
            }
            else
            {
                if (leng > i)
                {
                    lab_scroll.Text = messageStr.Substring(i, leng - i);
                }
            }
            strNum++;
            if (strNum > leng)
            {
                strNum = 0;
            }
        }

原创粉丝点击