欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PowerShell中调用WPF生成炫酷窗口实例

程序员文章站 2022-06-24 11:01:02
怎样在powershell中调用wpf,你知道,我也知道;怎样在powershell中将很长的.net类型名称缩短成别名,你知道,我也知道。但是怎样将这两个知识点融汇贯通,...

怎样在powershell中调用wpf,你知道,我也知道;怎样在powershell中将很长的.net类型名称缩短成别名,你知道,我也知道。但是怎样将这两个知识点融汇贯通,写出一个优雅的demo,并且让你一眼就能看出,这就是wpf,不是别的,也许你以前就知道,而我直到今天才知道,有种相见恨晚的感觉。

先看一下炫酷的效果吧!

PowerShell中调用WPF生成炫酷窗口实例

powershell之wpf炫酷

# plik: 4_demo_v3_reflection.ps1
#requires -version 3
 
$akceleratory =
  [psobject].
  assembly.
  gettype("system.management.automation.typeaccelerators")
 
add-type -assemblyname presentationcore, presentationframework -passthru |
  where-object ispublic |
  foreach-object {
    $class = $_
    try {
      $akceleratory::add($class.name,$class)
    } catch {
      "failed to add $($class.name) accelerator pointing to $($class.fullname)"
    }
  }
 
[window]@{
  opacitymask = [drawingbrush]@{
    drawing = [drawinggroup]@{
      children = & {
        $kolekcja = new-object drawingcollection
        $kolekcja.add([geometrydrawing]@{
          brush = 'black'
          geometry = [ellipsegeometry]@{
            radiusx = 0.48
            radiusy = 0.48
            center = '0.5,0.5'
          }
        })
        $kolekcja.add([geometrydrawing]@{
          brush = 'transparent'
          geometry = [rectanglegeometry]@{
            rect = '0,0,1,1'
          }
        })
        , $kolekcja
      }
    }
  }
  background = [lineargradientbrush]@{
    opacity = 0.5
    startpoint = '0,0.5'
    endpoint = '1,0.5'
    gradientstops = & {
      $stopki = new-object gradientstopcollection
      $colors = 'blue', 'green'
        foreach ($i in 0..1) {
        $stopki.add(
          [gradientstop]@{
            color = $colors[$i]
            offset = $i
          }
        )
      }
      , $stopki
    }      
  }
  width = 800
  height = 400
  windowstyle = 'none'
  allowstransparency = $true
  effect = [dropshadoweffect]@{
    blurradius = 10
  }
  topmost = $true
  content = & {
    $stos = [stackpanel]@{
      verticalalignment = 'center'
      horizontalalignment = 'center'
    }
 
    $stos.addchild(
      [label]@{
        content = 'powershell rocks!'
        fontsize = 80
        fontfamily = 'consolas'
        foreground = 'white'
        effect = [dropshadoweffect]@{
          blurradius = 5
        }
      }
    )
    , $stos
  }
} | foreach-object {
  $_.add_mouseleftbuttondown({
    $this.dragmove()
  })
  $_.add_mouserightbuttondown({
    $this.close()
  })
  $_.showdialog() | out-null
}