Click here to Skip to main content
15,891,253 members
Articles / Desktop Programming / WPF

Introducing PresentationWindows

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
4 Nov 2012CPOL6 min read 18K   564   19  
PresentationWindows is a WPF class library that introduces three new types derived from the Window class, enabling many previously difficult to access features of windows.
Imports System
Imports System.Windows
Imports System.Windows.Converters
Imports System.ComponentModel
Imports System.Text

<TypeConverter(GetType(WindowFrameThicknessConverter))>
Public Structure WindowFrameThickness
#Region "Constructors"
    Public Sub New(leftFrame As Int32?, topFrame As Int32?, rightFrame As Int32?, bottomFrame As Int32?)
        If leftFrame = -1 Or topFrame = -1 Or rightFrame = -1 Or bottomFrame = -1 Then
            If Not (leftFrame = -1 And topFrame = -1 And rightFrame = -1 And bottomFrame = -1) Then Throw New ArgumentException("The parameters for a WindowFrameThickness instance must not be negative unless they are all set to -1.")
        End If
        _TopFrame = topFrame
        _LeftFrame = leftFrame
        _BottomFrame = bottomFrame
        _RightFrame = rightFrame
    End Sub
#End Region

#Region "Properties"
    Public Shared ReadOnly Full As WindowFrameThickness = New WindowFrameThickness(-1, -1, -1, -1)

    Private _TopFrame As Double?
    Private _LeftFrame As Double?
    Private _BottomFrame As Double?
    Private _RightFrame As Double?

    Public ReadOnly Property TopFrame As Double?
        Get
            Return _TopFrame
        End Get
    End Property

    Public ReadOnly Property LeftFrame As Double?
        Get
            Return _LeftFrame
        End Get
    End Property

    Public ReadOnly Property BottomFrame As Double?
        Get
            Return _BottomFrame
        End Get
    End Property

    Public ReadOnly Property RightFrame As Double?
        Get
            Return _RightFrame
        End Get
    End Property
#End Region

#Region "Methods"
    Public Overrides Function ToString() As String
        Dim result As StringBuilder = New StringBuilder()
        If IsNothing(LeftFrame) Then
            result.Append("*")
        Else
            result.Append(LeftFrame.ToString())
        End If
        result.Append(", ")
        If IsNothing(TopFrame) Then
            result.Append("*")
        Else
            result.Append(TopFrame.ToString())
        End If
        result.Append(", ")
        If IsNothing(RightFrame) Then
            result.Append("*")
        Else
            result.Append(RightFrame.ToString())
        End If
        result.Append(", ")
        If IsNothing(BottomFrame) Then
            result.Append("*")
        Else
            result.Append(BottomFrame.ToString())
        End If
        Return result.ToString()
    End Function

    Public Shared Function Parse(str As String) As WindowFrameThickness
        If str = "Full" Then Return Full
        Dim parameters As List(Of Double?) = New List(Of Double?)
        Dim currentItem As String = ""
        Dim currentIndex As Byte = 0
        str = str.Replace(" ", "")
        For Each chr As Char In str
            If chr = "," Then
                If (currentItem = "*") Then
                    parameters.Add(Nothing)
                Else
                    parameters.Add(Double.Parse(currentItem))
                End If
                currentItem = ""
                currentIndex += 1
            Else
                currentItem &= chr
            End If
        Next
        If (currentItem = "*") Then
            parameters.Add(Nothing)
        Else
            parameters.Add(Double.Parse(currentItem))
        End If
        If parameters.Count = 1 Then
            Return New WindowFrameThickness(parameters(0), parameters(0), parameters(0), parameters(0))
        ElseIf parameters.Count = 4 Then
            Return New WindowFrameThickness(parameters(0), parameters(1), parameters(2), parameters(3))
        End If
    End Function

    Public Overrides Function GetHashCode() As Integer
        Dim result As Integer
        If IsNothing(LeftFrame) Then
            result += Integer.MaxValue / 8
        Else
            result += LeftFrame / 4
        End If

        If IsNothing(RightFrame) Then
            result += Integer.MaxValue / 8
        Else
            result += RightFrame / 4
        End If

        If IsNothing(TopFrame) Then
            result += Integer.MaxValue / 8
        Else
            result += TopFrame / 4
        End If

        If IsNothing(BottomFrame) Then
            result += Integer.MaxValue / 8
        Else
            result += BottomFrame / 4
        End If
        Return result
    End Function

    Public Overrides Function Equals(obj As Object) As Boolean
        If IsNothing(obj) Then Return False
        If Not TypeOf (obj) Is WindowFrameThickness Then Return False
        Dim typedObj As WindowFrameThickness = obj
        Return StaticMethods.EqualsOperatorNothingSafe(typedObj.LeftFrame, Me.LeftFrame) And StaticMethods.EqualsOperatorNothingSafe(typedObj.TopFrame, Me.TopFrame) And StaticMethods.EqualsOperatorNothingSafe(typedObj.RightFrame, Me.RightFrame) And StaticMethods.EqualsOperatorNothingSafe(typedObj.BottomFrame, Me.BottomFrame)
    End Function
#End Region

#Region "Operators"
    Public Shared ReadOnly DefaultHeight As CaptionHeight = New CaptionHeight(Double.MaxValue)

    Public Shared Operator =(left As WindowFrameThickness, right As WindowFrameThickness) As Boolean
        Return left.Equals(right)
    End Operator

    Public Shared Operator <>(left As WindowFrameThickness, right As WindowFrameThickness) As Boolean
        Return Not left.Equals(right)
    End Operator
#End Region
End Structure

Public Class WindowFrameThicknessConverter
    Inherits TypeConverter

    Public Overrides Function CanConvertFrom(context As ITypeDescriptorContext, sourceType As Type) As Boolean
        Return (sourceType.Equals(GetType(String)) Or sourceType.Equals(GetType(Thickness)))
    End Function

    Public Overrides Function CanConvertTo(context As ITypeDescriptorContext, destinationType As Type) As Boolean
        Return (destinationType.Equals(GetType(String)) Or destinationType.Equals(GetType(Thickness)))
    End Function

    Public Overrides Function ConvertFrom(context As ITypeDescriptorContext, culture As Globalization.CultureInfo, value As Object) As Object
        If (TypeOf (value) Is String) Then
            Return WindowFrameThickness.Parse(value)
        ElseIf (TypeOf (value) Is Thickness) Then
            Dim typedValue As Thickness = value
            Return New WindowFrameThickness(typedValue.Left, typedValue.Top, typedValue.Right, typedValue.Bottom)
        End If
        Return Nothing
    End Function

    Public Overrides Function ConvertTo(context As ITypeDescriptorContext, culture As Globalization.CultureInfo, value As Object, destinationType As Type) As Object
        If destinationType.Equals(GetType(String)) Then
            Return value.ToString()
        ElseIf destinationType.Equals(GetType(Thickness)) Then
            Dim typedValue As WindowFrameThickness = value
            Return New Thickness(typedValue.LeftFrame.Value, typedValue.TopFrame.Value, typedValue.RightFrame.Value, typedValue.BottomFrame.Value)
        End If
        Return Nothing
    End Function

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions