ControlDesigner 类
扩展 Control 的设计模式行为。
**命名空间:**System.Windows.Forms.Design
**程序集:**System.Design(在 system.design.dll 中)
语法
声明
Public Class ControlDesigner
Inherits ComponentDesigner
用法
Dim instance As ControlDesigner
public class ControlDesigner : ComponentDesigner
public ref class ControlDesigner : public ComponentDesigner
public class ControlDesigner extends ComponentDesigner
public class ControlDesigner extends ComponentDesigner
备注
ControlDesigner 为从 Control 派生的组件的设计器提供基类。除了从 ComponentDesigner 类继承的方法和功能外,ControlDesigner 还提供其他方法来支持在设计时扩展和改变关联的 Control 的行为。
可以使用 DesignerAttribute 将设计器与类型关联起来。有关自定义设计时行为的概述,请参见 扩展设计时支持。
示例
下面的示例中的 ControlDesigner 实现说明了如何处理 MouseEnter 和 MouseLeave 事件、如何通过设计器代码在控件上绘图以及如何使用部分 IDesignerFilter 接口在设计时为控件添加属性。下面的代码示例包含一个设计器以及与该设计器相关联的示例用户控件。要生成此示例,请将该示例编译成类库,为 Windows 窗体项目添加对该库的引用,将此控件添加到“工具箱”中,并向窗体中添加该控件的实例。指向控件时,控件周边的内部轮廓会突出显示,用来绘制轮廓的颜色相应于 OutlineColor
属性,设计器已将此属性添加到针对控件列出的属性中。
Imports System
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 TestControlDesigner
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.
Protected Overrides Sub PreFilterProperties(ByVal properties As System.Collections.IDictionary)
properties.Add("OutlineColor", TypeDescriptor.CreateProperty(GetType(TestControlDesigner), "OutlineColor", GetType(System.Drawing.Color), Nothing))
End Sub
End Class
' This example control demonstrates the ExampleControlDesigner.
<DesignerAttribute(GetType(TestControlDesigner))> _
Public Class TestControl
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 Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
End Class
End Namespace
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.
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
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.
protected override void PreFilterProperties(System.Collections.IDictionary properties)
{
properties.Add("OutlineColor", TypeDescriptor.CreateProperty(typeof(ExampleControlDesigner), "OutlineColor", typeof(System.Drawing.Color), null));
}
}
// 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 );
}
}
}
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;
}
}
};
继承层次结构
System.Object
System.ComponentModel.Design.ComponentDesigner
System.Windows.Forms.Design.ControlDesigner
System.Windows.Forms.Design.ParentControlDesigner
线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台
Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
请参见
参考
ControlDesigner 成员
System.Windows.Forms.Design 命名空间
ComponentDesigner
IDesigner
DesignerAttribute