views:

122

answers:

2

I want to use the mouse middle button to clear a RichTextBox, but it also activates a mouse scrolling functionality similar to what you find in web broswers. When the vertical scrollbar is visible (there's enough data) and you press the middle button in the mouse a scrolling cursor appears and you can scroll up or down by moving the cursor up or down. How do I disable the mouse scrolling?

Mouse scrolling seems to be a Windows (or mouse driver) feature, so how can I stop the MouseDown event (if the mouse middle button is pressed) from reaching whatever routine is responsible for the mouse scrolling?

A: 

No scrolling RichTextBox, just Inherit from RichTextBox and you done.

public class NoScrollRichTextBox : RichTextBox
{
   const int WM_MOUSEWHEEL = 0x020A;

   protected override void WndProc(ref Message m)
   {
      // This will completely ignore the mouse wheel, which will disable zooming as well
      if (m.Msg != WM_MOUSEWHEEL)
         base.WndProc(ref m);
   }
}
JeremySpouken
I get the idea, but I don't want to disable the "mouse wheel scrolling", I want to disable "mouse movement scrolling". Have you seen how you can scroll in all directions in a web page by moving the mouse after you click the mouse wheel? I also don't want to disable the mouse wheel click as I want to use it to clear the contents of the richtextbox without activating the "mouse movement scrolling" cursor.
OIO
+1  A: 

Check for 0x207 and 0x208, middle button down and up:

using System;
using System.Windows.Forms;

class MyRtb : RichTextBox {
    protected override void WndProc(ref Message m) {
        if (m.Msg == 0x207) this.Clear();
        else if (m.Msg != 0x208) base.WndProc(ref m);
    }
}
Hans Passant
The code needs a small correction. As you stated, the messages are 0x207 and 0x208, but the code uses 0x208 and 0x209.
OIO
When debugging the application I see too many iterations of this method. It doesn't look like an efficient solution, especially when the RichTextBox is used to display data from a serial port, it seems to processes all messages related to the RichTextBox.
OIO
Oh Lord. The 3 nanoseconds that it takes is dragging things down measurably?
Hans Passant
Is it possible in C# to stop the MouseDown/Up event once it has been acted upon by a method, because that's all the issue, the event is acted upon, but then other routines are also activated. It is recommended to let events go their course, but this is only a RichTextBox, there must be a way to handle it for good, like e.hanlded for key presses.
OIO
That's what the code does, it makes sure that the native Windows control cannot see the message. It is completely unclear to me why you think there's a problem with this.
Hans Passant
Does this code check every message the RIchTextBox receives, like when text is added to the control, or even when the control moves or is painted? I'm not clear about the scope of the messages the method handles (it looks like Windows messages and there are a bunch of them).
OIO