0

当我向上滚动聊天框时,我想向上滚动时间框。(不一定反之亦然)

我发现以下代码可以做到这一点:

/// Subclass RichTextBox to add the capability to bind scrolling for multiple RichTextBoxs.
/// This is useful for 'parallel' RTBs that require synchronized scrolling.
/// Taken from https://gist.github.com/593809
/// Added WM_HSCROLL 
/// Added BindScroll() to form a two-way linkage between RichTextBoxes.
/// Example usage showing how to bind 3 RichTextBoxes together:
/// rtb1.BindScroll(rtb2);
/// rtb2.BindScroll(rtb3);
/// rtb3.BindScroll(rtb1);

class RichTextBoxSynchronizedScroll : RichTextBox
{

    private const int WM_VSCROLL = 0x115;
    private const int WM_HSCROLL = 0x114;

    private List<RichTextBoxSynchronizedScroll> peers = new List<RichTextBoxSynchronizedScroll>();

    /// <summary>
    /// Establish a 2-way binding between RTBs for scrolling.
    /// </summary>
    /// <param name="arg">Another RTB</param>
    public void BindScroll( RichTextBoxSynchronizedScroll arg )
    {
        if ( peers.Contains( arg ) || arg==this ) { return; }
        peers.Add( arg );
        arg.BindScroll(this);
    }

    private void DirectWndProc(ref Message m)
    {
        base.WndProc(ref m);
    }

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_VSCROLL || m.Msg == WM_HSCROLL )
        {
            foreach (RichTextBoxSynchronizedScroll peer in this.peers)
            {
                Message peerMessage = Message.Create(peer.Handle, m.Msg, m.WParam, m.LParam);
                peer.DirectWndProc(ref peerMessage);
            }
        }

        base.WndProc(ref m);
    }
}

但是,我已经为此绞尽脑汁超过 2 小时,试图让不同的代码工作,但我无法让它们中的任何一个工作,因为我只是相对刚刚开始编程,我不知道是什么与此代码有关。

我已经尝试将它作为一个额外的类放在我的表单代码中,但是我实际上无法将 BindScroll() 应用于任何文本框,因为我无法引用它们或实例。

也许我可以,但我不知道怎么做。我试过只使用类内部的代码,而不是它本身就是一个类,但这会导致错误。

任何帮助将非常感激...

4

2 回答 2

2

测试您的代码后,它似乎工作正常。您的问题可能是您不知道如何使用代码,您必须将新的 Richtextbox 声明为RichTextBoxSynchronizedScroll不符合标准RichTextBox

//Here is the test
public partial class Form1 : Form {
   public Form1(){
     InitializeComponent();
     rb1.Size = new Size(200,100);
     rb2.Size = rb1.Size;
     rb2.Left = rb1.Right + 5;
     rb1.Parent = rb2.Parent = this;
     rtb1.BindScroll(rtb2);       
     //try populating some data for both the richtextboxes
     for(int i = 0; i < 200; i++)
        rtb1.Text += Guid.NewGuid() + "\r\n";
     rtb2.Text = rtb1;
     //now try scrolling the rtb1
     //I suggest you should add WM_MOUSEWHEEL = 0x20a into the if statement
     //something like this: if (m.Msg == WM_VSCROLL || m.Msg == WM_HSCROLL || m.Msg == WM_MOUSEWHEEL) ...
   }
   RichTextBoxSynchronizedScroll rtb1 = new RichTextBoxSynchronizedScroll();
   RichTextBoxSynchronizedScroll rtb2 = new RichTextBoxSynchronizedScroll();
}
//That's all
于 2013-07-30T02:40:35.390 回答
0

我已经解决了整个 3 小时......我想要一些非常简单的东西。我考虑了滚动的各个方面。您只需要创建 VScrollBar 并像这样定义滚动事件

private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
    {
        int position = text1.GetFirstCharIndexFromLine(vScrollBar1.Value);
        //position only 0.character on line
        textBox1.Select(position, 0);//if position=vScrollBar1.Value you would actually scrolling char by char
        textBox2.Select(position, 0);
        textBox1.ScrollToCaret();
        textBox2.ScrollToCaret();
    }

希望我将来能找到这个。

于 2017-11-21T17:32:04.360 回答