Feedback

VB - Austauschbares Farblayout-System

Veröffentlicht von am 6/16/2008
(1 Bewertungen)
Arbeiten mit unterschiedlichen Farblayout-Systemen


Damit man einen komfortablen Zugriff auf das Farbsystem und die verwendeten Farben hat, erfolgt dies über eine Enum-Auflistung und generischen Klassen.

1. Erstellen von abstrakten Klassen (MustInherit), einmalig

LayoutSystemFactory(Of T, U)
ColorSystemFactory(Of T)

2. Erstellen eines Vertrages (Interface), einmalig

ILayoutSystem(Of T, U)
IColorSystem(Of T)

3. Farbsysteme festlegen, einmalig pro Anwendung

Public Enum ColorMiniLayout
Public Class MiniGrey
Inherits ColorSystemFactory(Of ColorMiniLayout)

Punkte 1-3 als VB-Code-Klasse hinterlegt.


4. Farblayout-System anwenden

' ... Form erstellen

Public Class Form1
Implements IColorSystem(Of ColorMiniLayout)

' ... das erzeugt folgendes Property

Public Property ColorSystem() As ColorSystemFactory(Of ColorMiniLayout) Implements IColorSystem(Of ColorMiniLayout).ColorSystem
Get
Return FARB
End Get
Set(ByVal value As ColorSystemFactory(Of ColorMiniLayout))
FARB = value
If FARB Is Nothing Then FARB = New MiniGrey
End Set
End Property

' ... private Farbfactrory-Variable erzeugen

Private FARB As ColorSystemFactory(Of ColorMiniLayout)

' ... anwenden z.B.

Me.BackColor = Me.FARB.Farbe(ColorMiniLayout.BackGround)
Namespace HTD.FarbSystem

#Region " Interfaces "

    ''' <summary>
    ''' das Interface für das Farbsystem
    ''' </summary>
    ''' <typeparam name="T">Farbbennung mittels Enum</typeparam>
    ''' <remarks></remarks>
    Public Interface IColorSystem(Of T)
        Property ColorSystem() As ColorSystemFactory(Of T)
    End Interface

    ''' <summary>
    ''' Interface für benanntes Layout-System
    ''' </summary>
    ''' <typeparam name="T"></typeparam>
    ''' <typeparam name="U"></typeparam>
    ''' <remarks></remarks>
    Public Interface ILayoutSystem(Of T, U)
        Property LayoutSystem() As LayoutSystemFactory(Of T, U)
    End Interface

#End Region

#Region " Abstrakte Klassen "

    ''' <summary>
    ''' Festlegung für Zugriff auf ein Layoutsystem
    ''' </summary>
    ''' <typeparam name="T"></typeparam>
    ''' <typeparam name="U"></typeparam>
    ''' <remarks></remarks>
    <Serializable()> Public MustInherit Class LayoutSystemFactory(Of T, U)
        Implements IDisposable

        Private Values As Dictionary(Of T, U)

        Public Sub New()
            Values = New Dictionary(Of T, U)
        End Sub

        ''' <summary>
        ''' benannte Wert des LayoutSystems
        ''' </summary>
        ''' <param name="e"></param>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Property Value(ByVal e As T) As U
            Get
                Return Values(e)
            End Get
            Set(ByVal value As U)
                Values(e) = value
            End Set
        End Property

#Region " IDisposable Support "
        Private disposedValue As Boolean = False        ' To detect redundant calls
        ' IDisposable
        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    ' TODO: free other state (managed objects).
                End If
                ' TODO: set large fields to null.
            End If
            Me.disposedValue = True
        End Sub
        ' This code added by Visual Basic to correctly implement the disposable pattern.
        Public Sub Dispose() Implements IDisposable.Dispose
            ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
#End Region

    End Class

    ''' <summary>
    ''' Festlegung des Farbsystem mit Zugriff auf die verschiedenen Farben
    ''' </summary>
    ''' <typeparam name="T">Eunummerierungs-Bezeichnungen des Farbssystem</typeparam>
    ''' <remarks></remarks>
    <Serializable()> Public MustInherit Class ColorSystemFactory(Of T)
        Implements IDisposable

        Private Values As Dictionary(Of T, System.Drawing.Color)

        Public Sub New()
            Values = New Dictionary(Of T, System.Drawing.Color)
        End Sub

        ''' <summary>
        ''' benannte Farbwerte für das Farbsystem
        ''' </summary>
        ''' <param name="e"></param>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Property Farbe(ByVal e As T) As System.Drawing.Color
            Get
                Return Values(e)
            End Get
            Set(ByVal value As System.Drawing.Color)
                Values(e) = value
            End Set
        End Property

        ''' <summary>
        ''' Ausgabe eines SolidBrush-Objekts des Farbwertes
        ''' </summary>
        ''' <param name="e"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Function GetSolidBrush(ByVal e As T) As Brush
            Return New SolidBrush(Me.Farbe(e))
        End Function

        ''' <summary>
        '''  Ausgabe eines Pen-Objekts des Farbwertes
        ''' </summary>
        ''' <param name="e"></param>
        ''' <param name="width"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Function GetPen(ByVal e As T, Optional ByVal width As Single = 1) As Pen
            Return New Pen(Me.Farbe(e), width)
        End Function

#Region " IDisposable Support "
        Private disposedValue As Boolean = False        ' To detect redundant calls
        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    Values = Nothing
                End If
                ' TODO: set large fields to null.
            End If
            Me.disposedValue = True
        End Sub
        ' This code added by Visual Basic to correctly implement the disposable pattern.
        Public Sub Dispose() Implements IDisposable.Dispose
            ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
#End Region

    End Class

#End Region

End Namespace

Namespace HTD.FarbSystem.Example

    ''' <summary>
    ''' Die Farbbezeichnungen für das verwendete Farbsystem 
    ''' </summary>
    ''' <remarks></remarks>
    Public Enum ColorMiniLayout
        BackGroundLight
        BackGround
        HeaderDark
        ForeGround
        Black
        SelectedBackColor
        SelectedForeColor
    End Enum

    ''' <summary>
    ''' Farbsystem Grau mit benannten Farben
    ''' </summary>
    ''' <remarks></remarks>
    Public Class MiniGrey
        Inherits ColorSystemFactory(Of ColorMiniLayout)

        Public Sub New()
            '   Farben festlegen
            MyBase.Farbe(ColorMiniLayout.BackGroundLight) = Color.FromArgb(255, 161, 161, 161)
            MyBase.Farbe(ColorMiniLayout.BackGround) = Color.FromArgb(255, 134, 134, 134)
            MyBase.Farbe(ColorMiniLayout.HeaderDark) = Color.FromArgb(255, 125, 125, 125)
            MyBase.Farbe(ColorMiniLayout.ForeGround) = Color.LightGray
            MyBase.Farbe(ColorMiniLayout.Black) = Color.Black
            MyBase.Farbe(ColorMiniLayout.SelectedForeColor) = Color.Yellow
            MyBase.Farbe(ColorMiniLayout.SelectedBackColor) = Color.FromArgb(255, 125, 125, 125)
        End Sub

    End Class

    ''' <summary>
    ''' Farbsystem Blau mit benannten Farben
    ''' </summary>
    ''' <remarks></remarks>
    Public Class MiniBlue
        Inherits ColorSystemFactory(Of ColorMiniLayout)

        Public Sub New()
            '   Farben festlegen
            MyBase.Farbe(ColorMiniLayout.BackGroundLight) = Color.LightBlue
            MyBase.Farbe(ColorMiniLayout.BackGround) = Color.DarkBlue
            MyBase.Farbe(ColorMiniLayout.HeaderDark) = Color.SteelBlue
            MyBase.Farbe(ColorMiniLayout.ForeGround) = Color.DarkGray
            MyBase.Farbe(ColorMiniLayout.Black) = Color.Black
            MyBase.Farbe(ColorMiniLayout.SelectedForeColor) = Color.Yellow
            MyBase.Farbe(ColorMiniLayout.SelectedBackColor) = Color.DarkOrange
        End Sub

    End Class


End Namespace

Abgelegt unter Farbsystem, Layoutgestaltung, Color.

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!