三、文本增强过滤效果控件
包含在示例控件库中的上面6个文本改进效果控件中的每一个基本上都是以相同的格式创建。在此,我们不想逐个描述这些控件,而只描述一下CCEmboss控件。该CCEmboss控件继承自System.Web.UI.WebControl;我已经把对System.Design的引用添加到基本web控件上,并且添加了一个import语句以便在工程中包括System.Web.UI.Design库。这种添加对于建立一些设计时刻支持元素(以便使该控件在设计时刻更易于使用)是必要的。该代码被分为三个独立的区域:Declarations,Properties和Rendering。下面,让我们看一下该类定义的开始:
Imports System Imports System.Collections.Generic Imports System.ComponentModel Imports System.Text Imports System.Web Imports System.Web.UI Imports System.Web.UI.Design Imports System.Web.UI.WebControls <Designer(GetType(EmbossedLabelDesigner))>_ <ParseChildren(False)>_ Public Class CCEmboss Inherits WebControl | 注意,在import语句后,属性数据显示该类将使用一个包含在EmbossedLabelDesigner类中的定制设计器。后面,我们将描述这个设计器,它负责为该控件提供某种设计时刻支持。还要注意,已经添加了ParseChildren属性并且被设置为false。这是为了防止页面分析器分析该控件的内容,因为该控件是一个容器控件并且它的内容不属于那个控件一部分。
Declarations区域跟随其后而且很简单;它包含几个private型成员变量用于存储用户对这个控件的选择信息。有关这个过滤效果,你可以参考一下微软文档来确定是否还有任何其它需要探讨的属性。
#Region "Declarations" Private mEnabled As Boolean Private mBias As Single #End Region | 在此,mEnabled属性是一个布尔值,它将被传递到过滤效果中,并且它恰好能实现你希望的效果;它能够启动或停用该效果。这里的mBias用于决定事件的范围。我相信微软文档在这个问题上显示,0.7是典型的并且是针对这种效果的缺省值。
接下来是Properties区域,它的内容限于对private型成员变量的内容提供公共存取。其实现代码大致如下:
#Region "Properties" <Category("Embossed Label")>_ <Browsable(True)>_ <Description("Enable or display the embossed effect.")>_ Public Property EmbossEnabled() As Boolean Get EnsureChildControls() Return mEnabled End Get Set(ByVal value As Boolean) EnsureChildControls() mEnabled = value End Set End Property <Category("Embossed Label")>_ <Browsable(True)>_ <Description("Set the bias for the embossed effect (typically 0.7).")>_ Public Property Bias() As Single Get EnsureChildControls() Return mBias End Get Set(ByVal value As Single) EnsureChildControls() mBias = value End Set End Property #End Region | 最后一部分是Rendering区域。它包含在运行时刻激活该效果的必要代码;其代码大致如下所示:
#Region "Rendering" Protected Overrides Sub AddAttributesToRender(ByVal writer As HtmlTextWriter) writer.AddStyleAttribute(HtmlTextWriterStyle.Filter, _ "progid:DXImageTransform.Microsoft.Emboss(bias=" & Bias.ToString() & _ ",enabled = " & EmbossEnabled.ToString() & ");width:" & Width.Value.ToString() & "px") MyBase.AddAttributesToRender(writer) End Sub #End Region End Class | 注意,在此我们重载了AddAttributesToRender子例程,并且使用HtmlTextWriter来添加一个style属性。该属性相应于DirectX过滤器。你还能够看到,这个较早暴露的属性被传递给在该过滤器定义内的这个子例程。这是在运行时刻把过滤效果添加到容器的位置。
这一节后面是EmbossedLabelDesigner类的定义。这个类可以被写入一个单独的类文件中;然而我更喜欢这种方法,因为它使设计代码及相应目标一切都那么清晰。这部分代码为该控件提供设计时刻支持:
Public Class EmbossedLabelDesigner Inherits ContainerControlDesigner Protected Overrides Sub AddDesignTimeCssAttributes(ByVal styleAttributes As System.Collections.IDictionary) Dim embossLbl As CCEmboss = CType(Me.Component, CCEmboss) styleAttributes.Add("filter","progid:DXImageTransform.Microsoft.Emboss(bias=" & embossLbl.Bias.ToString() & ",enabled = " & embossLbl.Enabled.ToString() & ");width:" & embossLbl.Width.Value.ToString() & "px") MyBase.AddDesignTimeCssAttributes(styleAttributes) End Sub End Class | 如你所见,你基本上在该控件上添加了与在运行时刻一样的过滤效果,以便你能够看到该过滤的相同的可视化效果;然而,你现在使用的是表单设计器。这一点很好地归纳了这其中每个控件的工作方式,尽管你会注意到在表单的其它属性和方法中也存在一些微小区别。
[上一页] [1] [2] [3] [下一页]
|