IDesigner IDesigner IDesigner IDesigner Interface

定義

提供建置 (Build) 自訂設計工具的基本架構。Provides the basic framework for building a custom designer.

public interface class IDesigner : IDisposable
[System.Runtime.InteropServices.ComVisible(true)]
public interface IDesigner : IDisposable
type IDesigner = interface
    interface IDisposable
Public Interface IDesigner
Implements IDisposable
衍生
屬性
實作

範例

這個範例會示範IDesigner將本機參考儲存至元件的執行, 並在按兩下該元件時執行預設動作, 並提供設計工具動詞功能表命令。This example demonstrates an IDesigner implementation that stores a local reference to its component, performs a default action when the component is double-clicked, and provides a designer verb menu command.

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

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

public ref class ExampleIDesigner: public System::ComponentModel::Design::IDesigner
{
private:

   // Local reference to the designer's component.
   IComponent^ component;

public:

   property System::ComponentModel::IComponent^ Component 
   {
      // Public accessor to the designer's component.
      virtual System::ComponentModel::IComponent^ get()
      {
         return component;
      }
   }
   ExampleIDesigner(){}

   virtual void Initialize( System::ComponentModel::IComponent^ component )
   {
      // This method is called after a designer for a component is created,
      // and stores a reference to the designer's component.
      this->component = component;
   }

   // This method peforms the 'default' action for the designer. The default action 
   // for a basic IDesigner implementation is invoked when the designer's component 
   // is double-clicked. By default, a component associated with a basic IDesigner 
   // implementation is displayed in the design-mode component tray.
   virtual void DoDefaultAction()
   {
      // Shows a message box indicating that the default action for the designer was invoked.
      MessageBox::Show( "The DoDefaultAction method of an IDesigner implementation was invoked.", "Information" );
   }

   property System::ComponentModel::Design::DesignerVerbCollection^ Verbs 
   {
      // Returns a collection of designer verb menu items to show in the 
      // shortcut menu for the designer's component.
      [PermissionSetAttribute(SecurityAction::Demand, Name="FullTrust")]
      virtual System::ComponentModel::Design::DesignerVerbCollection^ get()
      {
         DesignerVerbCollection^ verbs = gcnew DesignerVerbCollection;
         DesignerVerb^ dv1 = gcnew DesignerVerb( "Display Component Name",gcnew EventHandler( this, &ExampleIDesigner::ShowComponentName ) );
         verbs->Add( dv1 );
         return verbs;
      }
   }

private:

   // Event handler for displaying a message box showing the designer's component's name.
   void ShowComponentName( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      if ( this->Component != nullptr )
            MessageBox::Show( this->Component->Site->Name, "Designer Component's Name" );
   }

public:

   // Provides an opportunity to release resources before object destruction.
   ~ExampleIDesigner(){}

};

// A DesignerAttribute associates the example IDesigner with an example control.

[DesignerAttribute(ExampleIDesigner::typeid)]
public ref class TestControl: public System::Windows::Forms::UserControl
{
public:
   TestControl(){}

};
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace IDesignerExample
{	
    // A DesignerAttribute associates the example IDesigner with an example control.
    [DesignerAttribute(typeof(ExampleIDesigner))]
    public class TestControl : System.Windows.Forms.UserControl
    {				
        public TestControl()
        {	
        }
    }

    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] 
    public class ExampleIDesigner : System.ComponentModel.Design.IDesigner
    {
        // Local reference to the designer's component.
        private IComponent component; 
        // Public accessor to the designer's component.
        public System.ComponentModel.IComponent Component
        {
            get
            {
                return component;
            }            
        }

        public ExampleIDesigner()
        {            
        }

        public void Initialize(System.ComponentModel.IComponent component)
        {
            // This method is called after a designer for a component is created,
            // and stores a reference to the designer's component.
            this.component = component;
        }        
        
        // This method peforms the 'default' action for the designer. The default action 
        // for a basic IDesigner implementation is invoked when the designer's component 
        // is double-clicked. By default, a component associated with a basic IDesigner 
        // implementation is displayed in the design-mode component tray.
        public void DoDefaultAction()
        {
            // Shows a message box indicating that the default action for the designer was invoked.
            MessageBox.Show("The DoDefaultAction method of an IDesigner implementation was invoked.", "Information");
        }

        // Returns a collection of designer verb menu items to show in the 
        // shortcut menu for the designer's component.
        public System.ComponentModel.Design.DesignerVerbCollection Verbs
        {
            get
            {
                DesignerVerbCollection verbs = new DesignerVerbCollection();
                DesignerVerb dv1 = new DesignerVerb("Display Component Name", new EventHandler(this.ShowComponentName));
                verbs.Add( dv1 );
                return verbs;
            }
        }

        // Event handler for displaying a message box showing the designer's component's name.
        private void ShowComponentName(object sender, EventArgs e)
        {
            if( this.Component != null )
                MessageBox.Show( this.Component.Site.Name, "Designer Component's Name" );
        }

        // Provides an opportunity to release resources before object destruction.
        public void Dispose()
        {        
        }
    }
}
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Windows.Forms

' A DesignerAttribute associates the example IDesigner with an example control.
<DesignerAttribute(GetType(ExampleIDesigner))> _
Public Class TestControl
    Inherits System.Windows.Forms.UserControl

    Public Sub New()
    End Sub
End Class

<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Public Class ExampleIDesigner
    Implements System.ComponentModel.Design.IDesigner

    ' Local reference to the designer's component.
    Private _component As IComponent

    ' Public accessor to the designer's component.
    Public ReadOnly Property Component() As System.ComponentModel.IComponent Implements IDesigner.Component
        Get
            Return _component
        End Get
    End Property

    Public Sub New()
    End Sub

    Public Sub Initialize(ByVal component As System.ComponentModel.IComponent) Implements IDesigner.Initialize
        ' This method is called after a designer for a component is created,
        ' and stores a reference to the designer's component.
        Me._component = component
    End Sub

    ' This method peforms the 'default' action for the designer. The default action 
    ' for a basic IDesigner implementation is invoked when the designer's component 
    ' is double-clicked. By default, a component associated with a basic IDesigner 
    ' implementation is displayed in the design-mode component tray.
    Public Sub DoDefaultAction() Implements IDesigner.DoDefaultAction
        ' Shows a message box indicating that the default action for the designer was invoked.
        MessageBox.Show("The DoDefaultAction method of an IDesigner implementation was invoked.", "Information")
    End Sub

    ' Returns a collection of designer verb menu items to show in the 
    ' shortcut menu for the designer's component.
    Public ReadOnly Property Verbs() As System.ComponentModel.Design.DesignerVerbCollection Implements IDesigner.Verbs
        Get
            Dim verbs_ As New DesignerVerbCollection()
            Dim dv1 As New DesignerVerb("Display Component Name", New EventHandler(AddressOf Me.ShowComponentName))
            verbs_.Add(dv1)
            Return verbs_
        End Get
    End Property

    ' Event handler for displaying a message box showing the designer's component's name.
    Private Sub ShowComponentName(ByVal sender As Object, ByVal e As EventArgs)
        If (Me.Component IsNot Nothing) Then
            MessageBox.Show(Me.Component.Site.Name, "Designer Component's Name")
        End If
    End Sub

    ' Provides an opportunity to release resources before object destruction.
    Public Sub Dispose() Implements IDisposable.Dispose
    End Sub

End Class

備註

IDesigner介面會提供介面, 讓您可以在其中執行設計工具的基本服務。The IDesigner interface provides an interface through which you can implement basic services for a designer. 設計工具可以在設計階段修改元件的行為, 而且可能會提供自己的服務和行為。A designer can modify the behavior of a component at design time, and may provide its own services and behavior. 設計工具只在設計階段作用, 而且必須與使用的DesignerAttribute元件類型相關聯, 才能在設計階段建立相關聯類型的元件時載入。A designer is only active at design-time, and must be associated with a type of component using a DesignerAttribute in order to be loaded when a component of the associated type is created at design time.

IDesigner介面會提供您可以執行的方法和屬性, 以便在設計階段提供自訂行為。The IDesigner interface provides methods and properties that you can implement in order to provide custom behavior at design time.

執行設計工具的方法,以便在建立元件時執行動作。InitializeImplement the Initialize method of a designer to perform actions when a component is created. 如果元件在設計階段應該具有特殊設定, 或視設計工具可以判斷的條件而定, 則這會很有用。This can be useful if a component should have a special configuration at design-time, or if its configuration should change depending on conditions that the designer can determine.

當使用者以滑鼠右鍵按一下設計階段環境中的元件或控制項時, 設計工具可以在快捷方式功能表上提供功能表命令。A designer can provide menu commands on the shortcut menu that is displayed when a user right-clicks a component or control in the design-time environment. 您可以執行Verbs屬性來定義 get 存取子, 以傳回, 其中包含DesignerVerbDesignerVerbCollection來產生功能表命令的物件。You can implement the Verbs property to define a get accessor that returns a DesignerVerbCollection containing the DesignerVerb objects for generating menu commands.

當按兩下元件時, 元件匣中出現之元件的設計工具可以執行預設動作。A designer for a component that appears in the component tray can perform a default action when the component is double-clicked. DoDefaultAction執行方法, 以指定按兩下元件時要執行的行為。Implement the DoDefaultAction method to specify the behavior to perform when the component is double-clicked.

設計工具也可以使用可用的設計階段服務來執行各種不同的工作, 包括調查元件及其屬性的目前設計階段環境、讀取和設定元件屬性的值、管理工具箱、管理選取的元件, 或顯示可用來設定值或套用進一步處理的使用者介面。A designer can also use the available design-time services to perform a variety of tasks, including surveying the current design-time environment for components and their properties, reading and setting the values of properties of components, managing the toolbox, managing selected components, or displaying a user interface that can be used to configure values or to apply further processing.

若要為可放置在表單上的控制項, 執行設計工具, 您可以繼承自ControlDesigner類別。To implement a designer for a control that can be sited on a form, you can inherit from the ControlDesigner class. 關聯的設計工具不是衍生自ControlDesigner的控制項會顯示在元件匣中。Controls whose associated designer does not derive from ControlDesigner are displayed in the component tray. 和類別會實作為IDesigner介面, 並提供其他設計階段支援, 可供設計人員的作者使用。 ControlDesigner ComponentDesignerThe ComponentDesigner and ControlDesigner classes implement the IDesigner interface and provide additional design-time support that may be of use to authors of designers. 如需詳細資訊, 請參閱這些類別的參考檔。For more information, see the reference documentation for these classes.

如需建立設計元件的總覽, 請參閱擴充設計階段支援For an overview of creating design components, please see Extending Design-Time Support.

屬性

Component Component Component Component

取得此設計工具正在設計的基底元件。Gets the base component that this designer is designing.

Verbs Verbs Verbs Verbs

取得設計工具所支援的設計階段動詞命令集合。Gets a collection of the design-time verbs supported by the designer.

方法

Dispose() Dispose() Dispose() Dispose()

執行與釋放 (Free)、釋放 (Release) 或重設 Unmanaged 資源相關聯之應用程式定義的工作。Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

(Inherited from IDisposable)
DoDefaultAction() DoDefaultAction() DoDefaultAction() DoDefaultAction()

執行這個設計工具的預設動作。Performs the default action for this designer.

Initialize(IComponent) Initialize(IComponent) Initialize(IComponent) Initialize(IComponent)

使用指定的元件,初始化設計工具。Initializes the designer with the specified component.

適用於

另請參閱