0

I have successfully controlled the vertical scrollbar in a RichTextBox thanks to the earlier post here: https://stackoverflow.com/a/5611856/848344. But how do I control the horizontal scrollbar?

The method is filled in for setVerticalScroll(). I just need it filled in for setHorizontalScroll() where it says "Insert gubbins here.".

// 32 bit scrolling of pane slider
// https://stackoverflow.com/questions/1380104/cc-setscrollpos-user32-dll
[DllImport("user32.dll")]
static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);
[DllImport("User32.Dll", EntryPoint = "PostMessageA")]
static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);
[DllImport("User32.dll")]
private extern static int GetScrollPos(IntPtr hWnd, int nBar);
private enum ScrollBarType : uint { SbHorz = 0, SbVert = 1, SbCtl = 2, SbBoth = 3 }

public void setVerticalScroll(IntPtr hWnd, int pos) {
    SetScrollPos(hWnd, 0x1, pos, true);
    PostMessage(hWnd, 0x115, 4 + 0x10000 * pos, 0);
}
public void setHorizontalScroll(IntPtr hWnd, int pos) {
    /////////////////////////////////////
    //////////////// Insert gubbins here.
    /////////////////////////////////////
}
public int getVerticalScroll(IntPtr hWnd) {
    int n = GetScrollPos(hWnd, (int)ScrollBarType.SbVert);
    return n;
}
public int getHorizontalScroll(IntPtr hWnd) {
    int n = GetScrollPos(hWnd, (int)ScrollBarType.SbHorz);
    return n;
}
Community
  • 1
  • 1
Dan W
  • 3,520
  • 7
  • 42
  • 69
  • Use proper symbols here, it is WM_VSCROLL instead of 0x115, SB_VERT instead of 0x1. You then cannot help yourself falling in the pit of success with WM_HSCROLL and SB_HORZ. Use SendMessage() instead. – Hans Passant Mar 31 '14 at 10:43
  • How is SendMessage() better than PostMessage? – Dan W Mar 31 '14 at 16:56
  • It is always sent, whatever program you are hacking might not expect GetMessage() to return that message and thus not handle it properly. – Hans Passant Mar 31 '14 at 18:00

1 Answers1

0

Through trial and error along with sheer luck, I think I found the solution. I just minus one from the 0x115 value to make 0x114 (and also changed 0x1 to 0x0):

public void setHorizontalScroll(IntPtr hWnd, int pos)
{
    SetScrollPos(hWnd, 0x0, pos, true);
    PostMessage(hWnd, 0x114, 4 + 0x10000 * pos, 0);
}

If someone could check that though, I'd be grateful.

Dan W
  • 3,520
  • 7
  • 42
  • 69