ControlDesigner 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
擴充 Control 的設計模式行為。
public ref class ControlDesigner : System::ComponentModel::Design::ComponentDesigner
public class ControlDesigner : System.ComponentModel.Design.ComponentDesigner
type ControlDesigner = class
inherit ComponentDesigner
Public Class ControlDesigner
Inherits ComponentDesigner
- 繼承
- 衍生
範例
下列範例 ControlDesigner 實作示範處理 MouseEnter
和 MouseLeave
事件、從設計工具程式碼繪製控制項,以及使用 介面的 IDesignerFilter 一部分,在設計階段加入控制項的屬性。 下列範例程式碼包含與設計工具相關聯的設計工具與範例使用者控制項。 若要建置此範例,請將範例編譯為類別庫、將程式庫的參考新增至Windows Forms專案、將控制項新增至 [工具箱],然後將控制項的實例新增至表單。 當您指向控制項時,控制項周邊的內部外框會反白顯示,而用來繪製外框的色彩會對應 OutlineColor
至設計工具已新增至控制項所列屬性的屬性。
新增 System.Design 元件的參考,以編譯器代碼範例。
using namespace System;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Design;
using namespace System::Collections;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::Windows::Forms::Design;
using namespace System::Security::Permissions;
public ref class TestControlDesigner: public System::Windows::Forms::Design::ControlDesigner
{
private:
bool mouseover;
Color lineColor;
public:
property Color OutlineColor
{
Color get()
{
return lineColor;
}
void set( Color value )
{
lineColor = value;
}
}
TestControlDesigner()
{
mouseover = false;
lineColor = Color::White;
}
protected:
virtual void OnMouseEnter() override
{
this->mouseover = true;
this->Control->Refresh();
}
virtual void OnMouseLeave() override
{
this->mouseover = false;
this->Control->Refresh();
}
virtual void OnPaintAdornments( System::Windows::Forms::PaintEventArgs^ pe ) override
{
if ( this->mouseover )
pe->Graphics->DrawRectangle( gcnew Pen( gcnew SolidBrush( this->lineColor ),6 ), 0, 0, this->Control->Size.Width, this->Control->Size.Height );
}
protected:
[ReflectionPermission(SecurityAction::Demand, Flags=ReflectionPermissionFlag::MemberAccess)]
virtual void PreFilterProperties( System::Collections::IDictionary^ properties ) override
{
properties->Add( "OutlineColor", TypeDescriptor::CreateProperty( TestControlDesigner::typeid, "OutlineColor", System::Drawing::Color::typeid, nullptr ) );
}
};
[DesignerAttribute(TestControlDesigner::typeid)]
public ref class TestControl: public System::Windows::Forms::UserControl
{
private:
System::ComponentModel::Container^ components;
public:
TestControl()
{
components = gcnew System::ComponentModel::Container;
}
protected:
~TestControl()
{
if ( components != nullptr )
{
delete components;
}
}
};
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace ControlDesignerExample
{
// ExampleControlDesigner is an example control designer that
// demonstrates basic functions of a ControlDesigner.
public class ExampleControlDesigner : System.Windows.Forms.Design.ControlDesigner
{
// This Boolean state reflects whether the mouse is over the control.
private bool mouseover = false;
// This color is a private field for the OutlineColor property.
private Color lineColor = Color.White;
// This color is used to outline the control when the mouse is
// over the control.
public Color OutlineColor
{
get
{
return lineColor;
}
set
{
lineColor = value;
}
}
public ExampleControlDesigner()
{
}
// Sets a value and refreshes the control's display when the
// mouse position enters the area of the control.
protected override void OnMouseEnter()
{
this.mouseover = true;
this.Control.Refresh();
}
// Sets a value and refreshes the control's display when the
// mouse position enters the area of the control.
protected override void OnMouseLeave()
{
this.mouseover = false;
this.Control.Refresh();
}
// Draws an outline around the control when the mouse is
// over the control.
protected override void OnPaintAdornments(System.Windows.Forms.PaintEventArgs pe)
{
if (this.mouseover)
{
pe.Graphics.DrawRectangle(
new Pen(new SolidBrush(this.lineColor), 6),
0,
0,
this.Control.Size.Width,
this.Control.Size.Height);
}
}
// Adds a property to this designer's control at design time
// that indicates the outline color to use.
// The DesignOnlyAttribute ensures that the OutlineColor
// property is not serialized by the designer.
protected override void PreFilterProperties(System.Collections.IDictionary properties)
{
PropertyDescriptor pd = TypeDescriptor.CreateProperty(
typeof(ExampleControlDesigner),
"OutlineColor",
typeof(System.Drawing.Color),
new Attribute[] { new DesignOnlyAttribute(true) });
properties.Add("OutlineColor", pd);
}
}
// This example control demonstrates the ExampleControlDesigner.
[DesignerAttribute(typeof(ExampleControlDesigner))]
public class ExampleControl : System.Windows.Forms.UserControl
{
private System.ComponentModel.Container components = null;
public ExampleControl()
{
components = new System.ComponentModel.Container();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
}
}
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Collections
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
Namespace ControlDesignerExample
_
' ExampleControlDesigner is an example control designer that
' demonstrates basic functions of a ControlDesigner.
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Public Class ExampleControlDesigner
Inherits System.Windows.Forms.Design.ControlDesigner
' This boolean state reflects whether the mouse is over the control.
Private mouseover As Boolean = False
' This color is a private field for the OutlineColor property.
Private lineColor As Color = Color.White
' This color is used to outline the control when the mouse is
' over the control.
Public Property OutlineColor() As Color
Get
Return lineColor
End Get
Set(ByVal Value As Color)
lineColor = Value
End Set
End Property
Public Sub New()
End Sub
' Sets a value and refreshes the control's display when the
' mouse position enters the area of the control.
Protected Overrides Sub OnMouseEnter()
Me.mouseover = True
Me.Control.Refresh()
End Sub
' Sets a value and refreshes the control's display when the
' mouse position enters the area of the control.
Protected Overrides Sub OnMouseLeave()
Me.mouseover = False
Me.Control.Refresh()
End Sub
' Draws an outline around the control when the mouse is
' over the control.
Protected Overrides Sub OnPaintAdornments(ByVal pe As System.Windows.Forms.PaintEventArgs)
If Me.mouseover Then
pe.Graphics.DrawRectangle(New Pen(New SolidBrush(Me.lineColor), 6), 0, 0, Me.Control.Size.Width, Me.Control.Size.Height)
End If
End Sub
' Adds a property to this designer's control at design time
' that indicates the outline color to use.
' The DesignOnlyAttribute ensures that the OutlineColor
' property is not serialized by the designer.
Protected Overrides Sub PreFilterProperties(ByVal properties As System.Collections.IDictionary)
Dim pd As PropertyDescriptor = TypeDescriptor.CreateProperty( _
GetType(ExampleControlDesigner), _
"OutlineColor", _
GetType(System.Drawing.Color), _
New Attribute() {New DesignOnlyAttribute(True)})
properties.Add("OutlineColor", pd)
End Sub
End Class
' This example control demonstrates the ExampleControlDesigner.
<DesignerAttribute(GetType(ExampleControlDesigner))> _
Public Class ExampleControl
Inherits System.Windows.Forms.UserControl
Private components As System.ComponentModel.Container = Nothing
Public Sub New()
components = New System.ComponentModel.Container()
End Sub
Protected Overloads Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If (components IsNot Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
End Class
End Namespace
備註
ControlDesigner 提供衍生自 Control 之元件設計工具的基類。 除了繼承自 ComponentDesigner 類別的方法和功能之外, ControlDesigner 還提供其他方法來支援在設計階段擴充和改變相關聯 Control 的行為。
您可以使用 建立設計工具與型別 DesignerAttribute 的關聯。 如需自訂設計階段行為的概觀,請參閱 擴充Design-Time支援。
建構函式
ControlDesigner() |
初始化 ControlDesigner 類別的新執行個體。 |
欄位
accessibilityObj |
指定設計工具的可及性物件。 |
InvalidPoint |
屬性
方法
明確介面實作
IDesignerFilter.PostFilterAttributes(IDictionary) |
如需這個成員的描述,請參閱 PostFilterAttributes(IDictionary) 方法。 (繼承來源 ComponentDesigner) |
IDesignerFilter.PostFilterEvents(IDictionary) |
如需這個成員的描述,請參閱 PostFilterEvents(IDictionary) 方法。 (繼承來源 ComponentDesigner) |
IDesignerFilter.PostFilterProperties(IDictionary) |
如需這個成員的描述,請參閱 PostFilterProperties(IDictionary) 方法。 (繼承來源 ComponentDesigner) |
IDesignerFilter.PreFilterAttributes(IDictionary) |
如需這個成員的描述,請參閱 PreFilterAttributes(IDictionary) 方法。 (繼承來源 ComponentDesigner) |
IDesignerFilter.PreFilterEvents(IDictionary) |
如需這個成員的描述,請參閱 PreFilterEvents(IDictionary) 方法。 (繼承來源 ComponentDesigner) |
IDesignerFilter.PreFilterProperties(IDictionary) |
如需這個成員的描述,請參閱 PreFilterProperties(IDictionary) 方法。 (繼承來源 ComponentDesigner) |
ITreeDesigner.Children |
如需這個成員的描述,請參閱 Children 屬性。 (繼承來源 ComponentDesigner) |
ITreeDesigner.Parent |
如需這個成員的描述,請參閱 Parent 屬性。 (繼承來源 ComponentDesigner) |
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應