gpt4 book ai didi

c# - Visual Studio 扩展 : How to get the line on which Context Menu was called?

转载 作者:太空宇宙 更新时间:2023-11-03 22:54:35 25 4
gpt4 key购买 nike

我想创建一个 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);
}

关于c# - Visual Studio 扩展 : How to get the line on which Context Menu was called?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46006614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com