Example of using APIs to control automatic text scrolling in richtextbox,
Let alone code
Public partial class Form1: Form
{
[DllImport ("user32.dll")]
[Return: financialas (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 );
}
}