像Component的Control
来源:互联网 发布:php posix 编辑:程序博客网 时间:2024/06/11 06:31
像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
- 像Component的Control
- 像Component的Control
- 像Component的Control
- 像Component的Control
- 像Component的Control
- 像Component的Control
- 像Component的Control
- 像Component的Control
- 像Component的Control
- 像Component的Control
- 像Component的Control
- 像Component的Control
- 像Component的Control
- 像Component的Control
- 像Component的Control
- 像Component的Control
- 像Component的Control
- 像Component的Control
- JavaScript组件之JQuery(A~Z)教程(基于Asp.net运行环境)[示例代码下载]
- 撰写信息书籍注意事项
- .Net Remoting中Remote Server的Port占用/释放问题
- Asp.net 2.0 自定义控件开发[开发一个图表(WebChart)控件(柱状图示例)](示例代码下载)
- 书评--规划极致软件制程(Planning Extreme Programming)
- 像Component的Control
- Asp.net 2.0 自定义控件开发专题讲解[为用户控件增加DataSource属性, 能够自动识别不同数据源](示例代码下载)
- 2007.5.13 祝天下所有的母亲: 节日快乐!
- 信道、接收器、接收链和信道接受提供程序
- 书评--资料仓储理论与实务
- Inside ASP.NET 2.0-即时编译系统
- Asp.net 2.0自定义控件开发相关的几种嵌入资源解决方案
- 任务状态段和控制门
- Asp.net 2.0控件开发相关调试(JavaScript调试和自定义控件设计时调试)