4dots Software
CODE HELP BLOG
To set or get the scroll position of a RichTextBox control we need the help of a little Windows API.
Specifically, we use the SendMessage function to send the EM_GETSCROLLPOS, EM_SETSCROLLPOS messages to the control.
To get the scroll position of a RichTextBox control we use the following GetScrollPos function and to set it we use
the following SetScrollPos function.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;
namespace FourDots
{
class RichTextBoxHelper
{
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hwndLock, Int32 wMsg, Int32 wParam, ref Point pt);
public static Point GetScrollPos(RichTextBox txtbox)
{
const int EM_GETSCROLLPOS = 0x0400 + 221;
Point pt = new Point();
SendMessage(txtbox.Handle, EM_GETSCROLLPOS, 0, ref pt);
return pt;
}
public static void SetScrollPos(Point pt,RichTextBox txtbox)
{
const int EM_SETSCROLLPOS = 0x0400 + 222;
SendMessage(txtbox.Handle, EM_SETSCROLLPOS, 0, ref pt);
}
}
}