像Component的Control

摘要:像Component的Control

當我第一次接觸到ToolStrip控件時,我很好奇,為何此控件的行為就像是Component一樣,意思是當你將其拖到Form上後,除了在Form上看到她外,你還可以在Component Tray上找到她,如下圖:

那這是如何達到的呢?在經過探索.NET Designer內部行為後,我發現到DocumentDesigner會針對ToolStrip控件做特別的處理,除了運行一般的控件處理外, DocumentDesigner最後會將ToolStrip加到ComponentTray中,這個動作使得ToolStrip控件可以出現在 Component Tray(下方的元件區)中。好了!知道.NET Designer是如何達到這個效果了之後,接下來的問題是,我們自訂的控件也可以做到這點嗎?答案是肯定的,雖然DocumentDesigner屬於 Form Designer層級,我們無法在不建立自訂Form的情況下改變其行為,但我們可以由ComponentDesigner下手,將該控件加到 Component Tray中,要達到這點,第一件事是要取得ComponentTray的實體,這可透過以下的程式碼達到。
public class MyTextBoxDesigner : ControlDesigner
{
        public override void Initialize(IComponent component)
        {
            IDesignerHost host = (IDesignerHost)component.Site.GetService(typeof(IDesignerHost));
            ComponentTray tray = (ComponentTray)host.GetService(typeof(ComponentTray));
            ........................
            base.Initialize(component);
        }
}
程式中首先透過componet.Site.GetService函式來取得DesignerHost物件,透過這個物件,我們可以取得目前作 用中的ComponentTray物件,然後就能將自訂的控件加到Component Tray中,這樣就能讓控件出現在Component Tray區中。
public class MyTextBoxDesigner : ControlDesigner
{
        public override void Initialize(IComponent component)
        {
            ...................
            if (tray == null)
            {               
                IComponent comp = host.CreateComponent(typeof(MyComponent));
                tray = (ComponentTray)host.GetService(typeof(ComponentTray));
                if (tray == null)
                    throw new SystemException("can't create component tray.");               
                tray.AddComponent(component);
                host.DestroyComponent(comp);
            }
            else
                tray.AddComponent(component);
            base.Initialize(component);
        }
}
由於ComponentTray物件在該Form上無任何Component的情況下,是不會被建立的,因此當ComponentTray未被 建立時,程式會透過DesignHost物件來建立一個DummyComponent,這個DummyComponent是我們自訂的 Component,其只是一個極為簡單、無作用的Component。
[ToolboxItem(false)]
public class DummyComponent : Component
{
}
當這個DummyComponent被加入後,ComponetTray物件就會被建立起來,剩下的工作就是將控件加到 ComponentTray中,並移除DummyComponent,這樣一來,我們就完成了一個可出現於Component Tray中的控件了,以下是完整的程式碼。
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
 
namespace ClassLibrary1
{
    [Designer(typeof(MyTextBoxDesigner))]
    public class MyTextBox:TextBox
    {
        private string _information;
 
        public string Information
        {
            get
            {
                return _information;
            }
            set
            {
                _information = value;
            }
        }
    }
 
    [ToolboxItem(false)]
    public class MyComponent : Component
    {
    }
 
    public class MyTextBoxDesigner : ControlDesigner
    {
        public override void Initialize(IComponent component)
        {
            IDesignerHost host = (IDesignerHost)component.Site.GetService(typeof(IDesignerHost));
            ComponentTray tray = (ComponentTray)host.GetService(typeof(ComponentTray));
            if (tray == null)
            {               
                IComponent comp = host.CreateComponent(typeof(DummyComponent));
                tray = (ComponentTray)host.GetService(typeof(ComponentTray));
                if (tray == null)
                    throw new SystemException("can't create component tray.");               
                tray.AddComponent(component);
                host.DestroyComponent(comp);
            }
            else
                tray.AddComponent(component);
            base.Initialize(component);
        }
    }
}
  
下圖是執行結果。
v