简体   繁体   English

在Winforms中同步RichTextBox滚动

[英]Synchronize richtextbox scrolling in winforms

I've used the following method 我使用了以下方法

http://snipplr.com/view/50758/scroll-multiple-richtextboxes-or-textboxes-in-unison-synchronized-scrolling/ http://snipplr.com/view/50758/scroll-multiple-richtextboxes-or-textboxes-in-unison-synchronized-scrolling/

to scroll two richtextboxes, I ended up with the following and just called rtb1.BindScroll(rtb2) 滚动两个richtextbox,我结束了以下内容,只是称为rtb1.BindScroll(rtb2)

This works but is it possible to support the wheelmouse for both? 这行得通,但是否可以同时为这两个鼠标器配套? Also, pageup and down would be good. 另外,向上和向下翻页也很好。

This method promised the wheelmouse but did not work at all http://social.msdn.microsoft.com/Forums/windows/en-US/161d1636-aea3-4fee-beb4-52370663d44c/synchronize-scrolling-in-2-richtextboxes-in-c?forum=winforms 此方法承诺可以使用鼠标,但根本无法使用http://social.msdn.microsoft.com/Forums/windows/en-US/161d1636-aea3-4fee-beb4-52370663d44c/synchronize-scrolling-in-2-richtextboxes -in-C?论坛=的WinForms

   [DebuggerNonUserCode]
    public class RouterRichTextBox : RichTextBox
    {    
        public delegate void vScrollEventHandler(Message message);
        public const int WM_VSCROLL = 0x115;
        public event vScrollEventHandler vScroll;

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

        public void BindScroll(RouterRichTextBox 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)
            {
                foreach (RouterRichTextBox peer in peers)
                {
                    Message peerMessage = Message.Create(peer.Handle, m.Msg, m.WParam, m.LParam);
                    peer.DirectWndProc(ref peerMessage);
                }
            }

            base.WndProc(ref m);
        }
    }

Support for mousewheel should be enabled by adding 对鼠标滚轮的支持应通过添加来启用

private const int WM_MOUSEWHEEL = 0x20a;

to your class. 上你的课。 In WndProc add this to the if-clause 在WndProc中将此添加到if子句中

if (m.Msg == WM_VSCROLL || m.Msg == WM_MOUSEWHEEL)

to enable the use of the mousewheel. 启用鼠标滚轮的使用。 I just tested it and it works here. 我刚刚对其进行了测试,并且在这里可以正常工作。

For the use of PageUp or PageDown you have to research further. 为了使用PageUp或PageDown,您必须进一步研究。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM