C# 确定鼠标悬停在FastColoredTextBox中的哪个单词?

C# 确定鼠标悬停在FastColoredTextBox中的哪个单词?,c#,winforms,C#,Winforms,我正在使用该项目,无法确定在触发MouseHover事件时鼠标悬停在哪个单词(或字符位置)上(因此我可以显示有关该单词用法的提示)。我发现了一个类似的方法,尽管它只处理RichTextBox,它方便地使用了这个方法 这可能不太可能,但这里有没有人以这种方式使用FastColoredTextBox并能提供帮助 否则,在源代码中窥探FastColoredTextBox我实际上无法告诉它用于绘制文本的控件。虽然我从它的类声明中怀疑 public partial class FastColoredTex

我正在使用该项目,无法确定在触发MouseHover事件时鼠标悬停在哪个单词(或字符位置)上(因此我可以显示有关该单词用法的提示)。我发现了一个类似的方法,尽管它只处理
RichTextBox
,它方便地使用了这个方法

这可能不太可能,但这里有没有人以这种方式使用
FastColoredTextBox
并能提供帮助

否则,在源代码中窥探
FastColoredTextBox
我实际上无法告诉它用于绘制文本的控件。虽然我从它的类声明中怀疑

public partial class FastColoredTextBox : UserControl, ISupportInitialize
它可能自己做。我想我最好的办法是看看
RickTextBox
如何实现
GetCharIndexFromPosition
,并尝试使用
FastColoredTextBox
重新创建它

关于如何处理这个问题有什么建议吗

EDIT:在FCTB的源代码中,我从
MouseClick
事件开始,因为当用户单击文本时,该事件会移动光标,因此它必须在鼠标下的字符的某个点上移动。在事件处理程序中,我找到了方法
public Place PointToPlace(Point Point)
。看起来这可能就是我要找的


如果这能让我找到解决方案,我会发布一个问题的答案

PointToPlace
返回一个
Place
,它告诉您行和字符索引(在行上)。
PointToPosition
返回绝对字符位置。
我测试了它(在vb.net中…),它不是优化的,而是第一次启动,目前为止运行正常:

Private Sub test_MouseMove(snd As Object, e As MouseEventArgs)
    Dim p As Integer = Me.fctb.PointToPosition(Me.fctb.PointToClient(Windows.Forms.Cursor.Position))
    ' display letter
    Dim ch As String = ""
    If p < Me.fctb.Text.Length Then ch = Me.fctb.Text.ToCharArray()(p)
    Me.Label1.Text = ch
    ' display word
    Me.Label2.Text = GetWord(Me.fctb, p)
End Sub

Private Function GetWord(ct As FastColoredTextBoxNS.FastColoredTextBox, p As Integer) As String
    Dim sb As New StringBuilder(ct.Text)
    If sb.Length = 0 OrElse p = sb.Length Then Return ""
    If Not Regex.IsMatch(sb.Chars(p).ToString, "^\w$") Then Return sb.Chars(p).ToString
    Dim n1 As Integer = p
    While n1 > 0 AndAlso Regex.IsMatch(sb.Chars(n1 - 1).ToString, "^\w$")
        n1 -= 1
    End While
    Dim n2 As Integer = p
    While n2 < sb.Length AndAlso Regex.IsMatch(sb.Chars(n2 + 1).ToString, "^\w$")
        n2 += 1
    End While

    Return sb.ToString.Substring(n1, n2 - n1 + 1)
End Function
私有子测试\u MouseMove(snd作为对象,e作为MouseEventArgs)
Dim p As Integer=Me.fctb.PointTopoPosition(Me.fctb.PointToClient(Windows.Forms.Cursor.Position))
“展示信
Dim ch As String=“”
如果p0并且还有正则表达式IsMatch(sb.Chars(n1-1).ToString,“^\w$”)
n1-=1
结束时
尺寸n2为整数=p
而n2

C#代码:

private void test\u MouseMove(对象snd、MouseEventArgs e){
var p=fctb.PointToPosition(fctb.PointToClient(Windows.Forms.Cursor.Position));
//显示字母
var ch=“”;
如果(p0&&Regex.IsMatch(sb[n1-1].ToString(),@“^\w$”){
n1-=1;
}
var n2=p;
while(n2
好吧,我已经决定,
MouseHover
事件不是我希望显示单词信息的方式。它不会在我想要的时候启动,我想我会使用键盘快捷键来显示我想要的信息

虽然在我退出之前,我确实在C#中提出了这个局部解决方案。它有一些bug。当鼠标悬停在文本的最后一个字上时,有时会出现问题。而且我觉得我把
char
String
混合在一起会导致一些我没有发现的深奥问题

private void fctbEditor_MouseHover(object sender, EventArgs e)
{
    int charIndex = fctbEditor.PointToPosition(fctbEditor.PointToClient(Cursor.Position));
    if (charIndex != fctbEditor.Text.Length && fctbEditor.Text.Length > 0 && fctbEditor.Text[charIndex] != ' ')
    {
        String hoverWord = GetWord(charIndex);

        // Do with the word as you please
        Console.Out.WriteLine(" - Index: " + charIndex + " Char[" + fctbEditor.Text[charIndex].ToString() + "] Word[" + hoverWord + "]");
    }
}

private String GetWord(int charPos)
{
    int revPos = charPos;
    char curChar = fctbEditor.Text[revPos];
    while (curChar != ' ' && curChar != '\n' && revPos > 0)
    {
        revPos -= 1;
        curChar = fctbEditor.Text[revPos];
    }

    int forPos = charPos;
    curChar = fctbEditor.Text[forPos];
    while(curChar != ' ' && curChar != '\n' && forPos < fctbEditor.Text.Length)
    {
        forPos += 1;
        curChar = fctbEditor.Text[forPos];
    }

    return fctbEditor.Text.Substring(revPos + 1, forPos - revPos - 1);
}
private void fctbEditor\u鼠标悬停(对象发送方,事件参数e)
{
int charIndex=fctbEditor.PointToPosition(fctbEditor.PointToClient(Cursor.Position));
如果(charIndex!=fctbEditor.Text.Length&&fctbEditor.Text.Length>0&&fctbEditor.Text[charIndex]!=“”)
{
字符串hoverWord=GetWord(charIndex);
//你想怎么做就怎么做
Console.Out.WriteLine(“-Index:+charIndex+”Char[”+fctbEditor.Text[charIndex].ToString()+“]Word[“+hoverWord+”]);
}
}
私有字符串GetWord(int-charPos)
{
int revPos=charPos;
char curChar=fctbEditor.Text[revPos];
而(curChar!=''&&curChar!='\n'&&revPos>0)
{
revPos-=1;
curChar=fctbEditor.Text[revPos];
}
int-forPos=charPos;
curChar=fctbEditor.Text[forPos];
while(curChar!=''&&curChar!='\n'&&forPos
源代码在它们的位置将文本提取出来,以便在绘制每个字符时保存其在
字典中的位置。然后,您可以将字符与单词进行匹配,即当有空格字符时,它是单词的结尾。然后从这里你可以找到指定点的单词。虽然不是最好的,但是实现起来应该很简单。我想这会让我开始!一旦我创建了一个C#解决方案,我将发布它,但将您的方案标记为适合您工作的解决方案regex
^\w$
匹配什么?@KDecker:我冒昧地将代码转换为C#(可能仍然有一些小错误,但希望没有)。regex
^\w$
匹配单个“单词”(通常为字母、数字和下划线)字符。我可能会告诉我们
private void fctbEditor_MouseHover(object sender, EventArgs e)
{
    int charIndex = fctbEditor.PointToPosition(fctbEditor.PointToClient(Cursor.Position));
    if (charIndex != fctbEditor.Text.Length && fctbEditor.Text.Length > 0 && fctbEditor.Text[charIndex] != ' ')
    {
        String hoverWord = GetWord(charIndex);

        // Do with the word as you please
        Console.Out.WriteLine(" - Index: " + charIndex + " Char[" + fctbEditor.Text[charIndex].ToString() + "] Word[" + hoverWord + "]");
    }
}

private String GetWord(int charPos)
{
    int revPos = charPos;
    char curChar = fctbEditor.Text[revPos];
    while (curChar != ' ' && curChar != '\n' && revPos > 0)
    {
        revPos -= 1;
        curChar = fctbEditor.Text[revPos];
    }

    int forPos = charPos;
    curChar = fctbEditor.Text[forPos];
    while(curChar != ' ' && curChar != '\n' && forPos < fctbEditor.Text.Length)
    {
        forPos += 1;
        curChar = fctbEditor.Text[forPos];
    }

    return fctbEditor.Text.Substring(revPos + 1, forPos - revPos - 1);
}