PoshCode Logo PowerShell Code Repository

WPF/WInForm shortcuts by mario 3 weeks ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/6830"></script>download | new post

Shortcut functions for instantiating WPF or WinForm widgets

  1. # api: ps
  2. # title: WPF + WinForms
  3. # description: WinForm and WPF shortcuts
  4. # depends: sys:presentationframework, sys:system.windows.forms, sys:system.drawing
  5. # version: 0.9
  6. # type: functions
  7. # category: ui
  8. # config: -
  9. # status: beta
  10. # license: PD
  11. # priority: default
  12. #
  13. # Simple aliases to `New-Object System.Windows.Controls.Widget`, which
  14. # ease parameter definition + method invocation using a hashtable arg:
  15. #
  16. #    $w_btn = W Button @{Content="Text"; Border="2"; add_Click=$cb}
  17. #    $w_grd = W Grid @{Add=$w1, $w2, $w3}
  18. #
  19. # Or augment existing form objects:
  20. #
  21. #    W $w_btn @{Background="Black"; Foreground="White"}
  22. #
  23. # Perhaps even nesting if you can stand the parentheses/curly braces:
  24. #
  25. #    $form = W Window @{Width=800; Height=600; Add=@(
  26. #        (W StackPanel @{Orientation="Vertical"; Add=@(
  27. #            (W Label @{Content="title"; Foreground="Blue"}),
  28. #            (W Button @{Content="Click me"; Background="Red"})
  29. #        )})
  30. #    )}
  31. #
  32. # The `W` shortcut is for WPF, whereas `WF` summons WinForms widgets.
  33. # While `WD` is an alias to System.Windows.Document.xyz elements.
  34.  
  35.  
  36. #-- register libs
  37. Add-Type -AN PresentationCore, PresentationFramework, WindowsBase
  38. Add-Type -AN System.Drawing, System.Windows.Forms, Microsoft.VisualBasic
  39.  
  40.  
  41. #-- WPF/WinForms widget wrapper
  42. function W {
  43.     [CmdletBinding()]
  44.     Param($type = "Button", $prop = @{}, $Base="Controls")
  45.  
  46.     # new object
  47.     if ($type.getType().Name -eq "String") {
  48.         $w = New-Object System.Windows.$Base.$type
  49.     }
  50.     else {
  51.         $w = $type
  52.     }
  53.  
  54.     # apply options+methods
  55.     $prop.keys | % {
  56.         $key = $_
  57.         $val = $prop[$_]
  58.         if ($pt = ($w | Get-Member -Force -Name $key)) { $pt = $pt.MemberType }
  59.  
  60.         # properties
  61.         if ($pt -eq "Property") {
  62.             if (($Base -eq "Forms") -and (@("Size" , "ItemSize", "Location") -contains $key)) {
  63.                 $val = New-Object System.Drawing.Size($val[0], $val[1])
  64.             }
  65.             $w.$key = $val
  66.         }
  67.         # check for methods in widget and common subcontainers
  68.         else {
  69.             ForEach ($obj in @($w, $w.Children, $w.Child, $w.Container)) {
  70.                 if ($obj.psobject -and $obj.psobject.methods.match($key) -and $obj.$key) {
  71.                     ([array]$val) | % { $obj.$key.Invoke($_) } | Out-Null
  72.                     break
  73.                 }
  74.             }
  75.         }
  76.     }
  77.     return $w
  78. }
  79.  
  80. #-- WinForms version
  81. function WF {
  82.     Param($type, $prop, $add=@{}, $click=$null)
  83.     W -Base Forms -Type $type -Prop ($prop + @{add=$add; add_click=$click})
  84. }
  85.  
  86. #-- Document "widgets"
  87. function WD {
  88.     Param($type, $prop=@{})
  89.     W -Base Documents -Type $type -Prop $prop
  90. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me