将新项目添加到Visual Studio工具箱的特定选项卡 [英] Add new Item to the specific tab of Visual Studio toolbox

查看:124
本文介绍了将新项目添加到Visual Studio工具箱的特定选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新的Visual Studio外接程序,用于在Visual Studio工具箱中创建并添加自定义选项卡,并将新项(控件)添加到我的自定义选项卡.代码可用于将新选项卡添加到Visual Studio工具箱中,但不适用于将新项(控件)添加到我的选项卡中.
我的Visual Studio插件代码为:

 使用系统;
使用可扩展性;
使用 EnvDTE;
使用 EnvDTE80;
使用 EnvDTE90;
使用 EnvDTE100;
使用使用System.Windows.Forms;
命名空间 MyAddin1
{
    ///  <  > 用于实现外接程序的对象./summary  > 
 ///  <  另请参见    class ='IDTExtensibility2' / 公共  class 连接:IDTExtensibility2
    {
        ///  <  > 实现外接程序对象的构造函数.将初始化代码放在此方法中.<  /summary   > 
 公共 Connect()
        {
        }

        ///  <  > 实现IDTExtensibility2接口的OnConnection方法.接收到加载项正在加载的通知.<  /summary  ///  <  参数    term ='application' <  > 
 ///  <  参数    term ='connectMode' <  /param  > 
 ///  <  参数    term ='addInInst' <  /param  > 
 ///  <  另请参见    class ='IDTExtensibility2' / 公共  void  OnConnection(对象应用程序,ext_ConnectMode connectMode,对象 addInInst, ref 数组自定义)
        {
            _applicationObject =(DTE2)应用程序;
            _addInInstance =(AddIn)addInInst;
            // 将applicationObject成员变量传递给代码示例.
            ToolboxExample(_applicationObject);
        }

        公共 无效 ToolboxExample(DTE2 dte)
        {
            工具箱tlBox = ;
            ToolBoxTabs tbxTabs = ;
            ToolBoxTab3 tbxTab = ;
            尝试
            {
                tlBox =(ToolBox)(dte.Windows.Item(Constants.vsWindowKindToolbox). Object );
                tbxTabs = tlBox.ToolBoxTabs;
                tbxTab =(ToolBoxTab3)tbxTabs.Add(" );
                tbxTab.Activate();
                tbxTab.ToolBoxItems.Add("  @"  C:\ Windows \ Microsoft.NET \ assembly \ GAC_MSIL \ WindowsFormsControlLibrary2 \ v4.0_1.0.0.0__197889249da45bfc \ WindowsFormsControlLibrary2.dll",vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent) ;
            }
            捕获(例如System.Exception)
            {
                MessageBox.Show("  + ex.Message);
            }
        }

        私有 DTE2 _applicationObject;
        私有 AddIn _addInInstance;
    }
} 


以下代码行不起作用:

 tbxTab.ToolBoxItems.Add("  @" ,vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent); 


我改变了

 tbxTab.ToolBoxItems.Add 


与:

 tbxTabs.Item(" ).ToolBoxItems.Add 


但是,它对我没有用.甚至我都改变了

  @"  


使用以下代码行并对其进行一个接一个地测试:

  @"  



 "  


但是,它再次对我不起作用.
我的自定义控件主类名称为 FloorsGrouping ,其显示名称为:

 [DisplayName(" )] 


其在GAC中的程序集名称为:

 [Editor("  使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用使用System.Runtime.InteropServices;
使用 EnvDTE;
使用 EnvDTE80;
使用 System.IO;

命名空间 InstallToolboxControls
{
    // 我们需要实现的IMessageFilter接口的定义和
    // 使用CoRegisterMessageFilter API注册.
    [ComImport(),Guid(" ),InterfaceTypeAttribute( ComInterfaceType.InterfaceIsIUnknown)]
    界面 IOleMessageFilter // 重命名以避免系统混乱. Windows.Forms.IMessageFilter 
    {
        [PreserveSig]
         int  HandleInComingCall( int  dwCallType, IntPtr  hTaskCaller , int  dwTickCount, IntPtr  lpInterfaceInfo);

        [PreserveSig]
         int  RetryRejectedCall( IntPtr  hTaskCallee, int  dwTickCount , int  dwRejectType);

        [PreserveSig]
         int  MessagePending( IntPtr  hTaskCallee, int  dwTickCount , int  dwPendingType);
    }

     class 程序:IOleMessageFilter
    {
        [DllImport(" )]
        私有 静态 外部  out  IOleMessageFilter oldFilter);
        静态 字符串 ctrlPath = "  WindowsFormsControlLibrary2.FloorsGrouping,WindowsFormsControlLibrary2,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = 197889249da45bfc";  @"C:\ Windows \ Microsoft.NET \ assembly \ GAC_MSIL \ WindowsFormsControlLibrary2 \ v4.0_1.0.0.0__197889249da45bfc \ WindowsFormsControlLibrary2.dll";//@"E:\ Rostami \ Saino \ Program \ Tests \ WindowsFormsControlLibrary2 \ WindowsFormsControlLibrary2 \ bin \ Debug \ WindowsFormsControlLibrary2.dll; 

        [STAThread]
        公共 静态  void 工具箱(字符串 arg)
        {
            程序程序=  Program();
            程序.Register();
            如果(arg.Equals(" ,StringComparison.CurrentCultureIgnoreCase))
            {
                程序.InstallControl();
            }
            其他 如果(arg.Equals(" 卸载",StringComparison.CurrentCultureIgnoreCase))
            {
                程序.UninstallControl();
            }
            program.Revoke();

            // 以确保dte对象实际上已释放,并且devenv.exe进程终止.
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

        无效 InstallControl()
        {
            // 创建VS IDE的实例,
            类型type = System.Type.GetTypeFromProgID(" );
            DTE dte =(DTE)System.Activator.CreateInstance(type, true );

            // 创建一个临时的winform项目; 
            字符串 tmpFile = Path.GetFileNameWithoutExtension(Path.GetTempFileName());
            字符串 tmpDir = 字符串 .Format("  {0} {1}",Path.GetTempPath(),tmpFile);
            Solution2解决方案= dte.Solution  as 
            字符串 templatePath = solution.GetProjectTemplate(" " );
            项目proj = solution.AddFromTemplate(templatePath,tmpDir," // 将控件添加到工具箱.
            EnvDTE.Window window = dte.Windows.Item(EnvDTE.Constants.vsWindowKindToolbox);
            EnvDTE.ToolBox工具箱=(EnvDTE.ToolBox)窗口.对象;
            EnvDTE.ToolBoxTab myTab = toolbox.ToolBoxTabs.Add(" );
            myTab.Activate();
            myTab.ToolBoxItems.Add(" ,ctrlPath,vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent);
            dte.Solution.Close( false );
            元帅.ReleaseComObject(dte);
            //  Console.WriteLine("Control Installed !!!"); 
        }

        无效 UninstallControl()
        {
            类型type = System.Type.GetTypeFromProgID(" );
            DTE dte =(DTE)System.Activator.CreateInstance(type, true );
            EnvDTE.Window window = dte.Windows.Item(EnvDTE.Constants.vsWindowKindToolbox);
            EnvDTE.ToolBox工具箱=(EnvDTE.ToolBox)窗口.对象;
            EnvDTE.ToolBoxTab myTab = toolbox.ToolBoxTabs.Item(" );
            myTab.Activate();
            myTab.Delete();
            元帅.ReleaseComObject(dte);
            //  Console.WriteLine("Control Uninstalled !!!"); 
        }

        无效 Register()
        {
            IOleMessageFilter oldFilter;
            CoRegisterMessageFilter( this  out  oldFilter);
        }

        无效 Revoke()
        {
            IOleMessageFilter oldFilter;
            CoRegisterMessageFilter( out  oldFilter);
        }

         #region IOleMessageFilter成员
 公共  int  HandleInComingCall( int  dwCallType, IntPtr  hTaskCaller, int  dwTickCount, IntPtr  lpInterfaceInfo)
        {
            返回  0 ; //  SERVERCALL_ISHANDLED 
        }

        公共  int  RetryRejectedCall( IntPtr  hTaskCallee, int  dwTickCount, int  dwRejectType)
        {
            如果(dwRejectType ==  2 )//  SERVERCALL_RETRYLATER 
            {
                返回  200 ; // 等待2秒钟,然后重试
            }
            返回 -1; // 取消通话
        }

        公共  int  MessagePending( IntPtr  hTaskCallee, int  dwTickCount, int  dwPendingType)
        {
            返回  2 ; //  PENDINGMSG_WAITDEFPROCESS 
        }
         #endregion
}
} 


我的代码如下所示,用于Form1.cs:

 使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用使用System.Drawing;
使用 System.Linq;
使用 System.Text;
使用使用System.Windows.Forms;
使用 InstallToolboxControls;

命名空间 WindowsFormsApplication12
{
    公共 部分  class  Form1:表单
    {
        公共 Form1()
        {
            InitializeComponent();
        }

        私有 无效 button1_Click(对象发​​件人,EventArgs e)
        {
            InstallToolboxControls.Program prg =  InstallToolboxControls.Program();
            InstallToolboxControls.Program.Toolbox(" );
        }
    }
} 


I have created a new Visual Studio Addin for create and add my custom tab in the Visual Studio toolbox and add new items (controls) to my custom tab. Codes work for add new tab to the Visual Studio toolbox, but not work for add new items (controls) to my tab.
My Visual Studio Addin code is:

using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using EnvDTE90;
using EnvDTE100;
using System.Windows.Forms;
namespace MyAddin1
{
    /// <summary>The object for implementing an Add-in.</summary>
    /// <seealso class='IDTExtensibility2' />
    public class Connect : IDTExtensibility2
    {
        /// <summary>Implements the constructor for the Add-in object. Place your initialization code within this method.</summary>
        public Connect()
        {
        }

        /// <summary>Implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded.</summary>
        /// <param term='application'>Root object of the host application.</param>
        /// <param term='connectMode'>Describes how the Add-in is being loaded.</param>
        /// <param term='addInInst'>Object representing this Add-in.</param>
        /// <seealso class='IDTExtensibility2' />
        public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
        {
            _applicationObject = (DTE2)application;
            _addInInstance = (AddIn)addInInst;
            // Pass the applicationObject member variable to the code example.
            ToolboxExample(_applicationObject);
        }

        public void ToolboxExample(DTE2 dte)
        {
            ToolBox tlBox = null;
            ToolBoxTabs tbxTabs = null;
            ToolBoxTab3 tbxTab = null;
            try
            {
                tlBox = (ToolBox)(dte.Windows.Item(Constants.vsWindowKindToolbox).Object);
                tbxTabs = tlBox.ToolBoxTabs;
                tbxTab = (ToolBoxTab3)tbxTabs.Add("MRS");
                tbxTab.Activate();
                tbxTab.ToolBoxItems.Add("FloorsGrouping", @"C:\Windows\Microsoft.NET\assembly\GAC_MSIL\WindowsFormsControlLibrary2\v4.0_1.0.0.0__197889249da45bfc\WindowsFormsControlLibrary2.dll", vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("ERROR: " + ex.Message);
            }
        }

        private DTE2 _applicationObject;
        private AddIn _addInInstance;
    }
}


Following line of code not works:

tbxTab.ToolBoxItems.Add("FloorsGrouping", @"C:\Windows\Microsoft.NET\assembly\GAC_MSIL\WindowsFormsControlLibrary2\v4.0_1.0.0.0__197889249da45bfc\WindowsFormsControlLibrary2.dll", vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent);


I change

tbxTab.ToolBoxItems.Add


with:

tbxTabs.Item("MRS").ToolBoxItems.Add


However, it didn''t work for me. Even I change

@"C:\Windows\Microsoft.NET\assembly\GAC_MSIL\WindowsFormsControlLibrary2\v4.0_1.0.0.0__197889249da45bfc\WindowsFormsControlLibrary2.dll"


with following code lines and test them one by one:

@"E:\Rostami\Saino\WindowsFormsControlLibrary2.dll"


and

"WindowsFormsControlLibrary2.FloorsGrouping, WindowsFormsControlLibrary2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=197889249da45bfc"


However, it didn''t work for me again.
My custom control main class name is FloorsGrouping, and its display name is:

[DisplayName("Floors Group")]


And its assembly name in the GAC is:

[Editor("WindowsFormsControlLibrary2.FloorsGrouping, WindowsFormsControlLibrary2, Version=1.0.0.0, Culture=neutral,  PublicKeyToken=197889249da45bfc", typeof(UITypeEditor))]


I searched the internet for any solutions, and I only find several solutions that describe adding new tab to the Visual Studio toolbox and adding controls to tab just possible from Visual Studio Addin.

解决方案

I found the solution and could add my custom tab to the Visual Studio toolbox and add my custom controls to my custom tab with Windows Forms Application project and not from Visual Studio Add-in project. I describe it as follows:
In Windows Forms Application project, first, we must create an instance of Visual Studio IDE. Then, must create a temporary Windows Forms Application project and next, must add our custom tab to the Visual Studio toolbox and add our custom controls to our custom tab.
My codes are as following for Class1.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using EnvDTE;
using EnvDTE80;
using System.IO;

namespace InstallToolboxControls
{
    // Definition of the IMessageFilter interface which we need to implement and 
    // register with the CoRegisterMessageFilter API.
    [ComImport(), Guid("00000016-0000-0000-C000-000000000046"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
    interface IOleMessageFilter // Renamed to avoid confusion w/ System.Windows.Forms.IMessageFilter
    {
        [PreserveSig]
        int HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo);

        [PreserveSig]
        int RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType);

        [PreserveSig]
        int MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType);
    }

    class Program : IOleMessageFilter
    {
        [DllImport("ole32.dll")]
        private static extern int CoRegisterMessageFilter(IOleMessageFilter newFilter, out IOleMessageFilter oldFilter);
        static string ctrlPath = "WindowsFormsControlLibrary2.FloorsGrouping, WindowsFormsControlLibrary2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=197889249da45bfc";//@"C:\Windows\Microsoft.NET\assembly\GAC_MSIL\WindowsFormsControlLibrary2\v4.0_1.0.0.0__197889249da45bfc\WindowsFormsControlLibrary2.dll";//@"E:\Rostami\Saino\Program\Tests\WindowsFormsControlLibrary2\WindowsFormsControlLibrary2\bin\Debug\WindowsFormsControlLibrary2.dll";

        [STAThread]
        public static void Toolbox(string arg)
        {
            Program program = new Program();
            program.Register();
            if (arg.Equals("Install", StringComparison.CurrentCultureIgnoreCase))
            {
                program.InstallControl();
            }
            else if (arg.Equals("UnInstall", StringComparison.CurrentCultureIgnoreCase))
            {
                program.UninstallControl();
            }
            program.Revoke();

            // to ensure the dte object is actually released, and the devenv.exe process terminates.
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

        void InstallControl()
        {
            // Create an instance of the VS IDE,
            Type type = System.Type.GetTypeFromProgID("VisualStudio.DTE.10.0");
            DTE dte = (DTE)System.Activator.CreateInstance(type, true);

            // create a temporary winform project;
            string tmpFile = Path.GetFileNameWithoutExtension(Path.GetTempFileName());
            string tmpDir = string.Format("{0}{1}", Path.GetTempPath(), tmpFile);
            Solution2 solution = dte.Solution as Solution2;
            string templatePath = solution.GetProjectTemplate("WindowsApplication.zip", "CSharp");
            Project proj = solution.AddFromTemplate(templatePath, tmpDir, "dummyproj", false);

            // add the control to the toolbox.
            EnvDTE.Window window = dte.Windows.Item(EnvDTE.Constants.vsWindowKindToolbox);
            EnvDTE.ToolBox toolbox = (EnvDTE.ToolBox)window.Object;
            EnvDTE.ToolBoxTab myTab = toolbox.ToolBoxTabs.Add("Saino");
            myTab.Activate();
            myTab.ToolBoxItems.Add("MyUserControl", ctrlPath, vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent);
            dte.Solution.Close(false);
            Marshal.ReleaseComObject(dte);
            //Console.WriteLine("Control Installed!!!");
        }

        void UninstallControl()
        {
            Type type = System.Type.GetTypeFromProgID("VisualStudio.DTE.10.0");
            DTE dte = (DTE)System.Activator.CreateInstance(type, true);
            EnvDTE.Window window = dte.Windows.Item(EnvDTE.Constants.vsWindowKindToolbox);
            EnvDTE.ToolBox toolbox = (EnvDTE.ToolBox)window.Object;
            EnvDTE.ToolBoxTab myTab = toolbox.ToolBoxTabs.Item("Saino");
            myTab.Activate();
            myTab.Delete();
            Marshal.ReleaseComObject(dte);
            //Console.WriteLine("Control Uninstalled!!!");
        }

        void Register()
        {
            IOleMessageFilter oldFilter;
            CoRegisterMessageFilter(this, out oldFilter);
        }

        void Revoke()
        {
            IOleMessageFilter oldFilter;
            CoRegisterMessageFilter(null, out oldFilter);
        }

        #region IOleMessageFilter Members
        public int HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo)
        {
            return 0; //SERVERCALL_ISHANDLED
        }

        public int RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType)
        {
            if (dwRejectType == 2) // SERVERCALL_RETRYLATER
            {
                return 200; // wait 2 seconds and try again
            }
            return -1; // cancel call
        }

        public int MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType)
        {
            return 2; //PENDINGMSG_WAITDEFPROCESS
        }
        #endregion
    }
}


And my codes are as following for Form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InstallToolboxControls;

namespace WindowsFormsApplication12
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            InstallToolboxControls.Program prg = new InstallToolboxControls.Program();
            InstallToolboxControls.Program.Toolbox("Install");
        }
    }
}


这篇关于将新项目添加到Visual Studio工具箱的特定选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆