如何在"我的自定义工具"中写入Visual Studio输出窗口?

Jef*_*tin 34 vsx customtool visual-studio

我正在编写一个自定义工具,我目前正在做我想要的功能.如果出现问题,我希望能够写入Visual Studio.(代码格式不正确或其他).

这有什么标准吗?现在我基本上可以强制该工具失败,Visual Studio会发出警告,表明它已经这样做了.我想在"输出"窗口中输入一个类别,其中包含我要发送的任何结果消息.我还可以在错误列表窗口中使用更具描述性的任务/警告.

Ale*_*lex 58

输出窗口

要在Visual Studio中写入"常规"输出窗口,您需要执行以下操作:

IVsOutputWindow outWindow = Package.GetGlobalService( typeof( SVsOutputWindow ) ) as IVsOutputWindow;

Guid generalPaneGuid = VSConstants.GUID_OutWindowGeneralPane; // P.S. There's also the GUID_OutWindowDebugPane available.
IVsOutputWindowPane generalPane;
outWindow.GetPane( ref generalPaneGuid , out generalPane );

generalPane.OutputString( "Hello World!" );
generalPane.Activate(); // Brings this pane into view
Run Code Online (Sandbox Code Playgroud)

但是,如果要写入自定义窗口,则需要执行以下操作:

IVsOutputWindow outWindow = Package.GetGlobalService( typeof( SVsOutputWindow ) ) as IVsOutputWindow;

// Use e.g. Tools -> Create GUID to make a stable, but unique GUID for your pane.
// Also, in a real project, this should probably be a static constant, and not a local variable
Guid customGuid = new Guid("0F44E2D1-F5FA-4d2d-AB30-22BE8ECD9789");
string customTitle = "Custom Window Title";
outWindow.CreatePane( ref customGuid, customTitle, 1, 1 );

IVsOutputWindowPane customPane;
outWindow.GetPane( ref customGuid, out customPane);

customPane.OutputString( "Hello, Custom World!" );
customPane.Activate(); // Brings this pane into view
Run Code Online (Sandbox Code Playgroud)

有关IVsOutputWindowIVsOutputWindowPane的详细信息,请访问 MSDN.

错误列表

为了将项添加到错误列表,IVsSingleFileGenerator有一个方法调用void Generate(...),其具有类型的参数IVsGeneratorProgress.此接口有一个方法void GeneratorError(),可让您向Visual Studio错误列表报告错误和警告.

public class MyCodeGenerator : IVsSingleFileGenerator
{
    ...
    public void Generate( string inputFilePath, string inputFileContents, string defaultNamespace, out IntPtr outputFileContents, out int output, IVsGeneratorProgress generateProgress )
    {
        ...
        generateProgress.GeneratorError( false, 0, "An error occured", 2, 4);
        ...
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

可以在MSDN上找到GeneratorError()的详细信息.


Ian*_*vis 9

还有另一种方法Marshal.GetActiveObject可以获取正在运行的DTE2实例.

首先参考EnvDTE和envdte80.这目前在VisualStudio 2012中有效,我还没有尝试过其他的.

using System;
using System.Runtime.InteropServices;
using EnvDTE;
using EnvDTE80;

internal class VsOutputLogger
{
    private static Lazy<Action<string>> _Logger = new Lazy<Action<string>>( () => GetWindow().OutputString );

    private static Action<string> Logger
    {
        get { return _Logger.Value; }
    }

    public static void SetLogger( Action<string> logger )
    {
        _Logger = new Lazy<Action<string>>( () => logger );
    }

    public static void Write( string format, params object[] args)
    {
        var message = string.Format( format, args );
        Write( message );
    }

    public static void Write( string message )
    {
        Logger( message + Environment.NewLine );
    }

    private static OutputWindowPane GetWindow()
    {
        var dte = (DTE2) Marshal.GetActiveObject( "VisualStudio.DTE" );
        return dte.ToolWindows.OutputWindow.ActivePane;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 虽然这有效,但如果您打开了多个Visual Studio实例,则该消息可能最终会在您打开的第一个实例中结束. (2认同)