如何在c#中禁用richtextbox中的自动滚动

时间:2013-06-20 13:13:11

标签: c# scroll richtextbox

我想在c#中禁用richtextbox的滚动功能。我只想让richtextbox允许用户只在其大小区域输入,意味着没有用户的垂直滚动。就像MS-word或提前打开Office Pages.thanx一样。

2 个答案:

答案 0 :(得分:0)

您应该覆盖WndProc并屏蔽WM_SETFOCUS

protected override void WndProc(ref Message m)
{
    if(m.Msg != WM_SETFOCUS)
        base.WndProc(ref m);
}

以下是有关此内容的教程:How to: C# - Prevent RichTextBox from auto scrolling

答案 1 :(得分:0)

这对我有用。

您可能在其他帖子中看到的第一件事是您需要从C#访问 user32.dll

[System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, Int32 wParam, Int32 lParam);


    [System.Runtime.InteropServices.DllImport("user32.dll")]
    private static extern int SendMessage(IntPtr hwndLock,Int32 wMsg,Int32 wParam, ref Point pt);

我们需要做一些不断声明才能正确调用 SendMessage

private const int WM_USER = 0x400;
private const int EM_HIDESELECTION = WM_USER + 63;
private const int WM_SETREDRAW = 0x000B;
private const int EM_GETSCROLLPOS = WM_USER + 221;
private const int EM_SETSCROLLPOS = WM_USER + 222;

然后,每当我们需要停止滚动时,都会使用一些公共静态方法。

    public static void Suspend(Control control)
    {
        Message msgSuspendUpdate = Message.Create(control.Handle, WM_SETREDRAW, IntPtr.Zero,
            IntPtr.Zero);

        NativeWindow window = NativeWindow.FromHandle(control.Handle);
        window.DefWndProc(ref msgSuspendUpdate);
    }

    public static void Resume(Control control)
    {
        // Create a C "true" boolean as an IntPtr
        IntPtr wparam = new IntPtr(1);
        Message msgResumeUpdate = Message.Create(control.Handle, WM_SETREDRAW, wparam,
            IntPtr.Zero);

        NativeWindow window = NativeWindow.FromHandle(control.Handle);
        window.DefWndProc(ref msgResumeUpdate);

        control.Invalidate();
    }


    public static Point GetScrollPoint(Control control) {
        Point point = new Point();
        SendMessage(control.Handle, EM_GETSCROLLPOS, 0, ref point);
        return point;
    }

    public static void SetScrollPoint(Control control, Point point)
    {
        SendMessage(control.Handle, EM_SETSCROLLPOS, 0, ref point);

    }

暂停方法会停止控制以在屏幕上重绘。 恢复方法会在屏幕上重新开始给定控件的重绘。

GetScrollPoint 方法获取滚动插入符所在的实际 SetScrollPoint 将滚动插入符号放在给定的

如何使用这些方法?首先,如果控制,您需要停止自动滚动,拨打暂停,然后拨打 GetScrollPoint ,(制作您需要做的事情)控件,如突出显示或追加文字)然后 SetScrollPoint ,最后恢复

在我的情况下,我希望在光标从一行移动到另一行时随时复制 RichTextBox 的整行。 (这样做会在长行上产生滚动)。

这是我的工作方法:

    private int intLastLine = -1;
    private void richTextBoxSwitch_SelectionChanged(object sender, EventArgs e)
    {
        try
        {
            if (this.richTextBoxSwitch.TextLength > 0)
            {
                ControlBehavior.Suspend(this.richTextBoxSwitch);
                Point point = ControlBehavior.GetScrollPoint(this.richTextBoxSwitch);
                int intSelectionStartBackup = this.richTextBoxSwitch.SelectionStart;
                int intSelectionLengthBackup = this.richTextBoxSwitch.SelectionLength;

                int intCharIndex = this.richTextBoxSwitch.GetFirstCharIndexOfCurrentLine();
                int intLine = this.richTextBoxSwitch.GetLineFromCharIndex(intCharIndex);

                this.richTextBoxSwitch.SuspendLayout();
                if (intLastLine != intLine)
                {
                    intLastLine = intLine;
                    int intLength = this.richTextBoxSwitch.Lines[intLine].Length;
                    this.richTextBoxSwitch.Select(intCharIndex, intLength);
                    this.richTextBoxSwitch.BackColor = ColorMessageBackground;
                    strData = this.richTextBoxSwitch.SelectedText;
                    this.textBoxMessageSelected.Text = strData.Trim();
                    this.richTextBoxSwitch.Select(intSelectionStartBackup, intSelectionLengthBackup);
                }
                this.richTextBoxSwitch.ResumeLayout();
                ControlBehavior.SetScrollPoint(this.richTextBoxSwitch, point);
                ControlBehavior.Resume(this.richTextBoxSwitch);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

希望这有帮助!