wpf的RichTextBox能不能做到这样的效果:当鼠标移动到某些高亮字的时候,ToolTip出现,提示相关信息?

mxkycwqw 2012-04-09 03:29:39
就像我们在 VisualStudio 2010的中编写代码的时候,把鼠标移动到那些高亮字的时候,都有相应的提示出现。
...全文
243 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
startstartsvip 2012-04-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

RichTextBox 的内容包括:
Inline
InlineUIContainer
Run
Span
Bold
Hyperlink
Italic
Underline
LineBreak
你将高亮显示的内容放在Run块中
再设置Run块的ToolTip就可以了
run1.ToolTip = "xxx";
[/Quote]

额,测试了以下,正解

WPF 中还真是有简单方法
ohkuy 2012-04-09
  • 打赏
  • 举报
回复
RichTextBox 的内容包括:
Inline
InlineUIContainer
Run
Span
Bold
Hyperlink
Italic
Underline
LineBreak
你将高亮显示的内容放在Run块中
再设置Run块的ToolTip就可以了
run1.ToolTip = "xxx";
mngzilin 2012-04-09
  • 打赏
  • 举报
回复
重写RichTextBox也行,将InitializeComponent();里面的RichTextBox的类型改成MyRichTextBox ,
private MyRichTextBox richTextBox1;
this.richTextBox1 = new MyRichTextBox();
   class MyRichTextBox : RichTextBox
{
System.Timers.Timer timer;
int cout = 0;
Point pt = new Point();
ToolTip tooltip = new ToolTip();
protected override void OnCreateControl()
{
base.OnCreateControl();
timer = new System.Timers.Timer();
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Interval = 500;
timer.Enabled = false;
}
protected override void OnMouseHover(EventArgs e)
{
base.OnMouseHover(e);
timer.Enabled = true;
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
timer.Enabled = false;
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (!timer.Enabled)
timer.Enabled = true;
cout = 0;
pt.X = e.Location.X;
pt.Y = e.Location.Y;
}
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (cout >= 1000)
{
timer.Enabled = false;
func();
}
else
cout += 500;
}
void func()
{
this.Invoke(new MethodInvoker(delegate
{
int index = this.GetCharIndexFromPosition(pt);
string ret = "";
for (int i = index; i < this.Text.Length; i++)
{
char c = this.Text[i];
if ((c > 64 && c < 91) || (c > 96 && c < 123) || (c > 47 && c < 58))
{
ret += c.ToString();
}
else
break;
}
for (int i = index; i - 1 > 0; i--)
{
char c = this.Text[i - 1];
if ((c > 64 && c < 91) || (c > 96 && c < 123) || (c > 47 && c < 58))
{
ret = c.ToString() + ret;
}
else
break;
}
if (ret.Length > 0)
{
//textBox1.Text = ret;
tooltip.Show("当前停留的文本内容是:" + ret, this, pt, 5000);
}
}));
}
}
mngzilin 2012-04-09
  • 打赏
  • 举报
回复
可以,这个得原理很简单,在界面上放置一个richtextbox,然后添加如代码中所示的事件即可,
这个例子是当鼠标停留在文本上面时候获取停留单词文本
如果你要停留在高亮的内容上才显示提示文本,那么你最好把高亮的内容索引范围存入List中,这部分自己实现吧,基本功能有了都。
   public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
System.Timers.Timer timer;
int cout = 0;
Point pt = new Point();
ToolTip tooltip = new ToolTip();
private void Form1_Load(object sender, EventArgs e)
{
timer= new System.Timers.Timer();
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Interval = 500;
timer.Enabled = false;
}

void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (cout >= 1000)
{
timer.Enabled = false;
func();
}
else
cout += 500;
}

private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
{
if (!timer.Enabled)
timer.Enabled = true;
cout = 0;
pt.X = e.Location.X;
pt.Y = e.Location.Y;
}
private void richTextBox1_MouseHover(object sender, EventArgs e)
{
timer.Enabled = true;
}

private void richTextBox1_MouseLeave(object sender, EventArgs e)
{
timer.Enabled = false;
}
void func()
{
this.Invoke(new MethodInvoker(delegate
{
int index = richTextBox1.GetCharIndexFromPosition(pt);
string ret = "";
for (int i = index; i < richTextBox1.Text.Length; i++)
{
char c=richTextBox1.Text[i];
if ((c > 64 && c < 91) || (c > 96 && c < 123) || (c > 47 && c < 58))
{
ret += c.ToString();
}
else
break;
}
for (int i = index; i-1 >0; i--)
{
char c=richTextBox1.Text[i-1];
if ((c > 64 && c < 91) || (c > 96 && c < 123) || (c > 47 && c < 58))
{
ret = c.ToString() + ret;
}
else
break;
}
if (ret.Length > 0)
{
//textBox1.Text = ret;
tooltip.Show("当前停留的文本内容是:"+ret, richTextBox1, pt, 5000);
}
}));
}
}
startstartsvip 2012-04-09
  • 打赏
  • 举报
回复
当然可以,不用wpf 都可以,

但是 wpf 可能更简单,还是等高手来解答吧

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧