3

マウスをダブルクリックするのと同じように、AvalonEdit に単語を選択するためのヘルパー メソッドはありますか? SelectWordFromCurrentCaretPosition関数を書くために必要です。

4

2 に答える 2

2

@Danielの回答に基づく私の実装は次のとおりです。

private string GetWordAtMousePosition(MouseEventArgs e)
{
    var mousePosition = this.GetPositionFromPoint(e.GetPosition(this));

    if (mousePosition == null)
        return string.Empty;

    var line = mousePosition.Value.Line;
    var column = mousePosition.Value.Column;
    var offset = Document.GetOffset(line, column);

    if (offset >= Document.TextLength)
        offset--;

    int offsetStart = TextUtilities.GetNextCaretPosition(Document, offset, LogicalDirection.Backward, CaretPositioningMode.WordBorder);
    int offsetEnd = TextUtilities.GetNextCaretPosition(Document, offset, LogicalDirection.Forward, CaretPositioningMode.WordBorder);

    if (offsetEnd == -1 || offsetStart == -1)
        return string.Empty;

    var currentChar = Document.GetText(offset, 1);

    if (string.IsNullOrWhiteSpace(currentChar))
        return string.Empty;

    return Document.GetText(offsetStart, offsetEnd - offsetStart);
}

private void OnMouseMove(object sender, MouseEventArgs e)
{
    string wordUnderCaret = GetWordAtMousePosition(e);
    Debug.Print(wordUnderCaret);
}

デリゲートを MouseMove イベント ハンドラーに追加します。

TextArea.MouseMove += OnMouseMove;
于 2018-06-07T13:21:42.493 に答える
2

いいえ、これは API で公開されていません。EditingCommands MoveLeftByWord(Ctrl+Left) とSelectRightByWord(Ctrl+Shift+Right) を交互に実行することで近づくことができますが、キャレットが単語の先頭に配置されている場合、これは望ましい効果がありません。

EditingCommands.MoveLeftByWord.Execute(null, textEditor.TextArea);
EditingCommands.SelectRightByWord.Execute(null, textEditor.TextArea);

または、これを自分で実装することもできます。単語境界を検出するためのロジックは、として利用できますVisualLine.GetNextCaretPosition(..., CaretPositioningMode.WordBorder)

AvalonEdit ソース コードを調べて、ダブルクリック ロジックがどのように実装されているかを確認できます。SelectionMouseHandler.GetWordAtMousePosition()

CaretNavigationCommandHandlerまた、 Ctrl+Left および Ctrl+Right ショートカットを実装するのソース コードを確認することもできます。

于 2014-11-19T19:25:42.703 に答える