same scroll bar for two richtextboxes

2019-06-26 10:57发布

问题:

Is there any third party tool available which has two richtextboxes but only a shared scroll bar for both. I need to implement some text in two different languages but both the textboxes should scroll at the same time.

回答1:

    public enum ScrollBarType : uint
    {
        SbHorz = 0,
        SbVert = 1,
        SbCtl = 2,
        SbBoth = 3
    }

    public enum Message : uint
    {
        WM_VSCROLL = 0x0115
    }

    public enum ScrollBarCommands : uint
    {
        SB_THUMBPOSITION = 4
    }

    [DllImport("User32.dll")]
    public extern static int GetScrollPos(IntPtr hWnd, int nBar);

    [DllImport("User32.dll")]
    public extern static int SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
   // Set the dual scrolling on the richTextbox1 and affects richTextbox2

    private void richTextBox1_VScroll(object sender, EventArgs e)
    {
        int nPos = GetScrollPos(richTextBox1.Handle, (int)ScrollBarType.SbVert); 
        nPos <<= 16;
        uint wParam = (uint)ScrollBarCommands.SB_THUMBPOSITION | (uint)nPos;
        SendMessage(richTextBox2.Handle, (int)Message.WM_VSCROLL, new IntPtr(wParam), new IntPtr(0));
    }