在 RichTextBox 上显示工具提示 [英] Show ToolTip on RichTextBox

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

问题描述

我在 WPF 窗口上有一个 RichTextBox.现在,我想在用户将鼠标移到 RichTextBox 上时显示一个工具提示.RichTextBox 的内容应该取决于鼠标指针下的文本.为此,我应该获取鼠标显示的字符的位置.

I have a RichTextBox on a WPF Window. Now, I want to show a ToolTip when the user move the mouse over the RichTextBox. The Content of the RichTextBox should depend on the Text which is under the mouse pointer. For this I should get the position of the char, on which the mouse shows to.

最好的问候,托马斯

推荐答案

在以下示例中,工具提示将显示插入符号所在的下一个字符.

In the following example the tooltip will show the next character where the caret is.

XML:

<Window x:Class="Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
  <RichTextBox ToolTipOpening="rtb_ToolTipOpening" ToolTip="" />
</Window>

代码隐藏:

void rtb_ToolTipOpening(object sender, ToolTipEventArgs e)
{
  RichTextBox rtb = sender as RichTextBox;

  if (rtb == null)
    return;

  TextPointer position = rtb.GetPositionFromPoint(Mouse.GetPosition(rtb), false);
  if (position == null)
    return;

  int offset = rtb.Document.ContentStart.GetOffsetToPosition(position);

  position = rtb.Document.ContentStart.GetPositionAtOffset(offset);
  if (position == null)
    return;

  string text = position.GetTextInRun(LogicalDirection.Forward);

  rtb.ToolTip = !string.IsNullOrEmpty(text) ? text.Substring(0, 1) : string.Empty;
}

这篇关于在 RichTextBox 上显示工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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