像Component的Control

来源:互联网 发布:爱奇艺pps影音linux版 编辑:程序博客网 时间:2024/06/03 03:08
 
像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
 


Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1054420


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 洗空调洗坏了怎么办 老师是条青花鱼怎么办 吃了带鱼和南瓜怎么办 苹果平板ad忘了怎么办 小米6进海水了怎么办 小米手机掉海水里怎么办 苹果7进海水了怎么办 7p手机进海水怎么办 育海参苗出现红细菌怎么办 苹果手机掉进厕所怎么办 2个月的婴儿肺炎怎么办 甜甜圈珊瑚脱骨怎么办 宝宝吃了生物球怎么办 狗尾巴被剪掉了怎么办 魟鱼尾巴刺了怎么办 狗咬过了24小时怎么办 狗咬超过24小时怎么办 狗咬超过48小时怎么办 狗咬超过72小时怎么办 狗抓超过24小时怎么办 龙须树叶子发黄怎么办? 车被广告牌砸了怎么办 开花店压力好大怎么办 快成熟葡萄软果怎么办 木本叶长白斑点了怎么办 天猫卖家超过72小时未发货怎么办 淘宝超过72小时不发货怎么办 鸿运当头干叶了怎么办 百合枝干长歪了怎么办 植物主干长歪了怎么办 钱兜子花叶子黄怎么办 丝瓜有苦味吃了怎么办 吃了苦的西葫芦怎么办 吃了苦的西葫芦中毒怎么办 吃了一点苦丝瓜怎么办 丝瓜苦味我吃了怎么办? 小孩牙地包天怎么办 小孩的牙地包天怎么办 小孩牙是地包天怎么办 布偶猫生宝宝时怎么办 仔猪拉稀打针喂药都不好怎么办