Disabling smooth scrolling on Richtextbox

I have a label that shows line numbers based on RichTextBox text. I bound the Vscroll event to handle marking.

private void rtbLogicCode_VScroll(object sender, EventArgs e) { Point pt = new Point(0, 1); int firstIndex = rtbLogicCode.GetCharIndexFromPosition(pt); int firstLine = rtbLogicCode.GetLineFromCharIndex(firstIndex); pt.X = ClientRectangle.Width; pt.Y = ClientRectangle.Height; int lastIndex = rtbLogicCode.GetCharIndexFromPosition(pt); int lastLine = rtbLogicCode.GetLineFromCharIndex(lastIndex); // Small correction if (rtbLogicCode.Text.EndsWith("\n")) lastLine++; labelLogicCode.ResetText(); LabelLineNum(firstLine+1,lastLine); } #endregion private void LabelLineNum(int startNum, int lastNum) { labelLogicCode.Font = UIConstant.DDCLogicCodeFont; for (int i = startNum; i < lastNum; i++) { labelLogicCode.Text += i + Environment.NewLine; } } 

Everything seems to work correctly, except that RichTextBox uses the Smooth Scrolling function, which in many cases spins my line numbering when the user did not scroll all the way to the next line. This results in line numbers not being synchronized with the actual text displayed in the RichTextBox.

In the end, I need to disable the smoothscrolling function in order to accomplish this. I was told that you can redefine the PostMessage RichTextBox API to disable the specified function, but after searching through many documents I could not find any good ones.

I would appreciate the most detailed decision on how to disable the smooth scrolling feature. Thanks.

+4
source share
1 answer

Here is an example of VB from Microsoft, offering you to intercept WM_MOUSEWHEEL messages.

Here's a quick prototype in C #:

 class MyRichTextBox : RichTextBox { [DllImport("user32.dll")] public static extern IntPtr SendMessage( IntPtr hWnd, // handle to destination window uint Msg, // message IntPtr wParam, // first message parameter IntPtr lParam // second message parameter ); const uint WM_MOUSEWHEEL = 0x20A; const uint WM_VSCROLL = 0x115; const uint SB_LINEUP = 0; const uint SB_LINEDOWN = 1; const uint SB_THUMBTRACK = 5; private void Intercept(ref Message m) { int delta = (int)m.WParam >> 16 & 0xFF; if((delta >> 7) == 1) { SendMessage(m.HWnd, WM_VSCROLL, (IntPtr)SB_LINEDOWN, (IntPtr)0); } else { SendMessage(m.HWnd, WM_VSCROLL, (IntPtr)SB_LINEUP, (IntPtr)0); } } protected override void WndProc(ref Message m) { switch((uint)m.Msg) { case WM_MOUSEWHEEL: Intercept(ref m); break; case WM_VSCROLL: if(((uint)m.WParam & 0xFF) == SB_THUMBTRACK) { Intercept(ref m); } else { base.WndProc(ref m); } break; default: base.WndProc(ref m); break; } } } 
+5
source

All Articles