Updated, Hippy Dippy PowerShell Clock with ShowUI

July 13, 2011

in Microsoft,PowerShell,ShowUI,WPF

image

I posted PowerShell Clock with ShowUI. Ravi, another PowerShell MVP, tried the sample using the AsJob parameter. The AsJob will run the PowerShell WPF GUI in the background. Unfortunately, the FontSize and other parameters would not set correctly and the clocked was not readable.

After emailing James Brundage, a fellow ShowUI author , he blogged Six Steps for Writing ShowUI Controls and a video tutorial based on this clock example.

Definitely check it out, he kicks it up a notch with data context’s, PowerShell DataSources. splatting, adding event handlers and more.

Here is my updated clock code

Run it with either –Show or –AsJob

HippyDippy-Clock -Show
HippyDippy-Clock -AsJob
function HippyDippy-Clock {
    param (
        [string]$Name,
        [Int]$Row,
        [Int]$Column,
        [Int]$RowSpan,
        [Int]$ColumnSpan,
        [Int]$Width,
        [Int]$Height,
        [Double]$Top,
        [Double]$Left,
        [Windows.Controls.Dock]$Dock,
        [Switch]$Show,
        [Switch]$AsJob
    )            

    begin {ipmo showui}
    process {
        if (-not $psBoundParameters.Background) {
            $psBoundParameters.Background = 'Transparent'
        }                        

        $uiParameters = @{} + $psBoundParameters            

        Border @uiParameters -On_Loaded {
            $this.Child = Grid -Columns 150, 300* {
                Image -Source "http://weaselzippers.us/wp-content/uploads/2011/05/hippie.jpg" -Column 0
                Label -Name Target -FontSize 90 -Column 1 `
                    -FontFamily "Impact, Arial" -FontWeight 800 -Foreground (
                    LinearGradientBrush $(
                        GradientStop -Color Red    -Offset 1
                        GradientStop -Color Orange -Offset 0.85
                        GradientStop -Color Yellow -Offset 0.7
                        GradientStop -Color Green  -Offset 0.55
                        GradientStop -Color Blue   -Offset 0.4
                        GradientStop -Color Indigo -Offset 0.2
                        GradientStop -Color Violet -Offset 0
                    )
                )
            }            

            $this.DataContext = Get-PowerShellDataSource -On_OutputChanged {
                $output = Get-PowerShellOutput -Last -OutputOnly
                $Target.Content = $output
            } -Script {
                while ($true) {
                    (Get-Date).ToString('T'); Start-Sleep -Seconds 1
    }
            }                

        } -On_Initialized {
            $window.SizeToContent         = 'WidthAndHeight'
            $window.WindowStyle           = 'None'
            $window.Background            = 'Transparent'
            $window.AllowsTransparency    = $true
            $window.WindowStartupLocation = 'CenterScreen'            

            Add-EventHandler -EventName "On_Closing" -Handler {
                if ($this.Content.DataContext.Command.Stop) {
                        $this.Content.DataContext.Command.Stop()
                    }
                } -Object $window                                    

                # When the right mouse button is down, close the control            
                Add-EventHandler -EventName "On_PreviewMouseRightButtonDown" -Handler {
                    $_.Handled = $true
                    Close-Control
                } -Object $window            

                Add-EventHandler -EventName "On_PreviewMouseLeftButtonDown" -Handler {
                    $_.Handled = $true
                    $this.DragMove()
                } -Object $window
        }
    }
}

{ 1 trackback }

The Obligatory ShowUI Clock - Huddled Masses
07.19.11 at 12:52 am

{ 0 comments… add one now }

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Contrat Creative Commons

© 2007-2012, Doug Finke