如何使用鼠标在richTextBox中获取所选文本并实时显示文本?

时间:2015-06-03 04:19:27

标签: c# .net winforms richtextbox

今天当我用鼠标点击富文本框文本窗口并将鼠标向右拖动时,它突出显示蓝色文本。

我想要做的是模拟这种情况,但这一次:

  1. 要在移动鼠标并突出显示文本时进行制作,使其颜色为黄色,而不是默认为蓝色。

  2. 为了实现这一点,点击鼠标左键并不停地按下鼠标,我将鼠标移到文本上,实时显示标签中的文字。

  3. 到目前为止我做了什么:

    private void richTextBox1_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Clicks == 1 && e.Button == MouseButtons.Left)
                {
                    int positionStart = richTextBox1.GetCharIndexFromPosition
                                           (new Point(e.X, e.Y));
                    this.richTextBox1.SelectionStart = positionStart;
    
                }
            }
    

    不确定鼠标按下是否是正确的事件,也许我还需要使用鼠标按下事件。 或者鼠标也可以移动。

    现在我有SelectionStart索引。

    但接下来我该怎么办?

    编辑:

    为了实时显示所选的文本,它可以正常工作,但实时着色它不起作用。我试过了:

    Font drawFont = new Font("Arial", 16);
            SolidBrush drawBrush = new SolidBrush(Color.Yellow);
            PointF drawPoint = new PointF(0, 0);
    
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                e.Graphics.DrawString(richTextBox1.SelectedText, drawFont, drawBrush,drawPoint);
            }
    

    但它根本没有着色任何东西。 我检查了断点它到达那里但没有着色任何文本。 我想要的是实时在mousemove事件中它将为所选文本着色。

2 个答案:

答案 0 :(得分:0)

private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
{
    if(e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        label1.Text = richTextBox1.SelectedText;
    }
}

private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
    label1.Text = richTextBox1.SelectedText;
}

鼠标上的主要功能是否移动,并确保鼠标左键按下。然后要抓住人是否按下了ctrl A使用选择更改功能。我已经尝试并测试了它,它已经有效了。

至于选择颜色为黄色,你必须进入richtextbox的onpaint功能。

答案 1 :(得分:0)

为了在鼠标悬停时突出显示文本,尝试定义附加属性:

public class TextBoxProperties : DependencyObject
{
    public static readonly DependencyProperty HighlightTextOnFocusProperty =
DependencyProperty.RegisterAttached("HighlightTextOnFocus",
typeof(bool), typeof(TextBoxProperties),
new PropertyMetadata(false, HighlightTextOnFocusPropertyChanged));


    [AttachedPropertyBrowsableForChildrenAttribute(IncludeDescendants = false)]
    [AttachedPropertyBrowsableForType(typeof(TextBox))]
    public static bool GetHighlightTextOnFocus(DependencyObject obj)
    {
        return (bool)obj.GetValue(HighlightTextOnFocusProperty);
    }

    public static void SetHighlightTextOnFocus(DependencyObject obj, bool value)
    {
        obj.SetValue(HighlightTextOnFocusProperty, value);
    }

    private static void HighlightTextOnFocusPropertyChanged(
            DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        var sender = obj as UIElement;
        if (sender != null)
        {
            if ((bool)e.NewValue)
            {
                sender.GotKeyboardFocus += OnKeyboardFocusSelectText;
                sender.PreviewMouseMove += OnMouseMoveSetFocus;
            }
            else
            {
                sender.GotKeyboardFocus -= OnKeyboardFocusSelectText;
                sender.PreviewMouseLeftButtonDown -= OnMouseMoveSetFocus;
            }
        }
    }

    private static void OnKeyboardFocusSelectText(
        object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
    {
        var textBox = e.OriginalSource as TextBox;
        if (textBox != null)
        {
            textBox.SelectAll();
        }
    }

    private static void OnMouseMoveSetFocus(
        object sender, System.Windows.Input.MouseEventArgs e)
    {
        TextBox tb = FindAncestor<TextBox>((DependencyObject)e.OriginalSource);

        if (tb == null)
            return;

        if (!tb.IsKeyboardFocusWithin)
        {
            tb.Focus();
            e.Handled = true;
        }
    }

    static T FindAncestor<T>(DependencyObject current)
        where T : DependencyObject
    {
        current = VisualTreeHelper.GetParent(current);

        while (current != null)
        {
            if (current is T)
            {
                return (T)current;
            }
            current = VisualTreeHelper.GetParent(current);
        };
        return null;
    }
}

在xaml中添加如下:

                 <TextBox Grid.Column="1" 
                          Text="{Binding TestProperty}" 
                          VerticalAlignment="Center" 
                          IsReadOnly="True"
                          HorizontalAlignment="Left"
                          converters:TextBoxProperties.HighlightTextOnFocus="True"
                         />

当然,不要忘记你的命名空间:

          xmlns:converters="clr-namespace:YourProject.Converters"

此外,为了更改TextBox上的突出显示颜色,请尝试使用SelectionBrush:

 <TextBox SelectionBrush="Red" SelectionOpacity="0.5"/>