Scroll RichTextBox

I would like to control the scrolling of a RichTextBox, but cannot find any method for this in the control.

The reason for this is because I want the mouse wheel scroll to be effective when the mouse cursor is over the RichTextBox control (it does not have active focus: the mouse wheel event is processed by the form).

Any help is appreciated!

+4
source share
1 answer

It is a bit simple with win32. Here you are:

//must add using System.Reflection
public partial class Form1 : Form, IMessageFilter 
{
    bool hovered;
    MethodInfo wndProc;

    public Form1() 
    {
       InitializeComponent();
       Application.AddMessageFilter(this);
       richTextBox1.MouseEnter += (s, e) => { hovered = true; };
       richTextBox1.MouseLeave += (s, e) => { hovered = false; };
       wndProc = typeof(Control).GetMethod("WndProc", BindingFlags.NonPublic | 
                                                      BindingFlags.Instance);
    }

    public bool PreFilterMessage(ref Message m) 
    {
        if (m.Msg == 0x20a && hovered) //WM_MOUSEWHEEL = 0x20a
        {
           Message msg = Message.Create(richTextBox1.Handle, m.Msg, m.WParam, m.LParam);
           wndProc.Invoke(richTextBox1, new object[] { msg });
        }
        return false;
    }
}

. IMessageFilter, WM_MOUSEWHEEL application-level. Reflection WndProc WM_MOUSEWHEEL, SendMessage win32 WM_MOUSEWHEEL richTextBox1, declaration . .

+2

All Articles