两个richtextbox的滚动条相同

时间:2012-04-22 09:03:36

标签: c# .net custom-controls scrollbar

是否有任何第三方工具可用,它有两个richtextbox但只有两个共享滚动条。我需要用两种不同的语言实现一些文本,但两个文本框应该同时滚动。

1 个答案:

答案 0 :(得分:4)

    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));
    }