【问题标题】:Richtextbox mousemove event what color the text isRichtextbox mousemove 事件文本是什么颜色
【发布时间】:2013-05-04 15:39:11
【问题描述】:

有没有办法使用mousemove 事件来确定RichTextBox 中的文本是什么颜色?我想避免使用Richtextbox.Select,因为它会在鼠标移动的任何地方添加一个小选择栏。

private void rtbComputerstatus_MouseMove(object sender, MouseEventArgs e)
{
    int c = rtbComputerstatus.GetCharIndexFromPosition(new Point(e.X, e.Y));

    rtbComputerstatus.Select(c, 1);

    if (rtbComputerstatus.SelectionColor == Color.Blue)
        rtbComputerstatus.Cursor = Cursors.Hand;
    else
        rtbComputerstatus.Cursor = Cursors.Default;
}

【问题讨论】:

  • 如果您将rtbComputerstatus.Select(c, 1); 更改为rtbComputerstatus.Select(c, 0);,那么您至少不会看到蓝色的大选择框,即使您的鼠标在框周围出现插入符号也是如此。
  • 这可行,有没有办法只在富文本框中隐藏插入符号?
  • 可能。我知道可以使用 Windows API 来完成。不过,老实说,最好还是采用 Mark Hall 的解决方案。

标签: c# colors richtextbox mousemove


【解决方案1】:

你可以试试这样的东西,从这个MSDN Forum answer of JoOls修改,它会给你鼠标下像素的颜色。

private void rtbComputerstatus_MouseMove(object sender, MouseEventArgs e)
{
    Point  cursorPoint = Cursor.Position;
    Bitmap bm = new Bitmap(1, 1);
    Graphics g  = Graphics.FromImage(bm);
    g.CopyFromScreen(cursorPoint, new Point(), new Size(1, 1));
    Color pixelColor = bm.GetPixel(0, 0);
    g.Dispose();
    bm.Dispose();
    if (pixelColor.ToArgb().Equals(Color.Blue.ToArgb()))
    {
        if (rtbComputerstatus.Cursor != Cursors.Hand)
            rtbComputerstatus.Cursor = Cursors.Hand;
    }
    else
    {
        if(rtbComputerstatus.Cursor != Cursors.Default)
            rtbComputerstatus.Cursor = Cursors.Default;
    }
}

【讨论】:

  • +1 这在我本地测试时有效。当您发布它时,我正试图找出像这样的解决方案。
  • 谢谢马克,当我的鼠标在 Richtextbox 中的一个蓝色像素上时有效,但是当鼠标移动到字母之间的空白处时,它会回到正常光标。似乎它在像素上太准确了,增加位图(1,1)会增加它的“大小”吗?
  • 是的,但即使你这样做了,你仍然只是测试 1 个像素,在这种情况下是左上角。您需要检查所有这些以查看大多数是否为蓝色。我正在尝试一种可以降低 MouseMove Logic 灵敏度的解决方案
  • 在我的情况下,我知道我的蓝色文本会说什么:“单击到 RDP”我可以获取该字符串的像素宽度和高度并将其用作位图大小,然后循环遍历每个像素并如果循环检测到任何蓝色,停止循环并将光标更改为手?如果这有意义
  • 是的,您可以这样做,但您的主要问题是您的 MouseMove 事件非常嘈杂,它会在短时间内生成多个事件。您可能想做一些事情,比如在 MouseEnter 事件上触发一个计时器,将上述逻辑放入其中并在 MouseLeave 事件上停止它。我不确定您希望最终结果是什么,或者在检测到文本后您想要做什么,因此很难更具体。
【解决方案2】:

只是在黑暗中的一个镜头,但下面的工作会奏效吗?

    private void rtbComputerstatus_MouseMove(object sender, MouseEventArgs e)
    {
        if (rtbComputerstatus.ForeColor.ToKnownColor() == KnownColor.Blue)
            rtbComputerstatus.Cursor = Cursors.Hand;
        else
            rtbComputerstatus.Cursor = Cursors.Default;
    }

我认为这只会检查选定的文本:

    private void rtbComputerstatus_MouseMove(object sender, MouseEventArgs e)
    {

        if (rtbComputerstatus.SelectionColor.ToKnownColor() == KnownColor.Blue)
            rtbComputerstatus.Cursor = Cursors.Hand;
        else
            rtbComputerstatus.Cursor = Cursors.Default;

    }

【讨论】:

  • 我可能是错的,但我认为ForeColor 是 RichTextBox 的全局属性。它不会给你那个位置的颜色。
  • 不幸的是,这正是他在最初的问题中试图避免的。
  • 不在我的机器上它没有。它没有选择任何文本,只是检查选定的文本。当我运行他的测试代码时,我确实选择了文本,这是我认为他试图避免的。我的代码没有选择任何东西,只是检查选择的是“蓝色”。
  • 在这种情况下,它永远不会检查鼠标光标下当前的内容,而这正是他正在尝试做的事情。它只会检查当前插入符号位置的文本。
  • 很公平。我想我对 OP 所追求的有另一种解释。我会请你让开:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-11
  • 1970-01-01
  • 1970-01-01
  • 2011-07-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多