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

本文关键字:滚动 richtextbox | 更新日期: 2023-09-27 18:19:25

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

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

你应该重写WndProc并阻塞WM_SETFOCUS。

protected override void WndProc(ref Message m)
{
    if(m.Msg != WM_SETFOCUS)
        base.WndProc(ref m);
}
这里有一个教程:如何:c# -防止RichTextBox从自动滚动

这对我很有效。

首先,正如你在其他文章中看到的,你需要从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);
    }
Suspend方法停止Control在屏幕上重画。Resume方法在屏幕上为给定的Control重新绘制。 GetScrollPoint方法获取滚动插入符号所在的实际SetScrollPoint将滚动插入符号放置在给定的

如何使用这些方法?首先,给定一个控件,你需要停止自动滚动,调用Suspend,然后调用GetScrollPoint(做你需要做的控制,如高亮或追加文本),然后SetScrollPoint,最后Resume

在我的情况下,我想复制整行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());
        }
    }

希望这对你有帮助!