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); 
    } 
相關問題