2

私は Visual Studio Extensions の初心者です。Visual Studio 2010 ツール ウィンドウからコード ウィンドウを操作する方法はありますか。VisualStudio ツール ウィンドウでホストされている Datagrid があります。DataGrid には、ClassName、MethodName などが含まれます。className/MethodName をクリックすると、次のことを実現する必要があります

  1. className/MethodName を含む特定の .cs ファイルを開きます
  2. 特定のクラス名/メソッド名をハイライトします。

「IWpfTextView」クラスを使用してこれを達成できることは知っていますが、方法はわかりません。多くのグーグル検索を行いましたが、無駄でした。以下のリンクでさえ、未回答のリンクのままです。

上記のヘルプは大歓迎です。

4

2 に答える 2

2

実は私もほぼ同じことをしました。Visual Localizerで完全なソース コードを確認できます。

基本的に、次の 2 つのことを行う必要があります。

  1. ファイルの IVsTextView インスタンスを取得する
  2. パラメータとして範囲(開始行、終了行、開始列、終了列)を取る SetSelection() メソッドを呼び出します。また、EnsureSpanVisible() を呼び出して下にスクロールすることもできます。

IVsTextView の取得も非常に簡単です。私のプロジェクト (Visual Localizer) には、VLlib/components にある DocumentViewsManager というクラスがあり、GetTextViewForFile() という非常に単純なメソッドがあり、引数としてファイル パスのみを取ります。次のことを行います。

  1. VsShellUtilities.IsDocumentOpen メソッドを使用して、指定されたファイル パスの IVsWindowFrame を取得します。
  2. これを VsShellUtilities.GetTextView メソッドに渡すと、IVsTextView が返されます。

それが役に立てば幸い。

于 2013-03-21T19:09:48.507 に答える
0

ありがとうcre8or。同じことを行う代替手段を見つけました。

上記のコード行を強調表示するには、「TextSelection」クラスを使用する必要があります。

    foreach (CodeElement codeElement in projectItem.FileCodeModel.CodeElements)// search for the function to be opened
    {
        // get the namespace elements
        if (codeElement.Kind == vsCMElement.vsCMElementNamespace)
        {
            foreach (CodeElement namespaceElement in codeElement.Children)
            {
                // get the class elements
                if (namespaceElement.Kind == vsCMElement.vsCMElementClass || namespaceElement.Kind == vsCMElement.vsCMElementInterface)
                {
                    foreach (CodeElement classElement in namespaceElement.Children)
                    {
                        try
                        {
                            // get the function elements to highlight methods in code window
                            if (classElement.Kind == vsCMElement.vsCMElementFunction)
                            {
                                if (!string.IsNullOrEmpty(highlightString))
                                {
                                    if (classElement.Name.Equals(highlightString, StringComparison.Ordinal))
                                    {
                                        classElement.StartPoint.TryToShow(vsPaneShowHow.vsPaneShowTop, null);

                    classElement.StartPoint.TryToShow(vsPaneShowHow.vsPaneShowTop, null);

                        // get the text of the document
                     EnvDTE.TextSelection textSelection = window.Document.Selection as EnvDTE.TextSelection;

                        // now set the cursor to the beginning of the function
                        textSelection.MoveToPoint(classElement.StartPoint);
                        textSelection.SelectLine();

                                    }
                                }
                            }
                        }
                        catch
                        {
                        }
                    }
                }
            }
        }
    }
于 2013-04-16T13:30:33.330 に答える