C#: Synchronize Scroll Position of two RichTextBoxes?

前端 未结 5 600
失恋的感觉
失恋的感觉 2020-11-30 10:00

In my application\'s form, I have two RichTextBox objects. They will both always have the same number of lines of text. I would like to \"synchronize\" the vert

5条回答
  •  盖世英雄少女心
    2020-11-30 10:48

    const int WM_USER = 0x400;
    
    const int EM_GETSCROLLPOS = WM_USER + 221;
    
    const int EM_SETSCROLLPOS = WM_USER + 222;
    
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern int SendMessage(IntPtr hWnd, int msg, int wParam, ref Point lParam);
    
    private void RichTextBox1_VScroll(object sender, EventArgs e)
    {
        Point pt;
    
        SendMessage(RichTextBox1.Handle, EM_GETSCROLLPOS, 0, ref pt);
    
        SendMessage(RichTextBox2.Handle, EM_SETSCROLLPOS, 0, ref pt);
    }
    
    
    private void RichTextBox2_VScroll(object sender, EventArgs e)
    {
        Point pt;
    
        SendMessage(RichTextBox1.Handle, EM_GETSCROLLPOS, 0, ref pt);
    
        SendMessage(RichTextBox2.Handle, EM_SETSCROLLPOS, 0, ref pt);
    }
    

提交回复
热议问题