public void SetText(string text, double yPositionAsPercentage) { txtPreview.Text = text; var info = new Scrollbarinfo(); info.CbSize = Marshal.SizeOf(info); GetScrollBarInfo(txtPreview.Handle, OBJID_HSCROLL, ref info); //Position the scrollbar based on the presentage GetScrollRange(txtPreview.Handle, SB_HORZ, out int _, out int vSmaxPos); var tumbSize = info.XyThumbBottom - info.XyThumbTop; var pos = (int)Math.Floor(vSmaxPos * yPositionAsPercentage); pos -= tumbSize / 2; if (pos < 0) { pos = 0; } SetScrollPos(txtPreview.Handle, SB_HORZ, pos, true); PostMessageA(txtPreview.Handle, WM_HSCROLL, SB_THUMBPOSITION + 0x10000 * pos, 0); _isActive = true; }
private static extern int GetScrollBarInfo(IntPtr hWnd, uint idObject, ref Scrollbarinfo psbi);
public static extern bool GetScrollBarInfo([In] IntPtr a_hwnd, int a_idObject, ref Scrollbarinfo a_psbi);