如何在RichTextBox中将鼠标指针悬停在粗体单词上时更改光标?

3

当鼠标指针悬停在RichTextBox中的粗体单词上时,我希望将光标更改为HAND。如何实现?

2个回答

5
将此函数添加到richtextbox的OnMouseMove事件中。
private void richTextBox2_MouseMove(object sender, MouseEventArgs e)
        {
            int c = richTextBox2.GetCharIndexFromPosition(new Point(e.X, e.Y));
            richTextBox2.Select(c, 1);
            if (richTextBox2.SelectionFont.Bold)
            {
                richTextBox2.Cursor = Cursors.Hand;
            }
            else
            {
                richTextBox2.Cursor = Cursors.Default;
            }

        }

你只需要1个字符就可以知道它是否为粗体。

1
  • 注册一个 OnMouseMove 处理程序
  • 调用 GetCharIndexFormPosition
  • 确定该索引是否在加粗字符上方
  • 根据需要设置 Cursor 属性。

谢谢您的回答,但我的问题是第三步(•确定该索引是否在粗体字符上方),如何在不更改 SelectionStart 和 SelectionLength 的情况下确定? - Amir Saniyan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接