【问题标题】:Highlighting code window of visual studio based on line number using VSPackage使用VSPackage基于行号突出显示Visual Studio的代码窗口
【发布时间】:2013-03-18 11:36:18
【问题描述】:

我正在构建一个 VSpackage 扩展来创建“VisualStudio 工具窗口”。 我在工具窗口中有一个网格,由数字组成。如果用户选择网格的特定行。应该突出显示该特定代码行。

为了更清楚, 假设我的网格包含:

第 1 - 10 行, 第 2 - 15 行, 第 3 - 14 行,

如果用户选择第 1 行,则代码窗口中的第 10 行应突出显示。 使用 VisualStudio 包是否可以使用此功能。我有一种强烈的感觉,这是可能的。因为大多数搜索结果都是这样工作的。

非常感谢您的任何帮助!

【问题讨论】:

    标签: visual-studio-2010 visual-studio c#-4.0 vspackage visual-studio-sdk


    【解决方案1】:

    我终于根据大量的谷歌搜索找到了我的帖子的答案。希望这对其他人有所帮助。

    您需要使用“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
                                {
                                }
                            }
                        }
                    }
                }
            }
    

    【讨论】:

      【解决方案2】:

      您也可以使用更简单的解决方案;见下文

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

      【讨论】:

        猜你喜欢
        • 2019-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-13
        相关资源
        最近更新 更多