在RichTextBox的prevent自动滚屏 [英] Prevent Autoscrolling in RichTextBox

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

问题描述

我有我使用RichTextBox控件实现的只读数据记录窗口。我希望能够禁用,当用户点击在控制中发生的自动滚动,使用户可以选择一个特定的日志复制/粘贴操作或什么的。但是,只要用户点击RichTextBox的,它会自动滚动至底部,使这个困难。

I have a readonly data logging window that I implemented using the RichTextBox control. I'd like to be able to disable the autoscrolling that happens when the user clicks in the control so that the user can select a specific log for copy/paste operations or whatever. However, as soon as the user clicks in the RichTextBox, it automatically scrolls to the bottom, making this difficult.

任何人都知道的方式来覆盖此行为?

Anyone know a way to override this behavior?

谢谢!

推荐答案

RichTextBox控件自动滚动到当前的选择,如果选择不隐藏。 RichTextBox.AppendText(),除了附加的文字,还修改当前的选择,所以间接触发自动滚动的行为。请注意,如果RichTextBox.HideSelection设置为true,那么选择将当控件没有焦点隐藏;这说明您所描述的行为,只有当用户点击控制了发生自动滚动。 (从而给其焦点)
以prevent这一点,你需要做以下附加文字时:

The RichTextBox control automatically scrolls to the current selection, if the selection is not hidden. RichTextBox.AppendText(), in addition to appending text, also modifies the current selection, and so indirectly triggers the "autoscrolling" behaviour. Note that if RichTextBox.HideSelection is set to true, then the selection would be hidden when the control is not in focus; this explains the behaviour you described, where autoscrolling occurs only when the user clicks in the control. (thereby giving it focus) To prevent this, you need to do the following when appending text:


  1. 备份初始选择

  2. 的无焦点控

  3. 隐藏选择(通过Windows消息)

  4. AppendText通过

  5. 恢复初始选择

  6. 取消隐藏选

  7. 重新聚焦控制

您可能还需要检查选择是否已经在文本的末尾,并允许自动滚动的行为,如果它是 - 这基本上是模仿Visual Studio的输出窗口的行为。例如:

You may also want to check whether the selection is already at the end of the text, and allow the autoscrolling behaviour if it is - this essentially emulates the behaviour of Visual Studio's Output window. For example:

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, Int32 wParam, Int32 lParam);
    const int WM_USER = 0x400;
    const int EM_HIDESELECTION = WM_USER + 63;

    void OnAppend(string text)
    {
        bool focused = richTextBox1.Focused;
        //backup initial selection
        int selection = richTextBox1.SelectionStart;
        int length = richTextBox1.SelectionLength;
        //allow autoscroll if selection is at end of text
        bool autoscroll = (selection==richTextBox1.Text.Length);

        if (!autoscroll)
        {
            //shift focus from RichTextBox to some other control
            if (focused) textBox1.Focus();
            //hide selection
            SendMessage(richTextBox1.Handle, EM_HIDESELECTION, 1, 0);
        }

        richTextBox1.AppendText(text);

        if (!autoscroll)
        {
            //restore initial selection
            richTextBox1.SelectionStart = selection;
            richTextBox1.SelectionLength = length;
            //unhide selection
            SendMessage(richTextBox1.Handle, EM_HIDESELECTION, 0, 0);
            //restore focus to RichTextBox
            if(focused) richTextBox1.Focus();
        }
    }

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

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