Obtain scroll bar 'position' in RichTextBox with scroll bars disabled

c#, richtextbox, scroll, scrollbar, winapi

Solution

I think you will find something interesting here. It's description of `RichEdit` used behind `RichTextBox` in .Net.

Also solution of your question:

var ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(POINT)));
Marshal.StructureToPtr(new POINT(), ptr, false);
SendMessage(this.richTextBox1.Handle, EM_GETSCROLLPOS, IntPtr.Zero, ptr);
var point = (POINT)Marshal.PtrToStructure(ptr, typeof(POINT));
Marshal.FreeHGlobal(ptr);

Where:

EM_GETSCROLLPOS = WM_USER + 221

And `POINT` structure from pinvoke.net.

Problem

My earlier post here shows how to obtain the position of the horizontal or vertical scrollbars in a RichTextbox. However, these only work if scrollbars are enabled. If you set scrollbars to None (via `richTextBox1.ScrollBars = RichTextBoxScrollBars.None;`), then you can still scroll down off the bottom of the box (and off to the right if you disable WordWrap). However, the `getVerticalScroll()` and `getHorizontalScroll()` methods (as shown in the link I posted) only return 0 now. They seem to need to 'see' the scrollbars to actually work. So how can I get (and set) the 'scroll position' whilst scroll bars are disabled?

Original source

Related problems