共用方式為


進階的工具箱控制項開發

注意事項注意事項

若要將自訂控制項加入至 [工具箱] 中的建議的方式是使用 Visual Studio 的 10 SDK 隨附的工具箱控制項樣板。本主題被保留用於回溯相容性、 將現有的控制項加入至 [工具箱] 中,以及進階的工具箱控制項開發。

如需有關如何使用範本建立工具箱控制項的詳細資訊,請參閱How to: 建立使用 Windows Form 的工具箱控制項How to: 建立使用 WPF 的工具箱控制項

可擴充的管理套件架構為基礎的 VSPackage Visual Studio工具箱加入控制項的功能,物件衍生自ToolboxItem物件。 每個ToolboxItem由衍生自物件實作Component

工具箱項目提供者的 VSPackage

管理套件架構為基礎的 VSPackage 必須將自己登錄為工具箱控制提供者,透過.NET Framework屬性,並處理工具箱相關的事件。

若要將 VSPackage 設定為工具箱項目提供者

  1. 建立執行個體的ProvideToolboxItemsAttribute套用至類別實作Package。 例如:

    Namespace Vsip.LoadToolboxMembers 
        <ProvideToolboxItems(14)> _ 
        <DefaultRegistryRoot("Software\Microsoft\VisualStudio\8.0")> _ 
        <InstalledProductRegistration(False, "#100", "#102", "1.0", IconResourceID := 400)> _ 
        <ProvideLoadKey("Standard", "1.0", "Package Name", "Company", 1)> _ 
        <ProvideMenuResource(1000, 1)> _ 
        <Guid("YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYYY")> _ 
        Public Class LoadToolboxMembers 
            Inherits Package 
        End Class 
    End Namespace
    
    namespace Vsip.LoadToolboxMembers {
        [ProvideToolboxItems(14)]
        [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\8.0")]
        [InstalledProductRegistration(false, "#100", "#102", "1.0", IconResourceID = 400)]
        [ProvideLoadKey("Standard", "1.0", "Package Name", "Company", 1)]
        [ProvideMenuResource(1000, 1)]
        [Guid("YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYYY")]
        public class LoadToolboxMembers : Package {
    
    注意事項注意事項

    建構函式ProvideToolboxItemsAttribute會做為引數的整數版本號碼。Visual Studio環境使用這個版本號碼,來判斷 VSPackage,提供ToolboxItem物件就必須重新載入,或如果快取的資訊可供 [工具箱] 中。若要保證提供時,重新載入的 VSPackage ToolboxItem ,仍在開發、 恆增加任何修改之後的這個版本號碼。

  2. 如果ToolboxItem物件提供非標準的工具箱剪貼簿格式,執行個體的ProvideToolboxFormatAttribute必須套用至類別實作Package物件支援的每一種剪貼簿格式的ToolboxItem VSPackage 所提供的物件。

    如需有關支援的工具箱剪貼簿格式的詳細資訊,請參閱工具箱 (Visual Studio SDK)

    注意事項注意事項

    如果 VSPackage 指出它會提供任何ToolboxItem具有非標準的剪貼簿格式,物件Visual Studio環境假設只有所指示的格式ProvideToolboxFormatAttribute執行個體套用至 VSPackage Package類別實作所支援的 VSPackage。如果 VSPackage,就必須支援預設的剪貼簿格式,以及非標準的格式,它必須套用的執行個體ProvideToolboxFormatAttribute的每個預設的格式,以及非標準的格式。

  3. 如果 VSPackage 提供的動態組態ToolboxItem,則它必須:

    1. 套用的執行個體ProvideToolboxItemConfigurationAttribute使用建構Type封裝用來實作IConfigureToolboxItem介面。

    2. 在public類別不受影響的 VSPackage Package,VSPackage 必須實作IConfigureToolboxItem介面。

      執行個體的ProvideAssemblyFilterAttribute必須套用至類別實作IConfigureToolboxItem,使用字串,包含做為引數的選取條件 (篩選) ProvideToolboxItemConfigurationAttribute執行個體的建構函式。

如需如何告知Visual Studio VSPackage 提供工具箱的環境會控制,請參閱正在註冊工具箱支援功能

如需範例,說明如何實作其中一個IConfigureToolboxItem支援,請參閱逐步解說: 自訂動態 ToolboxItem 組態

  1. 提供 VSPackages ToolboxItem必須處理ToolboxInitializedToolboxUpgraded事件。

    1. 實作處理常式的ToolboxInitializedToolboxUpgraded事件:

      Private Sub OnToolboxUpgraded(ByVal sender As Object, ByVal e As EventArgs) 
          OnToolboxInitialized(send, e) 
      End Sub 
      Private Sub OnToolboxInitialized(ByVal sender As Object, ByVal e As EventArgs) 
          'Make sure all toolbox items are added. 
      End Sub
      
      private void OnToolboxUpgraded(object sender, EventArgs e) {
          OnToolboxInitialized(send,e);
      }
      private void OnToolboxInitialized(object sender, EventArgs e) {
          //Make sure all toolbox items are added.
      }
      
    2. 訂閱ToolboxInitializedToolboxUpgraded事件。

      尤其是Package實作的Initialize方法:

      Protected Overloads Overrides Sub Initialize() 
          AddHandler ToolboxInitialized, AddressOf OnToolboxInitialized 
          AddHandler ToolboxUpgraded, AddressOf OnToolboxUpgraded 
      End Sub
      
      protected override void Initialize() {
          ToolboxInitialized += new EventHandler(OnToolboxInitialized);
          ToolboxUpgraded += new EventHandler(OnToolboxUpgraded);
      }
      

    如需如何實作處理常式的範例ToolboxInitializedToolboxUpgraded事件,請參閱逐步解說: 自動載入工具箱項目

建立工具箱控制項

工具箱控制項的基礎實作必須衍生自Component ,封裝預設值,或是衍生程度的實作在ToolboxItem物件。

最簡單的方法,以提供Component-工具箱控制項衍生程度的實作是由擴充衍生自物件Control,特別是, UserControl類別。

若要建立工具箱控制項

  1. 使用方案總管] 中加入新項目命令,以建立工具箱物件實作UserControl

    Public Partial Class ToolboxControl1 
        Inherits UserControl 
        Public Sub New() 
            InitializeComponent() 
        End Sub 
        Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) 
            MsgBox("Hello world from" & Me.ToString()) 
        End Sub 
    
        Private Sub ToolboxItem1_Load(ByVal sender As Object, ByVal e As EventArgs) 
    
        End Sub 
    End Class
    
    public partial class ToolboxControl1 : UserControl {
            public ToolboxControl1() {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e) {
                MessageBox.Show("Hello world from" + this.ToString());
            }
    
            private void ToolboxItem1_Load(object sender, EventArgs e) {
    
            }
        }
    

    如需有關如何撰寫 Windows Form 控制項與工具箱控制項的詳細資訊,請參閱使用 .NET Framework 開發自訂的 Windows Form 控制項逐步解說: 自動載入工具箱項目

  2. (選擇性)應用程式可以選擇使用自訂物件衍生自ToolboxItem物件,以提供其工具箱控制項工具箱

    注意事項注意事項

    任何類別衍生自ToolboxItem物件必須要有執行個體的SerializableAttribute套用到它。

    自訂的實作衍生自ToolboxItem可以擴充應用程式提供更多的控制方式ToolboxItem資料序列化、 增強的設計工具的中繼資料的處理,支援非標準的剪貼簿格式,並可讓一般使用者的互動功能。

    在範例中,對話方塊會提示使用者選取的功能:

    <ToolboxItemAttribute(GetType(CustomControl))> _ 
    <Serializable()> _ 
    Class CustomControl 
        Inherits ToolboxItem 
    
        Public Sub New(ByVal type As Type) 
            MyBase.New(GetType(CustomControl)) 
        End Sub 
        Public Sub New(ByVal type As Type, ByVal icon As Bitmap) 
            MyBase.New(GetType(SCustomControl)) 
            Me.DisplayName = "CustomContorl" 
            Me.Bitmap = icon 
        End Sub 
    
        Private Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext) 
            Deserialize(info, context) 
        End Sub 
        Protected Overloads Overrides Function CreateComponentsCore(ByVal host As IDesignerHost) As IComponent() 
            Dim dialog As New CustomControlDialog(host) 
            Dim dialogResult__1 As DialogResult = dialog.ShowDialog() 
            If dialogResult__1 = DialogResult.OK Then 
                Dim component As IComponent = DirectCast(dialog.CustomInstance, IComponent) 
                Dim container As IContainer = host.Container 
                container.Add(component) 
                Return New IComponent() {component} 
            Else 
                Return New IComponent() {} 
            End If 
        End Function 
    End Class
    
    [ToolboxItemAttribute(typeof(CustomControl))]
    [Serializable]
    class CustomControl : ToolboxItem {
    
        public CustomControl(Type type) : base(typeof(CustomControl)) {}
            public CustomControl(Type type, Bitmap icon) : base(typeof(SCustomControl)) {
            this.DisplayName = "CustomContorl";
            this.Bitmap = icon;
        }
    
        private CustomControl(SerializationInfo info, StreamingContext context) {
            Deserialize(info, context);
        }
        protected override IComponent[] CreateComponentsCore(IDesignerHost host) {
            CustomControlDialog dialog = new CustomControlDialog(host);
            DialogResult dialogResult = dialog.ShowDialog();
            if (dialogResult == DialogResult.OK) {
                IComponent component = (IComponent)dialog.CustomInstance;
                IContainer container = host.Container;
                container.Add(component);
                return new IComponent[] { component };
            }
            else {
                return new IComponent[] {};
            }
        }
    }
    
注意事項注意事項

您也可為一個類別衍生自ToolboxItem物件,以提供自己的獨立的實作的基礎控制項。該類別是負責建立和提供所有的基礎元件。

明確的額外的工具箱項目

要加入至 [工具箱] 中,控制項必須位於執行個體的ToolboxItem或衍生自物件的ToolboxItem ,然後加入工具箱使用IToolboxService介面。

若要封裝和加入工具箱控制項

  1. 封裝Component的執行個體中的實作ToolboxItem物件或ToolboxItem-藉由呼叫該物件的衍生物件Initialize與實作元件的方法Type

    Dim customItem As New ToolboxItem() 
    If customItem IsNot Nothing Then 
        customItem.Initialize(userControl) 
    End If
    
    ToolboxItem customItem = new ToolboxItem() ;
    if (customItem != null) {
        customItem.Initialize(userControl);
    }
    

    以上所述是物件的範例userControl衍生自UserControl (執行個體的ToolboxControl1如上所示的物件) 用來建構新的ToolboxItem

    注意事項注意事項

    預設實作ToolboxItem建構函式製作Type引數 (#ctor(Type)建構函式呼叫ToolboxItem物件的Initialize方法。

  2. 使用工具箱服務 (IToolboxService) 來新增ToolboxItem從基礎的控制項實作建構的物件。

    在下面範例中,存取工具箱服務取得時,部分的屬性ToolboxItem執行個體customItem已設定,然後再customItem加入至工具箱

    Dim toolboxService As IToolboxService = TryCast(GetService(GetType(IToolboxService)), IToolboxService)
    customItem.Bitmap = New System.Drawing.Bitmap(ToolBoxControl1, "Control1.bmp")
    customItem.DisplayName = "Custom Item" 
    toolboxService.AddToolboxItem(item, "Custom Tab")
    
    IToolboxService toolboxService = GetService(typeof(IToolboxService)) as IToolboxService;
    customItem.Bitmap = new System.Drawing.Bitmap(ToolboxControl1,"Control1.bmp");
    customItem.DisplayName= "Custom Item";
    toolboxService.AddToolboxItem(item, "Custom Tab");
    

使用反映來加入工具箱控制項

將屬性套用至類別實作工具箱控制項可讓Visual Studio環境或Visual Studio SDK基礎的應用程式使用反映來自動偵測並適當地加入控制項以工具箱

若要將反映和屬性套用至工具箱控制項

  1. 識別用來實作的執行個體的 [工具箱] 控制項的所有物件ToolboxItemAttribute

    執行個體的型別ToolboxItemAttribute物件將會決定如何ToolboxItem由其建構。

    1. 套用的執行個體ToolboxItemAttribute所建構BOOLEAN的值false物件對該物件無法使用 [工具箱] 中經由反映。

      這可以用來找出一個物件,例如UserControl工具箱在開發期間。

    2. 套用的執行個體ToolboxItemAttribute所建構BOOLEAN的值true物件對該物件可以使用 [工具箱] 中經由反映,而且需要物件會加入 [工具箱] 中使用預設ToolboxItem物件。

    3. 套用的執行個體ToolboxItemAttribute所建構Type衍生自自訂物件的ToolboxItem讓物件可以使用工具箱透過反映,而且需要物件會加入 [工具箱] 中使用衍生自這個自訂物件ToolboxItem

  2. 指定 (以Visual Studio環境的反映機制) 要用來顯示 [工具箱] 中的控制項的點陣圖工具箱加上的執行個體ToolboxBitmapAttribute工具箱控制項實作。

  3. 如有必要,將套用的例項ToolboxItemFilterAttributeToolboxItem使用反映來以靜態方式將它們標示用於具有相符的屬性之物件的物件。

    在下面範例中,工具箱控制項的實作會有執行個體的ProvideAssemblyFilterAttribute套用,這會讓該控制項用於工具箱只有在目前的工作文件時UserControl設計工具

    <ToolboxItemFilter(System.Windows.Forms.UserControl, ToolboxItemFilterType.Require)> _ 
    <SerializableAttribute()> _ 
    <GuidAttribute("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")> _ 
    Friend Class CustomToolboxItem 
        Inherits ToolboxItem 
    End Class
    
    [ToolboxItemFilter(System.Windows.Forms.UserControl,ToolboxItemFilterType.Require)]
    [SerializableAttribute()]  //ToolboxItem implementations much has this attribute.
    [GuidAttribute("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")]
    internal class CustomToolboxItem : ToolboxItem 
    

有三種基本的技巧,可使用反映來自動載入ToolboxItem

使用 「 ToolService 」 功能,以擷取工具箱控制項

ToolboxService VSPackages 提供靜態GetToolboxItems方法,使用反映來掃瞄支援工具箱項目,所有類型的組件,並傳回這些類型的項目。 要傳回的工具箱項目必須:

若要取得這份清單

  1. 建立執行個體的Assembly參考的組件,是要加以掃描以便查看ToolboxItem物件。

    注意事項注意事項

    若要取得的執行個體Assembly目前的組件,請使用靜態方法GetExecutingAssembly

  2. 呼叫GetToolboxItems、 傳回ICollection物件,包含適當的物件清單。

    注意事項注意事項

    如果物件在傳回ICollection有有效的執行個體的ToolboxBitmapAttribute指派給它的實作, GetToolboxItems方法會設定ToolboxItem物件的Bitmap屬性。

  3. 使用GetService以取得存取權IToolboxService,並使用其AddToolboxItem方法,將項目傳回ICollection [工具箱] 中的物件。

    下列程式碼會查詢執行的應用程式,並會取得一份所有其ToolboxItem物件,並將它們載入。 如需說明這在執行程式碼範例,請參閱Initialization中的方法逐步解說: 自訂動態 ToolboxItem 組態

    Protected ToolboxItemList As ICollection = Nothing
    ToolboxItemList = ToolboxService.GetToolboxItems(Assembly.GetExecutingAssembly(), "")
    If ToolboxItemList Is Nothing Then 
        Throw New ApplicationException("Unable to generate a toolbox Items listing for " & [GetType]().FullName) 
    End If 
    Dim toolboxService As IToolboxService = TryCast(GetService(GetType(IToolboxService)), IToolboxService) 
    For Each itemFromList As ToolboxItem In ToolboxItemList 
        toolboxService.AddToolboxItem(itemFromList, CategoryTab) 
    Next
    
    protected ICollection ToolboxItemList = null;
    ToolboxItemList = ToolboxService.GetToolboxItems(Assembly.GetExecutingAssembly(), "");
    if (ToolboxItemList == null){
        throw new ApplicationException("Unable to generate a toolbox Items listing for "
    + GetType().FullName);
    }
    IToolboxService toolboxService = GetService(typeof(IToolboxService)) as IToolboxService;
    foreach (ToolboxItem itemFromList in ToolboxItemList){
        toolboxService.AddToolboxItem(itemFromList, CategoryTab);
    }
    

使用內嵌的文字資源,以自動載入套件工具箱控制項

包含格式正確的清單,工具箱控制項的組件中的文字資源可供ParseToolboxResource能自動載入工具箱控制項,如果適當地格式化。

文字資源,其中包含要載入的物件清單就會 VSPackage 可以存取組件中。

若要新增,可以使用文字資源組件

  1. 方案總管] 中,以滑鼠右鍵按一下專案。

  2. 指向新增,然後按一下 [ 新的項目

  3. 加入新項目 對話方塊中,選取 的文字檔 ,並提供一個名稱。

  4. 方案總管] 中,以滑鼠右鍵按一下新建立的文字檔,並設定 建置動作屬性為 [內嵌資源。

    項目工具箱要載入的控制項都必須包含實作的類別,包含它的組件名稱的名稱。

    如 [工具箱] 的格式資訊會控制內嵌的文字資源項目,請參閱ParseToolboxResource參考網頁。

  5. 設定搜尋路徑包含裝載工具箱控制項物件的組件的檔案。

    ParseToolboxResource用來代表登錄項目 HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\ 中所指定的目錄中只搜尋<version>\AssemblyFolders,其中 <version> 是 Visual Studio (例如,8.0) 發行的版本號碼。

    注意事項注意事項

    根路徑的 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\<Version> 與備用的根,當初始化 Visual Studio 殼層時或使用您可以覆寫DefaultRegistryRootAttribute。如需詳細資訊,請參閱 命令列參數 (Visual Studio SDK)

    如需 AssemblyFolder 登錄項目正確格式的詳細資訊,請參閱ParseToolboxResource參考網頁。

  6. 取得執行個體的Synchronized存取內嵌的資源,而且如果當地語系化支援所需的類別名稱,執行個體的ResourceManager,並使用這些來叫用ParseToolboxResource方法。

    Dim rm As New ResourceManager("TbxCategories", Assembly.GetExecutingAssembly())
    Dim toolboxStream As Stream = TbxItemProvider.[GetType]().Assembly.GetManifestResourceStream("ToolboxItems.txt")
    If toolboxStream IsNot Nothing Then 
        Using reader As TextReader = New StreamReader(toolboxStream) 
            ParseToolboxResource(reader, rm) 
        End Using 
    End If
    
    ResourceManager rm = new ResourceManager("TbxCategories", Assembly.GetExecutingAssembly());
    Stream toolboxStream = TbxItemProvider.GetType().Assembly.GetManifestResourceStream("ToolboxItems.txt");
    if (toolboxStream != null) {
        using (TextReader reader = new StreamReader(toolboxStream)) {
        ParseToolboxResource(reader, rm);
    }
    }
    

    上述清單中包含之類別的組件內嵌的資源中所包含的範例中TbxItemProvider傳遞至ParseToolboxResource與TbxCategories的字串資源。

    這個方法會搜尋所有列在資源並載入這些值包含在 [工具箱] 控制項的 AssemblyFolders 登錄項目] 下所指定的目錄中的組件的檔案。

    注意事項注意事項

    如果工具箱控制項找到ParseToolboxResource有有效的執行個體的ToolboxBitmapAttribute指派給它的實作, ParseToolboxResource將會設定用來顯示 [工具箱] 控制項的點陣圖。

明確地使用反映來自動載入套件工具箱控制項

如果要明確的相關資訊的查詢組件,它會工具箱及其包含的控制項,而不是委派任務給GetToolboxItems,其做法。

若要明確地使用反映來自動載入套件工具箱控制項

  1. 建立執行個體的Assembly,是要加以掃描以便查看每個組件參考ToolboxItem物件。

    注意事項注意事項

    若要取得的執行個體Assembly目前的組件,請使用靜態方法GetExecutingAssembly

  2. 每個組件進行掃描,請使用Assembly物件的GetTypes方法,以取得每一份Type組件中。

  3. 請確認型別不是抽象的且支援IComponent介面 (用來執行個體化的 [工具箱] 控制項的所有實作ToolboxItem物件必須實作這個介面)。

  4. 取得屬性的Type ,並使用這項資訊來判斷 VSPackage 希望載入物件。

    注意事項注意事項

    雖然在主體中都可建立ToolboxItem獲取IComponent介面實作的執行個體沒有ToolboxItemAttribute未設定為false套用到它時,我們不建議這樣做。

  5. 使用GetConstructor以取得建構函式的ToolboxItem工具箱控制項需要的物件。

  6. 建構ToolboxItem物件,並將它們新增至工具箱

若要查看說明明確使用反映來取得,並自動載入套件 [工具箱] 控制項的範例,請參閱CreateItemList所述逐步解說: 自動載入工具箱項目

額外的工具箱控制項設定

VSPackage 可以運用的進一步控管何時以及如何顯示工具箱控制項工具箱,透過實作IConfigureToolboxItem,並改用ProvideAssemblyFilterAttribute,以及ProvideToolboxItemConfigurationAttribute

套用ToolboxItemFilterAttribute類別的執行個體提供只有靜態控制項何時和如何工具箱控制項才可以使用。

若要建立工具箱控制項的動態組態支援

  1. 建構類別實作IConfigureToolboxItem VSPackage 的一部分的介面。

    注意事項注意事項

    IConfigureToolboxItem介面不會實作相同的類別會提供 VSPackage 的實作上Package

  2. 將相關聯的實作IConfigureToolboxItem藉由套用的執行個體的特定組件中的物件與ProvideAssemblyFilterAttribute給它。

    下面這個範例會提供動態組態中的工具箱控制項物件組件的Vsip.*命名空間和需要某些ToolboxItem物件是可見只用UserControl-架構設計人員和其他永遠不會顯示與UserControl-架構設計人員。

    <ProvideAssemblyFilterAttribute("Vsip.*, Version=*, Culture=*, PublicKeyToken=*")> _ 
    <GuidAttribute("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")> _ 
    Public NotInheritable Class ToolboxConfig 
        Implements IConfigureToolboxItem 
        Public Sub New() 
        End Sub 
    
    
        ''' <summary> 
        ''' Adds extra configuration information to this toolbox item. 
        ''' </summary> 
        Public Sub ConfigureToolboxItem(ByVal item As ToolboxItem) 
            If item Is Nothing Then 
                Exit Sub 
            End If 
    
            'hide from .NET Compact Framework on the device designer. 
            Dim newFilter As ToolboxItemFilterAttribute = Nothing 
            If item.TypeName = GetType(ToolboxControl1).ToString() Then 
                newFilter = New ToolboxItemFilterAttribute("System.Windows.Forms.UserControl", ToolboxItemFilterType.Require) 
            ElseIf item.TypeName = GetType(ToolboxControl2).ToString() Then 
    
                newFilter = New ToolboxItemFilterAttribute("System.Windows.Forms.UserControl", ToolboxItemFilterType.Prevent) 
            End If 
            If newFilter IsNot Nothing Then 
                Dim array As New ArrayList() 
                array.Add(newFilter) 
                item.Filter = DirectCast(array.ToArray(GetType(ToolboxItemFilterAttribute)), ToolboxItemFilterAttribute()) 
            End If 
        End Sub 
    End Class
    
    [ProvideAssemblyFilterAttribute("Vsip.*, Version=*, Culture=*, PublicKeyToken=*")]
        [GuidAttribute("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")]
        public sealed class ToolboxConfig : IConfigureToolboxItem {
            public ToolboxConfig() {
            }
    
    
            /// <summary>
            ///     Adds extra configuration information to this toolbox item.
            /// </summary>
            public void ConfigureToolboxItem(ToolboxItem item) {
                if (item == null)
                    return;
    
                //hide from .NET Compact Framework  on the device designer.
                ToolboxItemFilterAttribute newFilter = null;
                if (item.TypeName == typeof(ToolboxControl1).ToString()) {
                    newFilter = new ToolboxItemFilterAttribute("System.Windows.Forms.UserControl",
                                                          ToolboxItemFilterType.Require);
                } 
                else if (item.TypeName == typeof(ToolboxControl2).ToString()) {
    
                    newFilter = new ToolboxItemFilterAttribute("System.Windows.Forms.UserControl",
                                                          ToolboxItemFilterType.Prevent);
                }
                if (newFilter != null) {
                    ArrayList array = new ArrayList();
                    array.Add(newFilter);
                    item.Filter = (ToolboxItemFilterAttribute[])
                            array.ToArray(typeof(ToolboxItemFilterAttribute));
                }
            }
        }
    }
    
  3. 註冊為提供的特定實作的 VSPackage IConfigureToolboxItem藉由套用的執行個體ProvideToolboxItemConfigurationAttribute VSPackage 實作的Package

    下列範例會通知Visual Studio所實作的封裝的環境Vsip.ItemConfiguration.ItemConfiguration提供型別的類別Vsip.ItemConfiguration.ToolboxConfiguration支援動態ToolboxItem

    <ProvideToolboxItemsAttribute(3)> _ 
    <DefaultRegistryRoot("Software\Microsoft\VisualStudio\8.0")> _ 
    <InstalledProductRegistration(False, "#100", "#102", "1.0", IconResourceID := 400)> _ 
    <ProvideLoadKey("Standard", "1.0", "Package Name", "Company", 1)> _ 
    <ProvideMenuResource(1000, 1)> _ 
    <ProvideToolboxItemConfigurationAttribute(GetType(ToolboxConfig))> _ 
    <GuidAttribute("YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYYY")> _ 
    Public Class ItemConfiguration 
        Inherits Package 
    End Class
    
    [ProvideToolboxItemsAttribute(3)]
    [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\8.0")]
    [InstalledProductRegistration(false, "#100", "#102", "1.0", IconResourceID = 400)]
    [ProvideLoadKey("Standard", "1.0", "Package Name", "Company", 1)]
    [ProvideMenuResource(1000, 1)]
    [ProvideToolboxItemConfigurationAttribute(typeof(ToolboxConfig))]
    
    [GuidAttribute("YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYYY")]
    public class ItemConfiguration : Package
    

自訂的拖放支援

除了要加入至工具箱ToolboxItem物件和它們的實作可以用來擴充中的拖放支援Visual Studio IDE。 這可以讓任意的剪貼簿格式,以公開給工具箱 ,在編輯器中。

管理套件架構為基礎的 VSPackages 必須登錄為提供自訂工具箱項目所套用的執行個體的剪貼簿格式, ProvideToolboxFormatAttribute類別實作Package

如需詳細資訊,以適當地註冊工具箱提供者,請參閱正在註冊工具箱支援功能

若要提供自訂的剪貼簿格式及拖放支援工具箱控制項

  1. 實作建立ToolboxItemCreatorCallback委派。

    這項實作會傳回ToolboxItem支援非標準的剪貼簿格式的物件。

    項目的實作範例的ToolboxItemCreatorCallback委派,請參閱ToolboxItemToolboxItemCreatorCallback參考網頁。

  2. 進行這項實作的ToolboxItemCreatorCallback委派可以使用Visual Studio工具箱的非標準的工具箱,藉由呼叫AddCreator。 

    <GuidAttribute("7D91995B-A799-485e-BFC7-C52545DFB5DD")> _ 
    <ProvideToolboxFormatAttribute("MyFormat")> _ 
    Public Class ItemConfiguration 
        Inherits MSVSIP.Package 
        Public Overloads Overrides Sub Initialize() 
            '"Adding this class as a ToolboxItemCreator"); 
            Dim toolbox As IToolboxService = DirectCast(host.GetService(GetType(IToolboxService)), IToolboxService) 
            If toolbox IsNot Nothing Then 
                toolboxCreator = New ToolboxItemCreatorCallback(Me.OnCreateToolboxItem) 
                toolbox.AddCreator(toolboxCreator, "MyFormat", host) 
            End If 
        End Sub 
    End Class
    
    [GuidAttribute("7D91995B-A799-485e-BFC7-C52545DFB5DD")]
    [ProvideToolboxFormatAttribute("MyFormat")]
    public class ItemConfiguration : MSVSIP.Package
    {
        public override void Initialize() { 
            /*
            */
            //"Adding this class as a ToolboxItemCreator");
            IToolboxService toolbox = (IToolboxService)host.GetService(typeof(IToolboxService));
            if (toolbox != null) {
                toolboxCreator = new ToolboxItemCreatorCallback(this.OnCreateToolboxItem);
                toolbox.AddCreator(toolboxCreator, "MyFormat", host);
            }
            private ToolboxItem OnCreateToolboxItem(object serializedData, string format) {
               /*
                */
            }
        }
    }
    

本章節內容

請參閱

工作

如何:控制工具箱

概念

正在註冊工具箱支援功能

How to: 使用 Interop 組件提供自訂工具箱項目

管理工具箱

其他資源

工具箱 (Visual Studio SDK)

工具箱的逐步解說