gpt4 book ai didi

c# - 如何获得 EnvDte.ActiveDocument 的 IWpfTextView?

转载 作者:太空狗 更新时间:2023-10-29 23:31:40 26 4
gpt4 key购买 nike

我正在尝试操纵 Visual Studio 文本编辑器滚动条值。问题是我只有 dte.ActiveDocument,不可能从那里完成。

当 VS 启动并且我捕获 dte.Events.CommandEvents 时,我的扩展只加载一次。在某些时候,我想更改 ActiveDocument 的滚动条值。为此,我需要 IWpfTextView 或 ITextView。您知道如何获取该对象的实例吗?

internal class MyExtension
{
private CommandEvents commandEvents;

private DTE dte;

public MyExtension(DTE dte)
{
this.dte = dte;
commandEvents = dte.Events.CommandEvents;
commandEvents.BeforeExecute += commandEvents_BeforeExecute;
}

void commandEvents_BeforeExecute(string Guid, int ID, object CustomIn, object CustomOut, ref bool CancelDefault)
{
var doc = dte.ActiveDocument
// CHANGE SCROLLBAR VALUES HERE
}
}

最佳答案

我找到了一种方法来做到这一点。在扩展的主类中,我得到了 SVsTextManager

public sealed class MyExtensionPackage : Package
{
protected override void Initialize()
{
DTE dte = (DTE)base.GetService(typeof(DTE));
var txtMgr = (IVsTextManager)base.GetService(typeof(SVsTextManager));
plugin = new MyExtension(dte, txtMgr);
base.Initialize();
}
}

internal class MyExtension
{
private CommandEvents commandEvents;

private DTE dte;
private IVsTextManager txtMngr;

public MyExtension(DTE dte, IVsTextManager txtMngr)
{
this.txtMngr = txtMngr;
this.dte = dte;
commandEvents = dte.Events.CommandEvents;
commandEvents.BeforeExecute += commandEvents_BeforeExecute;
}

void commandEvents_BeforeExecute(string Guid, int ID, object CustomIn, object CustomOut, ref bool CancelDefault)
{
var doc = dte.ActiveDocument

IVsTextView textViewCurrent;
txtMngr.GetActiveView(1, null, out textViewCurrent);
int a, b, c, verticalScrollPosition;

var scrollInfo = textViewCurrent.GetScrollInfo(1, out a, out b, out c, out verticalScrollPosition);
textViewCurrent.SetScrollPosition(1, verticalScrollPosition);
}
}

关于c# - 如何获得 EnvDte.ActiveDocument 的 IWpfTextView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20101167/

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