0

"VisualStudio Tool Window"를 만들기 위해 VSpackage 확장 기능을 구축하고 있습니다. 숫자로 구성된 도구 창 내부에 격자가 있습니다. 사용자가 그리드의 특정 행을 선택하는 경우. 특정 코드 행을 강조 표시해야합니다. VSPackage를 사용하여 라인 번호를 기반으로 Visual Studio의 코드 창을 강조 표시합니다.

은 더 명확하게하려면 내 그리드에 포함 된 가정 :

행 중 1 - 10, 행 2-15, 행 3-14,

사용자가에서 행 한 후, 10 라인을 선택하는 경우 코드 창이 강조 표시되어야합니다. VisualStudio 패키지를 사용하여이 기능을 사용할 수 있습니까? 나는 이것이 가능하다는 강한 느낌을 가지고 있습니다. 왜냐하면 대부분의 검색 결과가 그런 식으로 작동하기 때문입니다.

동일한 도움말은 크게 감사드립니다 !!

답변

0

나는 마침내 많은 인터넷 검색을 기반으로 내 게시물에 대한 답변을 발견했습니다. 희망이 다른 사람들에게 도움이됩니다.

"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 
          { 
          } 
         } 
        } 
       } 
      } 
     } 
0

더 간단한 솔루션을 사용할 수도 있습니다. 아래 참조.

int lineNo = 3; 
    if (!isProjectItemOpen) 
    { 
     Window win = projectItem.Open(); 
     win.Visible = true; 
     Document doc = win.Document; 
     doc.Activate(); 
     var ts = dte.ActiveDocument.Selection; 
     ts.GotoLine(lineNo, true); 
    }