[英]How to create a popup message in Powershell without buttons
I'm trying to create a message dialogue in Powershell where the user has no option to action on the message as that is the intention. 我正在尝试在Powershell中创建消息对话框,在该对话框中,用户没有选择要对消息进行操作的意图,因为这是故意的。 So the message will have the X button grayed along with the buttons (not showing buttons are even better). 因此,该消息会将X按钮和按钮一起显示为灰色(不显示按钮会更好)。
The closest I could reach was disabling the X via below code: 我能到达的最接近的位置是通过以下代码禁用X:
$wshell = New-Object -ComObject Wscript.Shell -ErrorAction Stop
$wshell.Popup("Aborted",0,"ERROR!",48+4)
But cannot figure out disabling button part. 但是无法弄清楚禁用按钮部分。 Below MS articles were of little help as well: 以下MS文章也无济于事:
http://blogs.technet.com/b/heyscriptingguy/archive/2006/07/27/how-can-i-display-a-message-box-that-has-no-buttons-and-that-disappears-after-a-specified-period-of-time.aspx http://blogs.technet.com/b/heyscriptingguy/archive/2006/07/27/how-can-i-display-a-message-box-that-has-no-buttons-and-that-disappears-后一个指定的周期为的-time.aspx
https://msdn.microsoft.com/en-us/library/x83z1d9f(v=vs.84).aspx https://msdn.microsoft.com/en-us/library/x83z1d9f(v=vs.84).aspx
Referred to few other articles over net some even suggesting custom made buttons using HTML, or VB library. 通过网络引用其他几篇文章,甚至建议使用HTML或VB库定制按钮。 But not what I was looking for. 但不是我想要的。
Any help/hint/suggestion would be deeply appreciated. 任何帮助/提示/建议将不胜感激。
Regards, Shakti 问候,Shakti
Dig into the .NET Windows.Forms namespace, you can make pretty much any kind of window you want with that: 钻入.NET Windows.Forms命名空间,您可以使用它来创建几乎任何类型的窗口:
https://msdn.microsoft.com/en-us/library/system.windows.forms.aspx https://msdn.microsoft.com/en-us/library/system.windows.forms.aspx
Here's a quick sample window w/ no buttons that can't be moved/closed by the user, but closes itself after 5 seconds: 这是一个快速的示例窗口,其中没有用户无法移动/关闭的按钮,但会在5秒后关闭:
Function Generate-Form {
Add-Type -AssemblyName System.Windows.Forms
# Build Form
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Test"
$objForm.Size = New-Object System.Drawing.Size(220,100)
# Add Label
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(80,20)
$objLabel.Size = New-Object System.Drawing.Size(100,20)
$objLabel.Text = "Hi there!"
$objForm.Controls.Add($objLabel)
# Show the form
$objForm.Show()| Out-Null
# wait 5 seconds
Start-Sleep -Seconds 5
# destroy form
$objForm.Close() | Out-Null
}
generate-form
Using the script above as a launching point I'm attempting to make a function that will allow me to popup a please wait message run some more script then close the popup 我正在尝试使用上面的脚本作为启动点,以使我能够弹出一个请稍等消息,再运行更多脚本然后关闭弹出窗口的功能
Function Popup-Message {
param ([switch]$show,[switch]$close)
Add-Type -AssemblyName System.Windows.Forms
# Build Form
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Test"
$objForm.Size = New-Object System.Drawing.Size(220,100)
# Add Label
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(80,20)
$objLabel.Size = New-Object System.Drawing.Size(100,20)
$objLabel.Text = "Hi there!"
$objForm.Controls.Add($objLabel)
If ($show)
{
$objForm.Show() | Out-Null
$global:test = "Show"
}
If ($close)
{
# destroy form
$objForm.Close() | Out-Null
$global:test = "Close"
}
}
I can then get the popup to display by: 然后,我可以通过以下方式显示弹出窗口:
Popup-Message -show
At this point I can see the $test variable as Show 此时,我可以看到$ test变量为Show
But when I try to close the window with: 但是当我尝试用以下方法关闭窗口时:
Popup-Message -close
But the popup window will not close If I look at $test again it will show as Close 但是弹出窗口不会关闭如果我再次查看$ test ,它将显示为“ 关闭”
I'm assuming this has something to do with keeping the function in the Global Scope but I can't figure out how to do this with the form 我假设这与将函数保留在Global Scope中有关,但是我不知道如何使用表单来执行此操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.