0

我想创建一个VS扩展,我需要知道菜单被调用的行号。我发现一个VisualBasic implementation与宏,似乎要做到这一点,但我不知道如何在C#中启动此。目标是要知道ContextMenu被调用的确切数字,以便在其上放置一个占位符图标,就像一个断点一样。我很欣赏有用的链接,因为我无法在这个主题上找到太多内容。Visual Studio扩展:如何获取调用上下文菜单的行?

+0

见https://stackoverflow.com/questions/32502847/is-there-any-extension-for-vs-copying-code-position –

+0

你能提供一个例子就如何使用它?该示例的第一行实际上通过链接提供,'EnvDTE.TextSelection ts = DTE.ActiveWindow.Selection as EnvDTE.TextSelection;'给我错误: \t _对象引用对于非静态字段,方法,或属性'_DTE.ActiveWindow'_。 – rTECH

+0

要获得DTE对象,请参阅https://stackoverflow.com/questions/19087186/how-to-acquire-dte-object-instance-in-a-vs-package-project –

回答

2

您可以创建一个VSIX项目并在您的项目中添加一个Command项目。然后在MenuItemCallback()方法中添加以下代码以获取代码行号。

private void MenuItemCallback(object sender, EventArgs e) 
    { 
     EnvDTE.DTE dte = (EnvDTE.DTE)this.ServiceProvider.GetService(typeof(EnvDTE.DTE)); 

     EnvDTE.TextSelection ts = dte.ActiveWindow.Selection as EnvDTE.TextSelection; 
     if (ts == null) 
      return; 
     EnvDTE.CodeFunction func = ts.ActivePoint.CodeElement[vsCMElement.vsCMElementFunction] 
        as EnvDTE.CodeFunction; 
     if (func == null) 
      return; 

     string message = dte.ActiveWindow.Document.FullName + System.Environment.NewLine + 
      "Line " + ts.CurrentLine + System.Environment.NewLine + 
      func.FullName; 

     string title = "GetLineNo"; 

     VsShellUtilities.ShowMessageBox(
      this.ServiceProvider, 
      message, 
      title, 
      OLEMSGICON.OLEMSGICON_INFO, 
      OLEMSGBUTTON.OLEMSGBUTTON_OK, 
      OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST); 
    } 
相关问题