Visual Studio 扩展:如何获取调用上下文菜单的行?
Visual Studio Extension: How to get the line on which Context Menu was called?
我想创建一个 VS 扩展,我需要在其中知道调用菜单的行号。我找到了一个 VisualBasic implementation 和一个 似乎 可以执行此操作的宏,但我不知道如何在 C# 中启动它。目标是知道调用 ContextMenu
的行的确切编号,以便在其上放置占位符图标,就像断点一样。感谢有用的链接,因为我找不到太多关于这个主题的信息。
您可以创建一个 VSIX 项目并在您的项目中添加一个命令项。然后在 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);
}
我想创建一个 VS 扩展,我需要在其中知道调用菜单的行号。我找到了一个 VisualBasic implementation 和一个 似乎 可以执行此操作的宏,但我不知道如何在 C# 中启动它。目标是知道调用 ContextMenu
的行的确切编号,以便在其上放置占位符图标,就像断点一样。感谢有用的链接,因为我找不到太多关于这个主题的信息。
您可以创建一个 VSIX 项目并在您的项目中添加一个命令项。然后在 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);
}