支持同步滚动的RichTextbox控件

时间:2023-12-29 22:33:32
using System.Windows.Forms;

public class SynchronizedScrollRichTextBox : System.Windows.Forms.RichTextBox
{
public SynchronizedScrollRichTextBox Synchronized { get; set; }
public const int WM_VSCROLL = 0x115;
public const int EM_LINESCROLL = 0xB6; protected override void WndProc(ref System.Windows.Forms.Message msg)
{
if (msg.Msg == WM_VSCROLL || msg.Msg == EM_LINESCROLL)
{
if (Synchronized != null)
{
Message message = msg;
message.HWnd = Synchronized.Handle;
Synchronized.PubWndProc(ref message);
}
}
base.WndProc(ref msg);
} public void PubWndProc(ref System.Windows.Forms.Message msg)
{
base.WndProc(ref msg);
}
}

以上代码,复制到项目中,编译一次,拖到窗体中即可,例如拖2个实例,然后在设计界面,分别设置 Synchronized 属性为对方。运行后两个文本框就可以同步滚动。