博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
像Component的Control
阅读量:4191 次
发布时间:2019-05-26

本文共 3002 字,大约阅读时间需要 10 分钟。

 
像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

你可能感兴趣的文章
需求工程???
查看>>
清晰性和可测试性的权衡
查看>>
软件思考系列之一
查看>>
测试观念谈
查看>>
Layers, Levels, and DIP铪铪
查看>>
自底向上设计
查看>>
Hackers and Painters
查看>>
代码自动生成和抽象
查看>>
权衡的艺术
查看>>
软件测试认识中的误区
查看>>
Slashdot对Bjarne Stroustrup的采访
查看>>
递归之美 - Loki库TypeList源码剖析
查看>>
基于hibernate的das实现研究(4)
查看>>
基于hibernate的das实现研究(3)
查看>>
基于hibernate的das实现研究(1)
查看>>
基于hibernate的das实现研究(2)
查看>>
eclipse的jre设置
查看>>
世界是平的------说说SOA,jini,jxta,jbi,OSGi
查看>>
1.1 jBPM BPEL的wsdl扩展实现
查看>>
手机流媒体技术概要
查看>>