C# Richtextbox mousemove事件文本是什么颜色

C# Richtextbox mousemove事件文本是什么颜色,c#,colors,richtextbox,mousemove,C#,Colors,Richtextbox,Mousemove,是否有办法使用mousemove事件确定RichTextBox中的文本是什么颜色?我希望避免使用Richtextbox.Select,因为它会在鼠标移动的任何地方添加一个小的选择栏 private void rtbComputerstatus_MouseMove(object sender, MouseEventArgs e) { int c = rtbComputerstatus.GetCharIndexFromPosition(new Point(e.X, e.Y)); r

是否有办法使用
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;
}

只是在黑暗中开了一枪,但下面的方法行得通吗

    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;

    }

你可以尝试这样的东西,从这个修改,它会给你的颜色的像素,是在鼠标下

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;
    }
}

我可能错了,但我认为,
ForeColor
是RichTextBox的一个全局属性。它没有给你那个位置的颜色。不幸的是,这正是他在最初的问题中试图避免的。在我的机器上没有。它没有选择任何文本,只是检查所选文本。当我运行他的测试代码时,我确实得到了被选中的文本,这是我认为他试图避免的。我的代码没有选择任何内容,只是检查所选内容是否为“蓝色”。在这种情况下,它将永远不会检查鼠标光标下的当前内容,而这正是他试图执行的操作。它只会在当前插入符号位置检查文本。这很公平。我想我对这次行动的目的有另一种解释。如果您更改
rtbComputerstatus,请选择(c,1)
RTB计算机状态。选择(c,0)那么你至少不会得到蓝色的大选择框,即使你确实在框周围跟随鼠标获得插入符号。这可能会起作用,有没有办法只在富文本框内隐藏插入符号?可能。我知道可以使用Windows API来完成。不过,老实说,你最好还是使用马克·霍尔的解决方案。+1当我在本地测试它时,它会起作用。当你发布它时,我正试图想出一个类似的解决方案。谢谢马克,当我的鼠标位于Richtextbox中的蓝色像素上时,这个解决方案会起作用,但当鼠标移动到字母之间的空白处时,它会回到正常的光标。它似乎对像素太精确了,增加位图(1,1)会增加其“大小”吗?是的,但即使你这样做了,你仍然只是测试1个像素,在这种情况下是左上角。你需要检查所有的颜色,看看是否大多数是蓝色的。我正在努力寻找一种可以降低MouseMove逻辑灵敏度的解决方案在我的情况下,我知道我的蓝色文本会说:“点击RDP”我可以得到该字符串的像素宽度和高度,并将其用作位图大小,然后循环通过每个像素,如果循环检测到任何蓝色,停止循环并将光标更改为手?如果这样做有意义,那么您可以这样做,但您的主要问题是您的MouseMove事件非常嘈杂,它会在短时间内生成多个事件。您可能需要在MouseEnter事件上触发计时器,将上述逻辑放入其中,并在MouseLeave事件上停止。我不确定你到底希望你的最终结果是什么,或者一旦你检测到你的文本,你想做什么,所以要更具体一点有点困难。