透明标签 .NET 控件






4.57/5 (15投票s)
本文档介绍了如何在 .NET Framework 中创建透明标签控件,uLabelX 透明标签控件支持边框样式、图像以及具有渐变背景的父控件。
引言
我认为很多人在创建具有渐变背景的控件上的透明标签时遇到困难。
本文档介绍了一种在基于 Windows 的应用程序中显示透明标签的方法。此库中的 uLabelX
控件旨在外观灵活且时尚。首先,它支持边框形状、图像以及具有渐变背景的父控件。与控件关联的图像可以以 Alpha 混合形式绘制。控件中的文本可以使用高级效果绘制。所有这些吸引人的特性还可以针对控件的不同状态进行个性化定制。

Using the Code
该项目包含以下文件
- BorderStyles.vb (指定控件的边框样式)
- ImageSizes.vb (指定控件的图像大小)
- uLabelXDesigner.vb (指定用于实现控件设计时服务的类)
- uLabelX.vb (指定自定义控件类)
控件类 uLabelX
继承自基类 Control。
Public Class uLabelX
Inherits Control
构造函数
Public Sub New()
InitializeComponent()
Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
Me.SetStyle(ControlStyles.ResizeRedraw, True)
Me.SetStyle(ControlStyles.Opaque, False)
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, False)
AddHandler Me.PropertyChanged, AddressOf OnPropertyChanged
RaiseEvent PropertyChanged(False)
End Sub
事件
事件声明
Protected Event PropertyChanged(ByVal _RecreateHandle As Boolean)
属性更改时调用的方法
Private Sub OnPropertyChanged(ByVal _RecreateHandle As Boolean)
If (_RecreateHandle = True) Then Me.RecreateHandle()
Me.Invalidate()
End Sub
属性
' Gets or sets the Image of the control
Public Property Image() As Image
Get
Return m_Image
End Get
Set(ByVal value As Image)
m_Image = value
RaiseEvent PropertyChanged(True)
End Set
End Property
' Gets or sets the ImageSize of the control
Public ReadOnly Property ImageSize() As ImageSizes
Get
Return m_ImageSize
End Get
End Property
' Gets or sets the BorderStyle of the control
Public Shadows Property BorderStyle() _
As iGreen.Controls.uControls.uLabelX.Common.BorderStyles
Get
Return m_BorderStyle
End Get
Set(ByVal value As iGreen.Controls.uControls.uLabelX.Common.BorderStyles)
m_BorderStyle = value
RaiseEvent PropertyChanged(True)
End Set
End Property
' Gets or sets the BorderDashStyle of the control
Public Property BorderDashStyle() As System.Drawing.Drawing2D.DashStyle
Get
Return m_BorderDashStyle
End Get
Set(ByVal value As System.Drawing.Drawing2D.DashStyle)
m_BorderDashStyle = value
RaiseEvent PropertyChanged(True)
End Set
End Property
' Gets or sets the BorderWidth of the control
Public Property BorderWidth() As Single
Get
Return m_BorderWidth
End Get
Set(ByVal value As Single)
m_BorderWidth = value
RaiseEvent PropertyChanged(True)
End Set
End Property
' Gets or sets the BorderColor of the control
Public Property BorderColor() As Color
Get
Return m_BorderColor
End Get
Set(ByVal value As Color)
m_BorderColor = value
RaiseEvent PropertyChanged(True)
End Set
End Property
' Gets or sets the Text of the control.
' Represents Text as a series of Unicode characters.
Public Overrides Property Text() As String
Get
Return Replace(MyBase.Text, "ULabelX", "uLabelX")
End Get
Set(ByVal value As String)
MyBase.Text = value
RaiseEvent PropertyChanged(True)
End Set
End Property
' Specifies alignment of Text on the control.
' Placement and direction of text in relation to the control border
Public Property TextAlign() As ContentAlignment
Get
Return m_TextAlign
End Get
Set(ByVal value As ContentAlignment)
m_TextAlign = value
RaiseEvent PropertyChanged(True)
End Set
End Property
方法
在这里我们正在重写 OnPaintBackground()
,否则背景绘制任务会破坏最近重新绘制的父控件内容,通过覆盖 OnPaintBackground()
方法来实现。
Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
' NOTHING TODO:
End Sub
重写以下内容以刷新
控件
Protected Overrides Sub OnFontChanged(ByVal e As System.EventArgs)
RaiseEvent PropertyChanged(True)
End Sub
Protected Overrides Sub OnForeColorChanged(ByVal e As System.EventArgs)
RaiseEvent PropertyChanged(True)
End Sub
重写基类 OnPaint()
以绘制控件
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
e.Graphics.CompositingMode = CompositingMode.SourceOver
e.Graphics.CompositingQuality = CompositingQuality.GammaCorrected
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
Call DrawControlBorder(e.Graphics, Me.ClientRectangle)
Call DrawControlText(e.Graphics, Me.ClientRectangle)
Call DrawControlImage(e.Graphics, Me.ClientRectangle)
End Sub
为了从设计时属性页中删除不必要的属性,我在这里使用 ControlDesigner
类的 PostFilterProperties。
Imports System.Windows.Forms.Design
Namespace Common
Friend Class uLabelXDesigner
Inherits ControlDesigner
Protected Overrides Sub PostFilterProperties_
(ByVal _Properties As System.Collections.IDictionary)
_Properties.Remove("BackColor")
_Properties.Remove("BackgroundImage")
_Properties.Remove("BackgroundImageLayout")
_Properties.Remove("RightToLeft")
_Properties.Remove("TabStop")
_Properties.Remove("TabIndex")
_Properties.Remove("AutoSize")
MyBase.PostFilterProperties(_Properties)
End Sub
End Class
End Namespace
希望这能有所帮助。
祝您编程愉快!!!