richtextbox内文字自动滚动的例子

通过API 来控制richtextbox内文字自动滚动的例子,

二话不说,上代码

   public partial class Form1 : Form
    {
        [DllImport(
"user32.dll")]
        [
return: MarshalAs(UnmanagedType.Bool)]
        
static extern bool GetScrollInfo(IntPtr hwnd, int fnBar, ref SCROLLINFO lpsi);

        [DllImport(
"user32.dll")]
        
static extern int SetScrollInfo(IntPtr hwnd, int fnBar, [In] ref SCROLLINFO lpsi, bool fRedraw);

        [DllImport(
"User32.dll", CharSet = CharSet.Auto, EntryPoint = "SendMessage")]
        
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        
struct SCROLLINFO
        {
            
public uint cbSize;
            
public uint fMask;
            
public int nMin;
            
public int nMax;
            
public uint nPage;
            
public int nPos;
            
public int nTrackPos;
        }

        
enum ScrollBarDirection
        {
            SB_HORZ 
= 0,
            SB_VERT 
= 1,
            SB_CTL 
= 2,
            SB_BOTH 
= 3
        }

        
enum ScrollInfoMask
        {
            SIF_RANGE 
= 0x1,
            SIF_PAGE 
= 0x2,
            SIF_POS 
= 0x4,
            SIF_DISABLENOSCROLL 
= 0x8,
            SIF_TRACKPOS 
= 0x10,
            SIF_ALL 
= SIF_RANGE + SIF_PAGE + SIF_POS + SIF_TRACKPOS
        }

        
const int WM_VSCROLL = 277;
        
const int SB_LINEUP = 0;
        
const int SB_LINEDOWN = 1;
        
const int SB_THUMBPOSITION = 4;
        
const int SB_THUMBTRACK = 5;
        
const int SB_TOP = 6;
        
const int SB_BOTTOM = 7;
        
const int SB_ENDSCROLL = 8;

        
private Timer t = new Timer();        
        
        
public Form1()
        {
            InitializeComponent();
                        
            t.Interval 
= 30;
            t.Tick 
+= new EventHandler(t_Tick);
            t.Enabled 
= true;
        }

        
void t_Tick(object sender, EventArgs e)
        {            
            scroll(richTextBox.Handle, 
1);
        }

        
// Scrolls a given textbox. handle: an handle to our textbox. pixels: number of pixels to scroll.
        void scroll(IntPtr handle, int pixels)
        {
            IntPtr ptrLparam 
= new IntPtr(0);
            IntPtr ptrWparam;
            
// Get current scroller posion
            
            SCROLLINFO si 
= new SCROLLINFO();
            si.cbSize 
= (uint)Marshal.SizeOf(si);
            si.fMask 
= (uint)ScrollInfoMask.SIF_ALL;
            GetScrollInfo(handle, (
int)ScrollBarDirection.SB_VERT, ref si);           

            
// Increase posion by pixles
            if (si.nPos < (si.nMax - si.nPage))
            {
                si.nPos 
+= pixels;
            }
            
else
            {
                ptrWparam 
= new IntPtr(SB_ENDSCROLL);
                t.Enabled 
= false;
                SendMessage(handle, WM_VSCROLL, ptrWparam, ptrLparam);
            }
                        
            
// Reposition scroller
            SetScrollInfo(handle, (int)ScrollBarDirection.SB_VERT, ref si, true);

            
// Send a WM_VSCROLL scroll message using SB_THUMBTRACK as wParam
            
// SB_THUMBTRACK: low-order word of wParam, si.nPos high-order word of wParam
            ptrWparam = new IntPtr(SB_THUMBTRACK + 0x10000 * si.nPos);            
            SendMessage(handle, WM_VSCROLL, ptrWparam, ptrLparam);         
        }
    }
原文地址:https://www.cnblogs.com/kakaliush/p/1985763.html