-
Notifications
You must be signed in to change notification settings - Fork 0
/
RichTextBoxSynchronizedScroll.cs
52 lines (46 loc) · 2.03 KB
/
RichTextBoxSynchronizedScroll.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
namespace PT_Sguil
{
/// Credit to:
/// http://snipplr.com/view/50758/scroll-multiple-richtextboxes-or-textboxes-in-unison-synchronized-scrolling/
/// 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 : System.Windows.Forms.RichTextBox
{
private const int WM_VSCROLL = 0x115;
private const int WM_HSCROLL = 0x114;
private System.Collections.Generic.List<RichTextBoxSynchronizedScroll> peers = new System.Collections.Generic.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 System.Windows.Forms.Message m)
{
base.WndProc(ref m);
}
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == WM_VSCROLL || m.Msg == WM_HSCROLL)
{
foreach (RichTextBoxSynchronizedScroll peer in this.peers)
{
System.Windows.Forms.Message peerMessage = System.Windows.Forms.Message.Create(peer.Handle, m.Msg, m.WParam, m.LParam);
peer.DirectWndProc(ref peerMessage);
}
}
base.WndProc(ref m);
}
}
}