Dsl学习笔记-5

今天介绍的是DTE。

msdn介绍:

Visual Studio 自动化对象模型中的顶级对象。使用此对象可以实现功能,有关此对象的文档,请参考 _DTE

            名称说明
公共属性 ActiveDocument 获取活动文档。 (继承自 _DTE。)
公共属性 ActiveSolutionProjects 获取当前选定项目的数组。 (继承自 _DTE。)
公共属性 ActiveWindow 返回当前活动窗口,如果没有其他活动窗口,则返回最顶层的窗口。 (继承自 _DTE。)
公共属性 AddIns 获取包含当前可用的所有外接程序的 AddIns 集合。 (继承自 _DTE。)
公共属性 Application 基础结构。仅由 Microsoft 内部使用。 (继承自 _DTE。)
公共属性 CommandBars 获取对开发环境的命令栏的引用。 (继承自 _DTE。)
公共属性 CommandLineArguments 获取一个表示命令行参数的字符串。 (继承自 _DTE。)
公共属性 Commands 返回 Commands 集合。 (继承自 _DTE。)
公共属性 ContextAttributes 获取 ContextAttributes 集合,该集合允许自动化客户端将新特性添加到“动态帮助”窗口中的当前所选项,并为其他特性提供上下文帮助。 (继承自 _DTE。)
公共属性 Debugger 获取调试器对象。 (继承自 _DTE。)
公共属性 DisplayMode 获取显示模式(MDI 或选项卡式文档)。 (继承自 _DTE。)
公共属性 Documents 获取开发环境中打开的文档的集合。 (继承自 _DTE。)
公共属性 DTE 获取顶级扩展性对象。 (继承自 _DTE。)
公共属性 Edition 获取环境版本的说明。 (继承自 _DTE。)
公共属性 Events 获取对 Events 对象的引用。 (继承自 _DTE。)
公共属性 FileName 基础结构。仅由 Microsoft 内部使用。 (继承自 _DTE。)
公共属性 Find 获取表示全局文本查找操作的 Find 对象。 (继承自 _DTE。)
公共属性 FullName 获取对象文件的完整路径和名称。 (继承自 _DTE。)
公共属性 Globals 获取 Globals 对象,该对象包含可以保存在解决方案 (.sln) 文件、项目文件或用户的配置文件数据中的外接程序值。 (继承自 _DTE。)
公共属性 IsOpenFile 基础结构。仅由 Microsoft 内部使用。 (继承自 _DTE。)
公共属性 ItemOperations 获取 ItemOperations 对象。 (继承自 _DTE。)
公共属性 LocaleID 获取运行开发环境所使用的区域设置的 ID。 (继承自 _DTE。)
公共属性 Macros 获取 Macros 对象。 (继承自 _DTE。)
公共属性 MacrosIDE 获取宏 IDE 自动化模型的根。 (继承自 _DTE。)
公共属性 MainWindow 获取一个表示主开发环境窗口的 Window 对象。 (继承自 _DTE。)
公共属性 Mode 获取开发环境的模式(调试或设计)。 (继承自 _DTE。)
公共属性 Name 设置或获取 _DTE 对象的名称。 (继承自 _DTE。)
公共属性 ObjectExtenders 获取 ObjectExtenders 对象。 (继承自 _DTE。)
公共属性 Properties 返回表示“工具”菜单上“选项”对话框中包含的所有可用类别和子类别的 Properties 集合。 (继承自 _DTE。)
公共属性 RegistryRoot 获取包含 Visual Studio 注册表设置的根的路径的字符串。 (继承自 _DTE。)
公共属性 SelectedItems 获取一个包含当前在环境中选定的项的集合。 (继承自 _DTE。)
公共属性 Solution 获取 Solution 对象,该对象表示环境的当前实例中所有打开的项目并允许访问生成对象。 (继承自 _DTE。)
公共属性 SourceControl 获取一个允许操作对象后的文件源代码管理状态的 SourceControl 对象。 (继承自 _DTE。)
公共属性 StatusBar 获取表示主开发环境窗口上的状态栏的 StatusBar 对象。 (继承自 _DTE。)
公共属性 SuppressUI 获取或设置一个值,该值指示在执行自动化代码期间是否应显示用户界面。 (继承自 _DTE。)
公共属性 UndoContext 获取全局 UndoContext 对象。 (继承自 _DTE。)
公共属性 UserControl 设置或获取一个值,该值指示环境是由用户启动还是自动启动的。 (继承自 _DTE。)
公共属性 Version 获取宿主应用程序的版本号。 (继承自 _DTE。)
公共属性 WindowConfigurations 获取表示所有可用窗口配置的 WindowConfigurations 集合。 (继承自 _DTE。)
公共属性 Windows 获取 Windows 集合,其中包含在对象中显示的窗口。 (继承自 _DTE。)

了解了DTE,那我们开始要获取他了:
有多中种方法获取:
EnvDTE80.DTE2 _dte2 = (EnvDTE80.DTE2)ServiceProvider.GlobalProvider.GetService(typeof(EnvDTE.DTE));
[DllImport("ole32.dll", PreserveSig = false)]
        private static extern void CreateBindCtx(uint reserved, out IBindCtx ppbc);
        private static EnvDTE80.DTE2 dte;

        public static EnvDTE80.DTE2 Instance
        {
            get
            {
                if (dte != null) return dte;

                //return Marshal.BindToMoniker(
                var vsMoniker = @"!VisualStudio.DTE.10.0:{0}";
                IBindCtx ctx;
                IRunningObjectTable rot;
                IEnumMoniker enumMoniker;
                var moniker = new IMoniker[1];

                // ItemMoniker of the IDE, refer to http://msdn.microsoft.com/en-us/library/ms228755.aspx
                string ideMoniker = String.Format(vsMoniker, System.Diagnostics.Process.GetCurrentProcess().Id);
                CreateBindCtx(0, out ctx);
                ctx.GetRunningObjectTable(out rot);
                rot.EnumRunning(out enumMoniker);

                while (enumMoniker.Next(1, moniker, IntPtr.Zero) == 0)
                {
                    string displayName;
                    moniker[0].GetDisplayName(ctx, moniker[0], out displayName);
                    if (displayName == ideMoniker)
                    {
                        // --- Got the IDE Automation Object
                        Object oDTE;
                        rot.GetObject(moniker[0], out oDTE);
                        dte = oDTE as EnvDTE80.DTE2;
                        if (dte != null)
                            return dte;
                    }
                }

                return null;
            }
        }

都可以获取到DTE.
然后我们添加一个输出框:

 public enum AstIdeOutputPanes
    {
        GavinIde,
    }

    public static class OutputPanes
    {
        public static readonly OutputWindowPane GavinIde = OutputWindow.GetPane<GavinIdePaneDef>();

        [DisplayName(@"GavinIde")]
        [AutoActivate(true)]
        private class GavinIdePaneDef : OutputPaneDefinition
        {
        }

        public static void AppendLine(Exception ex)
        {
            Assert.NotNull(ex, "ex is null");

            var pane = GavinIde;
            pane.WriteLine(ex.Message);
            pane.WriteLine(ex.StackTrace);

            if (ex.InnerException != null)
                AppendLine(ex.InnerException);
        }

        public static void AppendLine(string message = "")
        {
            var pane = GavinIde;

            Assert.NotNull(pane, "pane is null");
            Assert.NotNull(message, "message is null");

            pane.WriteLine(message);
        }
    }


接着修改上次定义的Service,ShowMessage方法,改为输出框输出:

public bool ShowMessage(string message)
        {
            //MessageBox.Show(message);
            //MenuItemCallback(message);
            //CommonIdeTools.ShowErrorMessageBox(message);
            //CommonIdeTools.OpenFile(message);
            OutputPanes.AppendLine(message);
            return true;
        }

最后可以修改控制了:

ICalculationService calcService = Package.GetGlobalService(typeof(SCalculationService)) as ICalculationService;
            if (calcService != null)
            {
                var name = DTEHelper.Instance.Solution.FullName;
                var item = CommonIdeTools.GetSingleSelectedObject();
                calcService.ShowMessage(item.Name);
            }

/// <summary>
        /// SolutionExplorer里面选定的UIHierarchyItem
        /// </summary>
        public static IEnumerable<UIHierarchyItem> SelectedItemsInSolutionExplorer
        {
            get
            {
                return (DTEHelper.Instance.ToolWindows.SolutionExplorer.SelectedItems as Array)
                    .OfType<UIHierarchyItem>();
            }
        }

/// <summary>
        /// 得到当前单选的ProjectElement
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static UIHierarchyItem GetSingleSelectedObject()
        {
            var items = SelectedItemsInSolutionExplorer.ToList();
            var item = items[0];
            return item;
        }

最后显示如下:

原文地址:https://www.cnblogs.com/gavinhuang/p/3388177.html