位置:海鸟网 > IT > ASP.NET >

在VB.NET中定制一个3D的Label的源码

Public Class Lebel3D
    Inherits System.Windows.Forms.UserControl  
#Region " Windows 窗体设计器生成的代码 "  
    Public Sub New()
        MyBase.New()  
        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()  
        '在 InitializeComponent() 调用之后添加任何初始化
        mCaption = "Label3D"
        mAlignment = Align.CenterMiddle
        mAutoSize = False
        mEffect = Effect3D.Raised
        setstyle(ControlStyles.ResizeRedraw, True)
    End Sub  
    'UserControl1 重写 dispose 以清理组件列表。
    Protected Overloads Overrides 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  
    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer
    '注意:以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()        '
        Me.Name = "Label3D"  
    End Sub  
#End Region  
    Private Shared mAlignment As Align
    Private Shared mAutoSize As Boolean
    Private Shared mEffect As Effect3D
    Private mCaption As String  
    Public Property Alignment() As Align
        Get
            Return mAlignment
        End Get
        Set(ByVal Value As Align)
            mAlignment = Value
            Invalidate()
        End Set
    End Property  
    Public Property AutoSize() As Boolean
        Get
            Return mAutoSize
        End Get
        Set(ByVal Value As Boolean)
            mAutoSize = Value
            Invalidate()
        End Set
    End Property  
    Public Property Effect() As Effect3D
        Get
            Return mEffect
        End Get
        Set(ByVal Value As Effect3D)
            mEffect = Value
            Invalidate()
        End Set
    End Property  
    Public Property Caption() As String
        Get
            Return mCaption
        End Get
        Set(ByVal Value As String)
            mCaption = Value
            Invalidate()
        End Set
    End Property  
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim lblFont As Font = Me.Font
        Dim lblBrush As New SolidBrush(Color.Red)
        Dim X, Y As Integer
        Dim textSize As SizeF  
        textSize = e.Graphics.MeasureString(mCaption, lblFont)  
        If mAutoSize Then
            Me.Width = textSize.Width
            Me.Height = textSize.Height
        End If  
        Select Case Me.mAlignment
            Case Align.BottomLeft
                X = 0
                Y = Me.Height - textSize.Height
            Case Align.BottomMiddle
                X = CInt((Me.Width - textSize.Width) / 2)
                Y = Me.Height - textSize.Height
            Case Align.BottomRight
                X = Me.Width - textSize.Width '- 2
                Y = Me.Height - textSize.Height
            Case Align.CenterLeft
                X = 0
                Y = (Me.Height - textSize.Height) / 2
            Case Align.CenterMiddle
                X = CInt((Me.Width - textSize.Width) / 2)
                Y = (Me.Height - textSize.Height) / 2
            Case Align.CenterRight
                X = Me.Width - textSize.Width '- 2
                Y = (Me.Height - textSize.Height) / 2
            Case Align.TopLeft
                X = 0
                Y = 0
            Case Align.TopMiddle
                X = CInt((Me.Width - textSize.Width) / 2)
                Y = 0
            Case Align.TopRight
                X = Me.Width - textSize.Width ' - 2
                Y = 0
        End Select  
        Dim dispX, dispY As Integer
        Select Case mEffect
            Case Effect3D.None : dispX = 0 : dispY = 0
            Case Effect3D.Carved : dispX = 1 : dispY = 1
            Case Effect3D.CarvedHeavy : dispX = 2 : dispY = 2
            Case Effect3D.Raised : dispX = -1 : dispY = -1
            Case Effect3D.RaisedHeavy : dispX = -2 : dispY = -2
        End Select  
        e.Graphics.Clear(Me.BackColor)
        lblBrush.Color = Color.White
        e.Graphics.DrawString(mCaption, lblFont, lblBrush, X, Y)
        lblBrush.Color = Me.ForeColor
        If Me.DesignMode Then
            e.Graphics.DrawString("Designtime", New Font("verdana", 24, FontStyle.Bold), New SolidBrush(Color.FromArgb(200, 230, 200, 255)), 0, 0)
        Else
            e.Graphics.DrawString("Runtime", New Font("verdana", 24, FontStyle.Bold), New SolidBrush(Color.FromArgb(200, 230, 200, 255)), 0, 0)
        End If
        e.Graphics.DrawString(mCaption, lblFont, lblBrush, X + dispX, Y + dispY)
    End Sub  
    Private mOnAlignmentChanged As EventHandler
    Private mOnAutoSizeChanged As EventHandler
    Private mOnEffectChanged As EventHandler
    Private mOnCaptionChanged As EventHandler  
    Public Event AlignmentChanged(ByVal sender As Object, ByVal ev As EventArgs)
    Public Event AutoSizeChanged(ByVal sender As Object, ByVal ev As EventArgs)
    Public Event EffectChanged(ByVal sender As Object, ByVal ev As EventArgs)
    Public Event CaptionChanged(ByVal sender As Object, ByVal ev As EventArgs)  
    Protected Overridable Sub OnAlignmentChanged(ByVal e As EventArgs)
        Invalidate()
        If Not (mOnAlignmentChanged Is Nothing) Then mOnAlignmentChanged.Invoke(Me, e)
    End Sub
    Protected Overridable Sub OnEffectChanged(ByVal e As EventArgs)
        Invalidate()
        If Not (mOnEffectChanged Is Nothing) Then mOnEffectChanged.Invoke(Me, e)
    End Sub
    Protected Overridable Sub OnAutoSizeChanged(ByVal e As EventArgs)
        Invalidate()
        If Not (mOnAutosizeChanged Is Nothing) Then mOnAutosizeChanged.Invoke(Me, e)
    End Sub
    Protected Overridable Sub OnCaptionChanged(ByVal e As EventArgs)
        Invalidate()
        If Not (mOnCaptionChanged Is Nothing) Then mOnCaptionChanged.Invoke(Me, e)
    End Sub
End Class  
Public Enum Align
    TopLeft
    TopMiddle
    TopRight
    CenterLeft
    CenterMiddle
    CenterRight
    BottomLeft
    BottomMiddle
    BottomRight
End Enum  
Public Enum Effect3D
    None
    Raised
    RaisedHeavy
    Carved
    CarvedHeavy
End Enum