Click here to Skip to main content
15,894,907 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.Windows
Imports System.Windows.Forms
Imports System.Windows.Media
Imports System.Windows.Media.Imaging

Public Class VisualStyleWindowElement
    Inherits FrameworkElement
    Implements IDisposable

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Private HeavyBitmap As System.Drawing.Bitmap
    Private HeavyGraphics As System.Drawing.Graphics

    Protected Overrides Sub OnRender(drawingContext As Media.DrawingContext)
        If ActualWidth < 1 Or ActualHeight < 1 Then Return
        SetBitmapSize()
        HeavyGraphics.Clear(System.Drawing.Color.Transparent)
        If VisualStyles.VisualStyleInformation.IsEnabledByUser Then
            'If False Then
            Dim vse As VisualStyles.VisualStyleRenderer = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Window.Caption.Active)
            Select Case Element
                Case VisualStylePiece.WindowFrameTop
                    If IsActive Then
                        vse = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Window.Caption.Active)
                    Else
                        vse = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Window.Caption.Inactive)
                    End If
                Case VisualStylePiece.WindowFrameLeft
                    If IsActive Then
                        vse = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Window.FrameLeft.Active)
                    Else
                        vse = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Window.FrameLeft.Inactive)
                    End If
                Case VisualStylePiece.WindowFrameRight
                    If IsActive Then
                        vse = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Window.FrameRight.Active)
                    Else
                        vse = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Window.FrameRight.Inactive)
                    End If
                Case VisualStylePiece.WindowFrameBottom
                    If IsActive Then
                        vse = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Window.FrameBottom.Active)
                    Else
                        vse = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Window.FrameBottom.Inactive)
                    End If
                Case VisualStylePiece.CloseButton
                    If IsActive Then
                        Select Case VisualState
                            Case ButtonVisualState.Hot
                                vse = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Window.CloseButton.Hot)
                            Case ButtonVisualState.Pressed
                                vse = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Window.CloseButton.Pressed)
                            Case Else
                                vse = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Window.CloseButton.Normal)
                        End Select
                    Else
                        vse = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Window.MaxButton.Disabled)
                    End If
                Case VisualStylePiece.MaximizeButton
                    If IsActive Then
                        Select Case VisualState
                            Case ButtonVisualState.Hot
                                vse = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Window.MaxButton.Hot)
                            Case ButtonVisualState.Pressed
                                vse = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Window.MaxButton.Pressed)
                            Case Else
                                vse = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Window.MaxButton.Normal)
                        End Select
                    Else
                        vse = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Window.MaxButton.Disabled)
                    End If
                Case VisualStylePiece.RestoreButton
                    If IsActive Then
                        Select Case VisualState
                            Case ButtonVisualState.Hot
                                vse = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Window.RestoreButton.Hot)
                            Case ButtonVisualState.Pressed
                                vse = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Window.RestoreButton.Pressed)
                            Case Else
                                vse = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Window.RestoreButton.Normal)
                        End Select
                    Else
                        vse = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Window.MaxButton.Disabled)
                    End If
                Case VisualStylePiece.MinimizeButton
                    If IsActive Then
                        Select Case VisualState
                            Case ButtonVisualState.Hot
                                vse = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Window.MinButton.Hot)
                            Case ButtonVisualState.Pressed
                                vse = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Window.MinButton.Pressed)
                            Case Else
                                vse = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Window.MinButton.Normal)
                        End Select
                    Else
                        vse = New VisualStyles.VisualStyleRenderer(VisualStyles.VisualStyleElement.Window.MaxButton.Disabled)
                    End If
            End Select
            HeavyGraphics.SetClip(vse.GetBackgroundRegion(HeavyGraphics, New System.Drawing.Rectangle(0, 0, ActualWidth, ActualHeight)), System.Drawing.Drawing2D.CombineMode.Replace)
            vse.DrawBackground(HeavyGraphics, New System.Drawing.Rectangle(0, 0, ActualWidth, ActualHeight))
        Else
            If Element = VisualStylePiece.CloseButton Then
                If IsActive Then
                    If VisualState = ButtonVisualState.Pressed Then
                        System.Windows.Forms.ControlPaint.DrawCaptionButton(HeavyGraphics, New System.Drawing.Rectangle(0, 0, ActualWidth, ActualHeight), CaptionButton.Close, ButtonState.Pushed)
                    Else
                        System.Windows.Forms.ControlPaint.DrawCaptionButton(HeavyGraphics, New System.Drawing.Rectangle(0, 0, ActualWidth, ActualHeight), CaptionButton.Close, ButtonState.Normal)
                    End If
                Else
                    System.Windows.Forms.ControlPaint.DrawCaptionButton(HeavyGraphics, New System.Drawing.Rectangle(0, 0, ActualWidth, ActualHeight), CaptionButton.Close, ButtonState.Inactive)
                End If
            ElseIf Element = VisualStylePiece.MaximizeButton Then
                If IsActive Then
                    If VisualState = ButtonVisualState.Pressed Then
                        System.Windows.Forms.ControlPaint.DrawCaptionButton(HeavyGraphics, New System.Drawing.Rectangle(0, 0, ActualWidth, ActualHeight), CaptionButton.Maximize, ButtonState.Pushed)
                    Else
                        System.Windows.Forms.ControlPaint.DrawCaptionButton(HeavyGraphics, New System.Drawing.Rectangle(0, 0, ActualWidth, ActualHeight), CaptionButton.Maximize, ButtonState.Normal)
                    End If
                Else
                    System.Windows.Forms.ControlPaint.DrawCaptionButton(HeavyGraphics, New System.Drawing.Rectangle(0, 0, ActualWidth, ActualHeight), CaptionButton.Maximize, ButtonState.Inactive)
                End If
            ElseIf Element = VisualStylePiece.RestoreButton Then
                If IsActive Then
                    If VisualState = ButtonVisualState.Pressed Then
                        System.Windows.Forms.ControlPaint.DrawCaptionButton(HeavyGraphics, New System.Drawing.Rectangle(0, 0, ActualWidth, ActualHeight), CaptionButton.Restore, ButtonState.Pushed)
                    Else
                        System.Windows.Forms.ControlPaint.DrawCaptionButton(HeavyGraphics, New System.Drawing.Rectangle(0, 0, ActualWidth, ActualHeight), CaptionButton.Restore, ButtonState.Normal)
                    End If
                Else
                    System.Windows.Forms.ControlPaint.DrawCaptionButton(HeavyGraphics, New System.Drawing.Rectangle(0, 0, ActualWidth, ActualHeight), CaptionButton.Restore, ButtonState.Inactive)
                End If
            ElseIf Element = VisualStylePiece.MinimizeButton Then
                If IsActive Then
                    If VisualState = ButtonVisualState.Pressed Then
                        System.Windows.Forms.ControlPaint.DrawCaptionButton(HeavyGraphics, New System.Drawing.Rectangle(0, 0, ActualWidth, ActualHeight), CaptionButton.Minimize, ButtonState.Pushed)
                    Else
                        System.Windows.Forms.ControlPaint.DrawCaptionButton(HeavyGraphics, New System.Drawing.Rectangle(0, 0, ActualWidth, ActualHeight), CaptionButton.Minimize, ButtonState.Normal)
                    End If
                Else
                    System.Windows.Forms.ControlPaint.DrawCaptionButton(HeavyGraphics, New System.Drawing.Rectangle(0, 0, ActualWidth, ActualHeight), CaptionButton.Minimize, ButtonState.Inactive)
                End If
            ElseIf Element = VisualStylePiece.WindowFrameTop Then
                ControlPaint.DrawBorder3D(HeavyGraphics, New System.Drawing.Rectangle(0, 0, ActualWidth, ActualHeight + 2), Border3DStyle.Raised)
                If IsActive Then
                    HeavyGraphics.FillRectangle(System.Drawing.SystemBrushes.Control, 2, 2, CType(ActualWidth, Integer) - 4, CType(ActualHeight, Integer) - 2)
                    HeavyGraphics.FillRectangle(New System.Drawing.Drawing2D.LinearGradientBrush(New System.Drawing.Rectangle(4, 4, CType(ActualWidth, Integer) - 8, CType(ActualHeight, Integer) - 4), System.Drawing.SystemColors.ActiveCaption, System.Drawing.SystemColors.GradientActiveCaption, 0), New System.Drawing.Rectangle(4, 4, CType(ActualWidth, Integer) - 8, CType(ActualHeight, Integer) - 4))
                Else
                    HeavyGraphics.FillRectangle(System.Drawing.SystemBrushes.Control, 2, 2, CType(ActualWidth, Integer) - 4, CType(ActualHeight, Integer) - 2)
                    HeavyGraphics.FillRectangle(New System.Drawing.Drawing2D.LinearGradientBrush(New System.Drawing.Rectangle(4, 4, CType(ActualWidth, Integer) - 8, CType(ActualHeight, Integer) - 4), System.Drawing.SystemColors.InactiveCaption, System.Drawing.SystemColors.GradientInactiveCaption, 0), New System.Drawing.Rectangle(4, 4, CType(ActualWidth, Integer) - 8, CType(ActualHeight, Integer) - 4))
                End If
            ElseIf Element = VisualStylePiece.WindowFrameBottom Then
                ControlPaint.DrawBorder3D(HeavyGraphics, New System.Drawing.Rectangle(0, -2, ActualWidth, ActualHeight + 2), Border3DStyle.Raised)
                HeavyGraphics.FillRectangle(System.Drawing.SystemBrushes.Control, 2, 0, CType(ActualWidth, Integer) - 4, CType(ActualHeight, Integer) - 2)
            ElseIf Element = VisualStylePiece.WindowFrameLeft Then
                ControlPaint.DrawBorder3D(HeavyGraphics, New System.Drawing.Rectangle(0, -2, ActualWidth + 2, ActualHeight + 4), Border3DStyle.Raised)
                HeavyGraphics.FillRectangle(System.Drawing.SystemBrushes.Control, 2, 0, CType(ActualWidth, Integer) - 2, CType(ActualHeight, Integer))
            ElseIf Element = VisualStylePiece.WindowFrameRight Then
                ControlPaint.DrawBorder3D(HeavyGraphics, New System.Drawing.Rectangle(-2, -2, ActualWidth + 2, ActualHeight + 4), Border3DStyle.Raised)
                HeavyGraphics.FillRectangle(System.Drawing.SystemBrushes.Control, 0, 0, CType(ActualWidth, Integer) - 2, CType(ActualHeight, Integer))
            End If
        End If
        drawingContext.DrawImage(Interop.Imaging.CreateBitmapSourceFromHBitmap(HeavyBitmap.GetHbitmap(), IntPtr.Zero, System.Windows.Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()), New Rect(0, 0, ActualWidth, ActualHeight))
    End Sub


    Private Sub SetBitmapSize()
        If IsNothing(HeavyBitmap) Then
            CreateBitmap()
        Else
            If Not (New Size(HeavyBitmap.Width, HeavyBitmap.Height) = New Size(ActualWidth, ActualHeight)) Then
                CreateBitmap()
            End If
        End If
    End Sub

    Private Sub CreateBitmap()
        If Not IsNothing(HeavyBitmap) Then HeavyBitmap.Dispose()
        If Not IsNothing(HeavyGraphics) Then HeavyGraphics.Dispose()
        HeavyBitmap = New System.Drawing.Bitmap(CType(ActualWidth, Integer), CType(ActualHeight, Integer))
        HeavyGraphics = System.Drawing.Graphics.FromImage(HeavyBitmap)
    End Sub

    Public Shared ElementProperty As DependencyProperty = DependencyProperty.Register("Element", GetType(VisualStylePiece), GetType(VisualStyleWindowElement), New FrameworkPropertyMetadata(VisualStylePiece.WindowFrameTop, FrameworkPropertyMetadataOptions.AffectsRender))
    Public Shared IsActiveProperty As DependencyProperty = DependencyProperty.Register("IsActive", GetType(Boolean), GetType(VisualStyleWindowElement), New FrameworkPropertyMetadata(True, FrameworkPropertyMetadataOptions.AffectsRender))
    Public Shared VisualStateProperty As DependencyProperty = DependencyProperty.Register("VisualState", GetType(ButtonVisualState), GetType(VisualStyleWindowElement), New FrameworkPropertyMetadata(ButtonVisualState.Normal, FrameworkPropertyMetadataOptions.AffectsRender))

    Public Property Element As VisualStylePiece
        Get
            Return GetValue(ElementProperty)
        End Get
        Set(value As VisualStylePiece)
            SetValue(ElementProperty, value)
        End Set
    End Property

    Public Property IsActive As Boolean
        Get
            Return GetValue(IsActiveProperty)
        End Get
        Set(value As Boolean)
            SetValue(IsActiveProperty, value)
        End Set
    End Property

    Public Property VisualState As ButtonVisualState
        Get
            Return GetValue(VisualStateProperty)
        End Get
        Set(value As ButtonVisualState)
            SetValue(VisualStateProperty, value)
        End Set
    End Property

#Region "IDisposable Support"
    Private disposedValue As Boolean ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                HeavyBitmap.Dispose()
                HeavyGraphics.Dispose()
            End If
            ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
            ' TODO: set large fields to null.
        End If
        Me.disposedValue = True
    End Sub

    ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
    'Protected Overrides Sub Finalize()
    '    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
    '    Dispose(False)
    '    MyBase.Finalize()
    '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(disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class

Public Enum ButtonVisualState As Byte
    Normal
    Hot
    Pressed
    Active
End Enum

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