← 返回所有博客文章

在本篇文章中我们将讨论如何在 C1RichBoxTextBox 中实现更改鼠标形状的功能。例如,我们的 RichTextBox 文档中包含 Paragraph 和 Table 。我们想实现当鼠标悬浮在 Table 上时改变鼠标形状,当鼠标离开 Table 时还原鼠标形状。

效果图

MouseCursorChanging

首先需要准备文档中显示的内容:

  1:    string myHTMLString =
  2: @"<html><head><style type='text/css'>.c0 { font-family: 'Verdana'; font-size: 18.6666666666667px } .c1 { margin: 0px 0px 12px } .c2 { font-family: 'Arial'; font-size: 13.3333333333333px } .c3 { margin: 0px 0px 10px } .c4 { border-collapse: collapse; width: 100% } .c5 { width: 33% } .c6 { border-color: Black; border-style: solid; border-width: thin; padding: 0px 7px } .c7 { margin: 0px } .c8 { font-family: 'Arial'; font-size: 13.33px } </style></head><body class='c0'><p class='c1'><span class='c2'>This table has 2 rows and 3 columns. Mouse pointer wil change to hand cursor when hovered over the table text.</span></p><p class='c3'><span class='c2'>&#x200b;</span></p><table class='c4'><col class='c5'/><col class='c5'/><col class='c5'/><tr><td class='c6'><p class='c7'>row1 col1</p></td><td class='c6'><p class='c7'>row1 col2</p></td><td class='c6'><p class='c7'>row1 col3</p></td></tr><tr><td class='c6'><p class='c7'>row2 col1</p></td><td class='c6'><p class='c7'>row2 col2</p></td><td class='c6'><p class='c7'>row2 col3</p></td></tr></table><p class='c3'><span class='c2'>&#x200b;</span></p><div><p class='c1'><span class='c8'>This table has 2 rows and 3 columns. Mouse pointer wil change to hand cursor when hovered over the table text.</span></p></div><p class='c3'><span class='c2'>&#x200b;</span></p></body></html>";

 

通过 ConvertToDocument 方法转换为 C1RichTextBox 文档内容:

  1: C1RichTextBox1.Document = C1RichTextBox1.HtmlFilter.ConvertToDocument(myHTMLString);


通过 ComponentOne RichTextBoxElementMouseMove  事件来实现鼠标形状切换功能:

  1: void C1RichTextBox1_ElementMouseMove(object sender, MouseEventArgs e)
  2:    {
  3:       var element = sender as C1TextElement;
  4:       if (element != null && element.GetParents().Any(p => p is C1Table))
  5:          {
  6:             C1RichTextBox1.Cursor = Cursors.Hand;
  7:          }
  8:       else
  9:          {
 10:              C1RichTextBox1.Cursor = Cursors.IBeam;
 11:          }
 12:    }

 

Demo 下载:

C#+VS2010+WPF+Studio for WPF 2013V2.5 点击下载