当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell Magic 8 球

作者:精品下载站 日期:2024-12-14 07:55:41 浏览:15 分类:玩电脑

PowerShell Magic 8 球


[我更新了本文,以反映代码中的细微更改以及 PowerShell 7 的发布。本文最初发布于 2019 年 10 月 28 日]。

去年的这个时候,我在 Twitter 上分享了一些 PowerShell 代码。我有一个简短的脚本,它使用 Windows Presentation Foundation (WPF) 创建一个令人毛骨悚然的图形提示,允许您询问 Magic 8 Ball 的问题(该名称可能已注册商标或以其他方式受到保护)。对于那些可能不熟悉的人来说,这是一个形状像台球游戏中的巨型 8 号球的玩具。你大声问一个问题,摇动球,然后看底部以获得答案。我用 PowerShell 做了同样的事情。

[玩转系统] PowerShell Magic 8 球

[玩转系统] PowerShell Magic 8 球

无非是一点无害的乐趣。尽管代码演示了组合一个简单的 WPF 表单是多么容易。该脚本还将在 Windows 平台上的 PowerShell Core 中运行

您可以在我的 GitHub 帐户上找到代码作为要点。

MagicOracle.ps1:

#requires -version 5.1
# Usage: .\MagicOracle.ps1

Try {
    #try to load the WPF assemblies
    Add-Type -AssemblyName PresentationFramework -ErrorAction Stop
    Add-Type -AssemblyName PresentationCore -ErrorAction Stop
    Add-Type -AssemblyName WindowsBase -ErrorAction Stop
}
Catch {
    Write-Warning "Failed to load required WPF assemblies. This script requires Windows PowerShell, or PowerShell 7.x  on a Windows platform."
    #bail out
    return
}


$answers = @(
    "It is hard to say"
    "No"
    "Maybe"
    "Yes"
    "Absolutely"
    "Try again"
    "Rejoice"
    "Be very afraid"
    "Don't give up"
    "Only you can know your soul"
    "What have you got to lose?"
    "Unknown"
    "The answer is murky"
    "Give up"
    "Beware"
    "Winter is coming"
    "Only you can answer that"
    "Sleep on it"
    "When you are ready"
    "An upgrade is required"
    "Please reboot"
    "Why not?"
    "Roll the dice"
    "Be the Master"
    "No one said life is fair"
    "Prepare"
    "A shadow hangs over you"
    "Only Jeffrey Snover knows"
    "It is so"
    "You must be kidding"
    "There is no good answer"
    "There is no wrong answer"
    "Seek help from others"
    "This is nonsense"
    "Phone home"
    "Danger, Will Robinson!"
    "Perhaps"
    "In your dreams"
    "Focus"
    "Full speed ahead"
    "Who are you kidding?"
    "Dream On"
    "YOLO"
    "You can only try"
    "Failure is always an option"
    "One can hope"
    "Try another day"
    "It is written"
    "You fool"
    "Truly"
    "Danger ahead"
    "Kismet"
    "Be wise"
    "Be bold"
    "You make your own luck"
    "Do you feel lucky?"
    "Look for the answer within"
    "Fortune favors the brave"
    "Karma is kind to you"
    "There is no answer"
    "There is no spoon"
    "I've got a bad feeling about this"
    "Highly illogical"
    "I'm a magic 8 ball, not a miracle worker"
    "More information is required"
    "Figure it out for yourself"
    "If you work hard"
    "In your dreams"
    "This is the way"
    "It is possible"
    "That's just crazy talk"
    "May the Force be with you"
    "Not the way you imagine"
)

Try {
    #create the container form
    $form = New-Object System.Windows.Window -ErrorAction stop
}
Catch {
    #it is possible to load the WPF assemblies but still not be able to create a form
    #this should probably never happen - but just in case.
    Write-Warning "Failed to create the form. $($_.Exception.message)"
    return
}

#define what it looks like
$form.Title = "Ask the Magic 8 Oracle"
$form.Height = 175
$form.Width = 400
$form.FontFamily = "Chiller"
$form.FontSize = 24
$form.Background = "orange"
$form.add_Loaded({ $textbox.focus() })

#create the stackpanel
$stack = New-Object System.Windows.Controls.StackPanel

#create a label
$label = New-Object System.Windows.Controls.Label
$label.HorizontalAlignment = "Left"
$label.Content = "Ask a simple Yes/No or True/False question"
#add to the stack
$stack.AddChild($label)

$TextBox = New-Object System.Windows.Controls.TextBox
$TextBox.Width = $form.Width - 50

$TextBox.HorizontalAlignment = "Center"
$TextBox.Add_TextChanged( { $btn.IsEnabled = $True })

$stack.AddChild($textbox)

$btn = New-Object System.Windows.Controls.Button
$btn.Content = "_Ask for guidance"
$btn.Width = 150
$btn.HorizontalAlignment = "Center"
$btn.IsEnabled = $False

#define an action scriptblock to run when OK is clicked
$OK = {
    if ($btn.Content -match "guidance") {
        $answer.content = $answers | Get-Random -Count 1
        $btn.content = "_Ask another question"
        $textbox.IsEnabled = $False
    }
    else {
        $answer.Content = ""
        $TextBox.Text = ""
        $textbox.focus()
        $textbox.IsEnabled = $True
        $btn.content = "_Ask for guidance"
    }
}
#add the event handler
$btn.Add_click($OK)

#add the button to the stack
$stack.AddChild($btn)

#create the answer elements
$answer = New-Object System.Windows.Controls.Label
$answer.HorizontalAlignment = "center"
$answer.Width = $form.width - 50
$answer.Height = 100

#add the answer to the stack
$stack.AddChild($answer)

#add the stack to the form
$form.AddChild($stack)

#display the form
[void]$form.showdialog()

不给糖就捣蛋!

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯