Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

WPF From PowerShell : Start-DrawMode

Another WPF from PowerShell post,

There is lot's of other interesting stuff in the new Windows PowerShell V2 CTP2   and you might find the recent WPF from PowerShell explosion a bit overwhelming and or overdone, so I was planning on some other topics, but could not resist to share this one, Start-DrawMode

A PowerShell script that inkes a transparant InkPanel that allows you to Draw on the Screen and can be used for examples in demos to point things out.

I got the idea after seeing the simple InkPanel example from James Brundage in his series about PowerShell and WPF and got it working in 12 lines of PowerShell code, after that I added some more code to be able to select some different pens and colors, the result of starting this PowerShell script is that you can draw on the screen to point something out or highlight some code :

image

when you start the script (press F5 to start the script in GPoSH) you can start to draw on the screen, until you press Space or Enter, a rightClick with the Mouse will clear the Screen, but there are also some Key-Shortcuts (List Below)

*Note* I got a mail from a reader where I got that Cool PowerShell IDE he did see in some of me recent posts.

This is Graphical PowerShell and in included in the CTP builds also, where I did find the version from the first CTP not so very useful, I worked with the new one for some time and actually like it 

It does replace notepad for me, but not yet the PowerShell console, I still find myself copying  the code from GPOSH into a Normal PowerShell console  mostly to be able to use PowerTab for listing method and properties.

I really can't miss PowerTab so this is one of the main reasons it not able to replace the console for me, some way to popup a list is very high on my Wish list for Graphical PowerShell.

PowerTab does not work at all the moment in Graphical PowerShell as it does $host.UI functions Graphical PowerShell does not have, even with $powertabconfig.DefaultHandler = 'Console'  It does not work yet, but this should be possible to fix so I will look into it, so we can at least use all extra completions scenario's PowerTab does add as completion on .NET Classes , WMI Classes etc.

But the Three pane editor Script in top frame, output in the Middle Frame, and a Command Prompt from Single Command (Red circled in picture above) works great, if not for PowerTab it could replace the Shell for a much greater part.

Be sure to check it out yourself !

But back to the Script, the Code Pointed out with the Blue arrow above is the Switch statement to Switch pen's, here I implemented the following keyboard Shortcuts ( see thick black line ):


"M" Select Yellow Marker

Pen size :

"F" Fine point
"N" Normal point
"T" Thick point

Pen Color :

"B" Black
"W" White
"R" Red

Other :

"C" {$inkCanvas.Strokes.Clear()}
"H" {$window.windowstate = 'Minimized'}
"Q" Close window

Any other key as those listed above will also Close the Script

And here is the Script in his current version :

# Start-DrawingMode.ps1
# requires -version 2
## requires STA Mode
# 
# Invokes WPF InkCanvas to Draw on the screen
#
# /\/\o\/\/ 2008
# Http://thePowerShellGuy.com

Add-Type -AssemblyName PresentationFramework
 
$window = New-Object Windows.Window
$window.WindowStyle = 'None'
$window.Title = "DRAWING"
$window.AllowsTransparency = $True
$window.Background="#01FFFFFF"
$window.Topmost = $true

$window.WindowState = 'Maximized'

$inkCanvas = New-Object Windows.Controls.InkCanvas
$inkCanvas.Background="Transparent"
#$inkCanvas.DefaultDrawingAttributes.FitToCurve = $true
$window.Content = $inkCanvas

function Set-PenColor ($Color) {
      $inkCanvas.DefaultDrawingAttributes.Color = $Color
}
function Set-PenSize ($Width,$height) {
      $inkCanvas.DefaultDrawingAttributes.Width = $Width
      $inkCanvas.DefaultDrawingAttributes.Height = $Height
}
function Set-Pen ($Color,$Width,$Height) {
      Set-PenColor $Color
      Set-PenSize $width $Height
}

Set-Pen Blue 5 5

$window.Add_KeyDown({
  switch ($_.key) {
    "Q" {$Window.close()}
    "M" {set-pen -Color '#90FFFF00' 5 15}
    "f" {Set-PenSize 2 2}
    "N" {Set-Pen 'Blue' 5 5}
    "T" {Set-PenSize 10 10}
    "B" {Set-PenColor Black}
    "W" {Set-PenColor White}
    "R" {Set-PenColor Red}
    "C" {$inkCanvas.Strokes.Clear()}
    "H" {$window.windowstate = 'Minimized'}
    Default {$Window.close()}
  }
})

$window.Add_MouseRightButtonUp({$inkCanvas.Strokes.Clear()})
 
$null = $window.ShowDialog() 

TIP : I created an alias for this in PowerShell so I can start it with a quick SD when needed :

image 

After Jaykul helped me out with a problem I had (The Ink was also transparent ), we started brainstorming and had a lot of cool ideas for extending this script, with color selectors etc.but for now I implemented just some basic key shortcuts.

But this could be continued ;-)

Enjoy,

Greetings /\/\o\/\/

Published Friday, May 30, 2008 5:13 PM by MoW
Filed under: ,

Comments

No Comments
Anonymous comments are disabled