在RichTextBox中禁用平滑滚动 [英] Disabling Smooth Scrolling on Richtextbox

查看:199
本文介绍了在RichTextBox中禁用平滑滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有标签的基础上对RichTextBox的文本行号的标签。我已经迷上VSCROLL的事件来处理的标签。

I have a label which labels the line numbers based on the text on RichTextBox. I have hooked the event of Vscroll to handle the labeling.

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;
    }
}

一切似乎都在正常工作,除了RichTextBox的使用平滑滚动功能,它搞砸了我的行号,在许多情况下,用户不滚动一路到下一行。这将导致行号将不会与在RichTextBox显示的实际文本同步。

Everything seems to work properly except RichTextBox uses Smooth Scrolling feature, which screws up my line numbering in many cases where the user has not scrolled all the way to the next line. This causes the line numbers to be not synchronized with the actual text shown on the RichTextBox.

在最后,我需要禁用smoothscrolling功能来做到这一点。有人告诉我,你可以覆盖的RichTextBox的postMessage的API来禁用提到的功能,而是通过许多文件搜索后,我找不到任何好的。

In the end, I need to disable smoothscrolling feature to accomplish this. I was told that you can override the postMessage API of RichTextBox to disable the mentioned feature but after searching through many documents, I couldn't find any good ones.

我会AP preciate一个解决方案,那就是尽可能详细了解如何禁用smoothscrolling功能。谢谢你。

I would appreciate a solution that is as detailed as possible on how to disable smoothscrolling feature. Thanks.

推荐答案

下面是一个<一href="http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/f41045bf-7f19-4303-b297-09f93cf56cd2"相对=nofollow> VB例如的微软,这表明你需要截取 WM_MOUSEWHEEL 的消息。

Here's a VB example from Microsoft, suggesting you need to intercept WM_MOUSEWHEEL messages.

下面是一个快速原型在C#:<​​/ P>

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 {
                    DefWndProc(ref m);
                }
                break;
            default:
                DefWndProc(ref m);
                break;
        }
    }
}

这篇关于在RichTextBox中禁用平滑滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆