tech.guitarrapc.cóm

Technical updates

PowerShellで画面ロックのジョークファイル

PowerShellな小遊びです。

画面いっぱいに半透明なWPFを表示して画面をロックしたように見せかけてみます。 悪用……できないでしょうが、まぁほどほどに節度を…。

コード全文

Function Show-LockScreen{
  [CmdletBinding()]
  param(
    [int]$Sleep,
    [string]$Context="このロックスクリーンはPowerShellで描かれています! $sleep 秒待ってね♡",
    [string]$LabelColour = 'Indigo',
    [string]$BackgroundColour = "#88AA55",
    [ValidateScript({($_ -le 1.0) -and ($_ -gt 0)})]
    [double]$Opacity = 0.4
  )

  begin
  {
    try
    {
      $label = New-Object Windows.Controls.Label
      $label.Content = $Context
      $label.FontSize = 60
      $label.FontFamily = 'Consolas'
      $label.Background = 'Transparent'
      $label.Foreground = $LabelColour
      $label.HorizontalAlignment = 'Center'
      $label.VerticalAlignment = 'Center'
    }
    catch
    {
    }

    try
    {
      $window = New-Object Windows.Window
      $Window.AllowsTransparency = $True
      $Window.Opacity = $Opacity
      $window.WindowStyle = 'None'
      $window.Background = $BackgroundColour
      $window.Content = $label
      $window.Left = $window.Top = 0
      $window.WindowState = 'Maximized'
      $window.Topmost = $true
    }
    catch
    {
    }
  }

  process
  {
    $window.Show() > $null
    Start-Sleep -Seconds $Sleep
    $window.Close()
  }
}

実行

この一文で。-Sleepには何秒ロックして見せるかを設定します。-Contextとして表示する文字を渡します。

Show-LockScreen -Sleep 5 -Opacity 0.5 -LabelColour "Indigo" -BackgroundColour "SkyBlue"

PowerShellで簡単WPFサンプルでした。主要部分はこれだけです。

$label = New-Object Windows.Controls.Label
$label.Content = $Context
$label.FontSize = 60
$label.FontFamily = 'Consolas'
$label.Background = 'Transparent'
$label.Foreground = $LabelColour
$label.HorizontalAlignment = 'Center'
$label.VerticalAlignment = 'Center'

$window = New-Object Windows.Window
$Window.AllowsTransparency = $True
$Window.Opacity = $Opacity
$window.WindowStyle = 'None'
$window.Background = $BackgroundColour
$window.Content = $label
$window.Left = $window.Top = 0
$window.WindowState = 'Maximized'
$window.Topmost = $true

$window.Show() > $null
Start-Sleep -Seconds $Sleep
$window.Close()