IDesignerHost Interface

Definition

提供用于管理设计器事务和组件的接口。Provides an interface for managing designer transactions and components.

public interface class IDesignerHost : IServiceProvider, System::ComponentModel::Design::IServiceContainer
public interface class IDesignerHost : System::ComponentModel::Design::IServiceContainer
public interface IDesignerHost : IServiceProvider, System.ComponentModel.Design.IServiceContainer
[System.Runtime.InteropServices.ComVisible(true)]
public interface IDesignerHost : IServiceProvider, System.ComponentModel.Design.IServiceContainer
public interface IDesignerHost : System.ComponentModel.Design.IServiceContainer
type IDesignerHost = interface
    interface IServiceContainer
    interface IServiceProvider
Public Interface IDesignerHost
Implements IServiceContainer, IServiceProvider
Public Interface IDesignerHost
Implements IServiceContainer
Derived
Attributes
Implements

Examples

下面的示例代码演示如何从设计器或已放置的组件获取 IDesignerHost 服务接口。The following example code demonstrates how to obtain the IDesignerHost service interface from a designer or sited component.

// Requests an IDesignerHost service from the design time environment using Component.Site.GetService()
IDesignerHost^ dh = static_cast<IDesignerHost^>(this->Component->Site->GetService( IDesignerHost::typeid ));
// Requests an IDesignerHost service from the design time environment using Component.Site.GetService()
IDesignerHost dh = (IDesignerHost) this.Component.Site.GetService(typeof(IDesignerHost));			
' Requests an IDesignerHost service from the design time environment using Component.Site.GetService()
Dim host As IDesignerHost = CType(Me.Component.Site.GetService(GetType(IDesignerHost)), IDesignerHost)

下面的示例代码演示如何使用 IDesignerHost 接口来列出项目组件。The following example code demonstrates using the IDesignerHost interface to list project components.

#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
#using <System.dll>

using namespace System;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Design;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::Security::Permissions;

// Provides a form containing a listbox that can display 
// a list of project components.
public ref class DesignerHostListForm: public System::Windows::Forms::Form
{
public:
   System::Windows::Forms::ListBox^ listBox1;

private:
   System::Windows::Forms::Button^ ok_button;

public:
   DesignerHostListForm()
   {
      this->Name = "DesignerHostListForm";
      this->Text = "List of design-time project components";
      this->SuspendLayout();
      this->listBox1 = gcnew System::Windows::Forms::ListBox;
      this->listBox1->Location = System::Drawing::Point( 8, 8 );
      this->listBox1->Name = "listBox1";
      this->listBox1->Size = System::Drawing::Size( 385, 238 );
      this->listBox1->TabIndex = 0;
      this->listBox1->Anchor = static_cast<AnchorStyles>(((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom) | System::Windows::Forms::AnchorStyles::Left) | System::Windows::Forms::AnchorStyles::Right);
      this->ok_button = gcnew System::Windows::Forms::Button;
      this->ok_button->DialogResult = System::Windows::Forms::DialogResult::OK;
      this->ok_button->Location = System::Drawing::Point( 232, 256 );
      this->ok_button->Name = "ok_button";
      this->ok_button->TabIndex = 1;
      this->ok_button->Text = "OK";
      this->ok_button->Anchor = static_cast<AnchorStyles>(System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right);
      this->ClientSize = System::Drawing::Size( 400, 285 );
      array<System::Windows::Forms::Control^>^temp2 = {this->ok_button,this->listBox1};
      this->Controls->AddRange( temp2 );
      this->ResumeLayout( false );
   }

public:
   ~DesignerHostListForm()
   {
   }
};


// You can double-click the component of an IDesignerHostExampleDesigner 
// to show a form containing a listbox that lists the name and type 
// of each component or control in the current design-time project.
public ref class IDesignerHostExampleDesigner: public IDesigner
{
private:
   System::ComponentModel::IComponent^ component;

public:
   IDesignerHostExampleDesigner(){}

   virtual void DoDefaultAction()
   {
      ListComponents();
   }

   virtual void Initialize( System::ComponentModel::IComponent^ component )
   {
      this->component = component;
      MessageBox::Show( "Double-click the IDesignerHostExample component to view a list of project components." );
   }


private:

   // Displays a list of components in the current design 
   // document when the default action of the designer is invoked.
   void ListComponents()
   {
      DesignerHostListForm^ listform = gcnew DesignerHostListForm;

      // Obtain an IDesignerHost service from the design environment.
      IDesignerHost^ host = dynamic_cast<IDesignerHost^>(this->component->Site->GetService( IDesignerHost::typeid ));

      // Get the project components container (control containment depends on Controls collections)
      IContainer^ container = host->Container;

      // Add each component's type name and name to the list box.
      System::Collections::IEnumerator^ myEnum = container->Components->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         IComponent^ component = safe_cast<IComponent^>(myEnum->Current);
         listform->listBox1->Items->Add( String::Concat( component->GetType()->Name, " : ", component->Site->Name ) );
      }

      listform->ShowDialog();
   }

public:

   property System::ComponentModel::IComponent^ Component 
   {
      virtual System::ComponentModel::IComponent^ get()
      {
         return this->component;
      }
   }

   property System::ComponentModel::Design::DesignerVerbCollection^ Verbs 
   {
      [PermissionSetAttribute(SecurityAction::Demand, Name="FullTrust")]
      virtual System::ComponentModel::Design::DesignerVerbCollection^ get()
      {
         DesignerVerbCollection^ dvc = gcnew DesignerVerbCollection;
         dvc->Add( gcnew DesignerVerb( "List Components",gcnew EventHandler( this, &IDesignerHostExampleDesigner::ListHandler ) ) );
         return dvc;
      }
   }

private:
   void ListHandler( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      ListComponents();
   }

public:
   ~IDesignerHostExampleDesigner(){}
};


// IDesignerHostExampleComponent is a component associated 
// with the IDesignerHostExampleDesigner that demonstrates 
// acquisition and use of the IDesignerHost service 
// to list project components.

[DesignerAttribute(IDesignerHostExampleDesigner::typeid)]
public ref class IDesignerHostExampleComponent: public System::ComponentModel::Component
{
public:
   IDesignerHostExampleComponent(){}

public:
   ~IDesignerHostExampleComponent(){}
};
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows.Forms;

namespace IDesignerHostExample
{	
    // IDesignerHostExampleComponent is a component associated 
    // with the IDesignerHostExampleDesigner that demonstrates 
    // acquisition and use of the IDesignerHost service 
    // to list project components.
    [DesignerAttribute(typeof(IDesignerHostExampleDesigner))]
    public class IDesignerHostExampleComponent : System.ComponentModel.Component
    {
        public IDesignerHostExampleComponent()
        {}

        protected override void Dispose( bool disposing )
        {
            base.Dispose( disposing );
        }
    }

    // You can double-click the component of an IDesignerHostExampleDesigner 
    // to show a form containing a listbox that lists the name and type 
    // of each component or control in the current design-time project.
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] 
    public class IDesignerHostExampleDesigner : IDesigner
    {
        private System.ComponentModel.IComponent component;

        public IDesignerHostExampleDesigner()
        {}

        public void DoDefaultAction()
        {
            ListComponents();
        }

        public void Initialize(System.ComponentModel.IComponent component)
        {
            this.component = component;
            MessageBox.Show("Double-click the IDesignerHostExample component to view a list of project components.");
        }

        // Displays a list of components in the current design 
        // document when the default action of the designer is invoked.
        private void ListComponents()
        {
            using (DesignerHostListForm listform = new DesignerHostListForm())
            {
                // Obtain an IDesignerHost service from the design environment.
                IDesignerHost host = (IDesignerHost)this.component.Site.GetService(typeof(IDesignerHost));
                // Get the project components container (control containment depends on Controls collections)
                IContainer container = host.Container;
                // Add each component's type name and name to the list box.
                foreach (IComponent component in container.Components)
                {
                    listform.listBox1.Items.Add(component.GetType().Name + " : " + component.Site.Name);
                }
                // Display the form.
                listform.ShowDialog();
            }
        }

        public System.ComponentModel.IComponent Component
        {
            get
            {
                return this.component;
            }
        }

        public System.ComponentModel.Design.DesignerVerbCollection Verbs
        {
            get
            {
                DesignerVerbCollection dvc = new DesignerVerbCollection();
                dvc.Add( new DesignerVerb("List Components", new EventHandler(ListHandler)) );
                return dvc;
            }
        }

        private void ListHandler(object sender, EventArgs e)
        {
            ListComponents();
        }

        public void Dispose() {	}
    }

    // Provides a form containing a listbox that can display 
    // a list of project components.
    public class DesignerHostListForm : System.Windows.Forms.Form
    {
        public System.Windows.Forms.ListBox listBox1;
        private System.Windows.Forms.Button ok_button;
        
        public DesignerHostListForm()
        {
            this.Name = "DesignerHostListForm";
            this.Text = "List of design-time project components";
            this.SuspendLayout();
            this.listBox1 = new System.Windows.Forms.ListBox();						
            this.listBox1.Location = new System.Drawing.Point(8, 8);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(385, 238);
            this.listBox1.TabIndex = 0;	
            this.listBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);		
            this.ok_button = new System.Windows.Forms.Button();
            this.ok_button.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.ok_button.Location = new System.Drawing.Point(232, 256);
            this.ok_button.Name = "ok_button";
            this.ok_button.TabIndex = 1;
            this.ok_button.Text = "OK";
            this.ok_button.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
            this.ClientSize = new System.Drawing.Size(400, 285);
            this.Controls.AddRange(new System.Windows.Forms.Control[] { this.ok_button, this.listBox1 });
            this.ResumeLayout(false);	
        }

        protected override void Dispose( bool disposing )
        {			
            base.Dispose( disposing );
        }	
    }
}
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Windows.Forms

Namespace IDesignerHostExample
   
   ' IDesignerHostExampleComponent is a component associated 
   ' with the IDesignerHostExampleDesigner that demonstrates 
   ' acquisition and use of the IDesignerHost service 
   ' to list project components.
    <DesignerAttribute(GetType(IDesignerHostExampleDesigner))> _
    Public Class IDesignerHostExampleComponent
        Inherits System.ComponentModel.Component

        Public Sub New()
        End Sub

        Protected Overloads Sub Dispose(ByVal disposing As Boolean)
            MyBase.Dispose(disposing)
        End Sub 
    End Class 

    ' You can double-click the component of a IDesignerHostExampleDesigner
    ' to show a form containing a listbox that lists the name and type 
    ' of each component or control in the current design-time project.
    <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
    Public Class IDesignerHostExampleDesigner
        Implements IDesigner
        Private component_ As System.ComponentModel.IComponent

        Public Sub New()
        End Sub

        Public Sub DoDefaultAction() Implements IDesigner.DoDefaultAction
            ListComponents()
        End Sub

        Public Sub Initialize(ByVal component As System.ComponentModel.IComponent) Implements IDesigner.Initialize
            Me.component_ = component
            MessageBox.Show("Double-click the IDesignerHostExample component to view a list of project components.")
        End Sub

        ' Displays a list of components in the current design 
        ' document when the default action of the designer is invoked.
        Private Sub ListComponents()

            Using listform As New DesignerHostListForm()

                ' Obtain an IDesignerHost service from the design environment.
                Dim host As IDesignerHost = CType(Me.Component.Site.GetService(GetType(IDesignerHost)), IDesignerHost)
                ' Get the project components container (control containment depends on Controls collections)
                Dim container As IContainer = host.Container
                ' Add each component's type name and name to the list box.
                Dim comp As Component
                For Each comp In container.Components
                    listform.listBox1.Items.Add((comp.GetType().Name + " : " + Component.Site.Name))
                Next comp
                ' Display the form.
                listform.ShowDialog()

            End Using

        End Sub

        Public ReadOnly Property Component() As System.ComponentModel.IComponent Implements IDesigner.Component
            Get
                Return component_
            End Get
        End Property

        Public ReadOnly Property Verbs() As System.ComponentModel.Design.DesignerVerbCollection Implements IDesigner.Verbs
            Get
                Dim dvc As New DesignerVerbCollection()
                dvc.Add(New DesignerVerb("List Components", New EventHandler(AddressOf ListHandler)))
                Return dvc
            End Get
        End Property

        Private Sub ListHandler(ByVal sender As Object, ByVal e As EventArgs)
            ListComponents()
        End Sub

        Public Sub Dispose() Implements IDisposable.Dispose
        End Sub
    End Class
    _ 

    ' Provides a form containing a list box that can display 
    ' a list of project components.
    Public Class DesignerHostListForm
        Inherits System.Windows.Forms.Form
        Public listBox1 As System.Windows.Forms.ListBox
        Private ok_button As System.Windows.Forms.Button

        Public Sub New()
            Me.Name = "DesignerHostListForm"
            Me.Text = "List of design-time project components"
            Me.SuspendLayout()
            Me.listBox1 = New System.Windows.Forms.ListBox()
            Me.listBox1.Location = New System.Drawing.Point(8, 8)
            Me.listBox1.Name = "listBox1"
            Me.listBox1.Size = New System.Drawing.Size(385, 238)
            Me.listBox1.TabIndex = 0
            Me.listBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
            Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right)
            Me.ok_button = New System.Windows.Forms.Button()
            Me.ok_button.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.ok_button.Location = New System.Drawing.Point(232, 256)
            Me.ok_button.Name = "ok_button"
            Me.ok_button.TabIndex = 1
            Me.ok_button.Text = "OK"
            Me.ok_button.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right)
            Me.ClientSize = New System.Drawing.Size(400, 285)
            Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.ok_button, Me.listBox1})
            Me.ResumeLayout(False)
        End Sub 

        Protected Overloads Sub Dispose(ByVal disposing As Boolean)
            MyBase.Dispose(disposing)
        End Sub 
    End Class 
End Namespace 

Remarks

IDesignerHost 是一种接口,与 .NET Framework 窗体设计器体系结构一起提供对设计器事务和组件管理的支持。IDesignerHost is an interface that works with the .NET Framework forms designer architecture to provide support for designer transaction and component management.

.NET Framework 不提供此接口的实现。The .NET Framework does not provide an implementation of this interface. 接口由支持设计器的开发工具实现。The interface is implemented by development tools that support designers.

Notes to Callers

若要从开发环境中获取 IDesignerHost 的实现,请在组件在设计模式下处于活动状态时调用 GetService(Type),同时传递 IDesignerHost 类型以请求 IDesignerHost 服务接口。To obtain an implementation of IDesignerHost from a development environment, call GetService(Type) while your component is active in design mode, passing the type of IDesignerHost to request an IDesignerHost service interface.

IDesignerHost 提供了与设计器状态相关的以下成员:IDesignerHost provides the following members related to designer state: -Loading 属性指示是加载设计器还是文档。- The Loading property indicates whether a designer or document is being loaded.

-在显示之前激活设计器时发生 Activated 事件。- The Activated event occurs when a designer is activated before display.

-停用设计器时发生 Deactivated 事件。- The Deactivated event occurs when a designer is deactivated.

-加载文档后,LoadComplete 事件发生。- The LoadComplete event occurs after a document is loaded.

-Activate() 方法激活设计器。- The Activate() method activates the designer.

IDesignerHost 提供了与管理组件相关的以下成员:IDesignerHost provides the following members related to managing components: -Container 属性指示设计器宿主的容器。- The Container property indicates the container for the designer host.

-RootComponent 属性指示根组件的基类。- The RootComponent property indicates the base class for the root component.

-RootComponentClassName 属性指示根组件的类的名称。- The RootComponentClassName property indicates the name of the class of the root component.

-CreateComponent(Type) 方法创建指定类型的组件。- The CreateComponent(Type) method creates the specified type of component.

-DestroyComponent(IComponent) 方法会销毁指定组件。- The DestroyComponent(IComponent) method destroys the specified component.

-GetDesigner(IComponent) 方法获取与指定组件关联的设计器。- The GetDesigner(IComponent) method gets the designer associated with a specified component.

-GetType(String) 方法获取具有指定名称的类型的实例。- The GetType(String) method gets an instance of the type with the specified name.

IDesignerHost 提供了与管理事务相关的以下成员:IDesignerHost provides the following members related to managing transactions: -InTransaction 属性指示设计器是否在事务中。- The InTransaction property indicates whether the designer is in a transaction.

-TransactionDescription 属性指示当前事务说明。- The TransactionDescription property indicates the current transaction description.

-完成事务时发生 TransactionClosed 事件。- The TransactionClosed event occurs when a transaction has been completed.

-当事务即将完成时,将发生 TransactionClosing 事件。- The TransactionClosing event occurs when a transaction is about to be completed.

-事务开始时发生 TransactionOpened 事件。- The TransactionOpened event occurs when a transaction has begun.

-当事务即将开始时发生 TransactionOpening 事件。- The TransactionOpening event occurs when a transaction is about to begin.

-CreateTransaction() 方法创建并返回一个新事务。- The CreateTransaction() method creates and returns a new transaction.

Properties

Container

获取此设计器宿主的容器。Gets the container for this designer host.

InTransaction

获取一个值,该值指示设计器宿主当前是否在事务中。Gets a value indicating whether the designer host is currently in a transaction.

Loading

获取一个值,该值指示设计器宿主当前是否正在加载文档。Gets a value indicating whether the designer host is currently loading the document.

RootComponent

获取用作当前设计的根组件的基类的实例。Gets the instance of the base class used as the root component for the current design.

RootComponentClassName

获取所设计的类的完全限定名。Gets the fully qualified name of the class being designed.

TransactionDescription

获取当前事务的说明。Gets the description of the current transaction.

Methods

Activate()

激活此宿主所承载的设计器。Activates the designer that this host is hosting.

AddService(Type, Object)

将指定的服务添加到服务容器中。Adds the specified service to the service container.

(Inherited from IServiceContainer)
AddService(Type, Object, Boolean)

将指定的服务添加到服务容器,并将此服务提升到任何父级服务容器(可选)。Adds the specified service to the service container, and optionally promotes the service to any parent service containers.

(Inherited from IServiceContainer)
AddService(Type, ServiceCreatorCallback)

将指定的服务添加到服务容器中。Adds the specified service to the service container.

(Inherited from IServiceContainer)
AddService(Type, ServiceCreatorCallback, Boolean)

将指定的服务添加到服务容器,并将此服务提升到父级服务容器(可选)。Adds the specified service to the service container, and optionally promotes the service to parent service containers.

(Inherited from IServiceContainer)
CreateComponent(Type)

创建指定类型的组件,并将其添加到设计文档中。Creates a component of the specified type and adds it to the design document.

CreateComponent(Type, String)

创建指定类型和名称的组件,并将其添加到设计文档中。Creates a component of the specified type and name, and adds it to the design document.

CreateTransaction()

创建 DesignerTransaction,它可封装事件序列以提高性能并启用撤消和重复支持功能。Creates a DesignerTransaction that can encapsulate event sequences to improve performance and enable undo and redo support functionality.

CreateTransaction(String)

使用指定的事务说明创建 DesignerTransaction,它可封装事件序列以提高性能并启用撤消和重复支持功能。Creates a DesignerTransaction that can encapsulate event sequences to improve performance and enable undo and redo support functionality, using the specified transaction description.

DestroyComponent(IComponent)

销毁指定组件并将其从设计器容器中移除。Destroys the specified component and removes it from the designer container.

GetDesigner(IComponent)

获取包含指定组件的设计器实例。Gets the designer instance that contains the specified component.

GetService(Type)

获取指定类型的服务对象。Gets the service object of the specified type.

(Inherited from IServiceProvider)
GetType(String)

获取指定的完全限定类型名称的实例。Gets an instance of the specified, fully qualified type name.

RemoveService(Type)

从服务容器中删除指定的服务类型。Removes the specified service type from the service container.

(Inherited from IServiceContainer)
RemoveService(Type, Boolean)

从服务容器中删除指定的服务类型,并将此服务提升到父级服务容器(可选)。Removes the specified service type from the service container, and optionally promotes the service to parent service containers.

(Inherited from IServiceContainer)

Events

Activated

在激活此设计器时发生。Occurs when this designer is activated.

Deactivated

在停用此设计器时发生。Occurs when this designer is deactivated.

LoadComplete

在此设计器加载完文档时发生。Occurs when this designer completes loading its document.

TransactionClosed

添加 TransactionClosed 事件的事件处理程序。Adds an event handler for the TransactionClosed event.

TransactionClosing

添加 TransactionClosing 事件的事件处理程序。Adds an event handler for the TransactionClosing event.

TransactionOpened

添加 TransactionOpened 事件的事件处理程序。Adds an event handler for the TransactionOpened event.

TransactionOpening

添加 TransactionOpening 事件的事件处理程序。Adds an event handler for the TransactionOpening event.

Applies to

See also