0

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

4

2 回答 2

1

您应该覆盖WndProc并阻止WM_SETFOCUS

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

这是一个关于此的教程:如何:C# - 防止 RichTextBox 自动滚动

于 2013-06-20T14:32:09.190 回答
1

这对我有用。

您可能在其他帖子中看到的第一件事是,您需要从 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方法停止控件以在屏幕上进行重绘。Resume方法在给定的 Control 的屏幕上重新开始重绘

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

如何使用这些方法?首先,给定一个Control,您需要停止自动滚动,调用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());
        }
    }

希望这可以帮助!

于 2015-01-24T14:22:16.963 回答