IDesignerHost 介面

定義

提供用來管理設計工具異動和元件的介面。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
[<System.Runtime.InteropServices.ComVisible(true)>]
type IDesignerHost = interface
    interface IServiceContainer
    interface IServiceProvider
Public Interface IDesignerHost
Implements IServiceContainer, IServiceProvider
Public Interface IDesignerHost
Implements IServiceContainer
衍生
屬性
實作

範例

下列範例程式碼示範如何 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 

備註

IDesignerHost 是一個介面,可搭配 .NET Framework forms 設計工具架構來提供設計工具交易和元件管理的支援。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.

給呼叫者的注意事項

若要從開發環境取得的實作為 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.

屬性

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.

方法

Activate()

啟動這個主應用程式正在裝載的設計工具。Activates the designer that this host is hosting.

AddService(Type, Object)

將指定的服務加入至服務容器。Adds the specified service to the service container.

(繼承來源 IServiceContainer)
AddService(Type, Object, Boolean)

將指定的服務加入至服務容器,並選擇性地將服務升級至任何父項服務容器。Adds the specified service to the service container, and optionally promotes the service to any parent service containers.

(繼承來源 IServiceContainer)
AddService(Type, ServiceCreatorCallback)

將指定的服務加入至服務容器。Adds the specified service to the service container.

(繼承來源 IServiceContainer)
AddService(Type, ServiceCreatorCallback, Boolean)

將指定的服務加入至服務容器,並選擇性地將服務升級至父項服務容器。Adds the specified service to the service container, and optionally promotes the service to parent service containers.

(繼承來源 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.

(繼承來源 IServiceProvider)
GetType(String)

取得指定的、完整的型別名稱的執行個體。Gets an instance of the specified, fully qualified type name.

RemoveService(Type)

從服務容器中移除指定的服務類型。Removes the specified service type from the service container.

(繼承來源 IServiceContainer)
RemoveService(Type, Boolean)

從服務容器中移除指定的服務類型,並選擇性地將服務升級至父項服務容器。Removes the specified service type from the service container, and optionally promotes the service to parent service containers.

(繼承來源 IServiceContainer)

事件

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.

適用於

另請參閱