如何确保Vbscript在使用弹出窗口时保持焦点



我一直在尝试实现一个未修改的Microsoft脚本,但没有成功,我想在我打算处理的脚本中使用它-(参见https://learn.microsoft.com/en-us/previous-versions/tn-archive/ee156593(v=technet.10)?redirectedfrom=MSDN参考=清单3.32显示定时进度消息框)。然而,当我完全按照页面上呈现的方式运行该脚本时,第一个弹出消息显示OK,然后根据需要消失,但随后的两个消息,虽然它们是生成的OK,但它们不会出现在最前线,而是留在当时页面上的任何其他内容的背后。清单3.32显示定时进度消息:

your text```Const TIMEOUT = 5你的文本Set objShell = WScript.CreateObject("WScript.Shell")你的文本objShell.Popup "Disk Report Complete", TIMEOUT你的文本objShell.Popup "Memory Report Complete", TIMEOUT你的文本' ' objShell。弹出"CPU报告完成",TIMEOUT

有人可以运行这个相同的脚本离开页面,看看他们是否得到与我一样的结果。我试过使用vbSystemModal,但它似乎不合适。论坛发帖对我没有帮助。如果有人能提供一个解决方案,为什么这个脚本不应该正常工作,我将不胜感激。

如果你将它设置为systemmodal,你就可以可靠地将通知放在前面:

对话框

MsgBox "Disk Report Complete",vbInformation+vbSystemModal,"Notice"

弹出

Set oWSH = CreateObject("WScript.Shell")
oWSH.Popup "Disk Report Complete",5,"Notice",vbInformation+vbSystemModal

但我认为更好的选择是使用内置的通知系统:

Toast-Example.vbs

Set oWSH = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
MyFolder = oFSO.GetParentFolderName(WScript.ScriptFullName)
oWSH.CurrentDirectory = MyFolder
Sub Toast(Msg,Wait)
oWSH.Run "Powershell.exe -NoLogo -ExecutionPolicy Bypass -File .Toast.ps1 """ & Msg & """",0,False
WScript.Sleep Wait * 1000
End Sub
Toast "Disk Report Complete",7
Toast "Memory Report Complete",7
Toast "CPU Report Complete",7

Toast.ps1改编自这里

Param (
$Msg = ''
)
function Show-Notification {
[cmdletbinding()]
Param (
[string]
$ToastTitle,
[string]
[parameter(ValueFromPipeline)]
$ToastText
)
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
$Template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText02)
$RawXml = [xml] $Template.GetXml()
($RawXml.toast.visual.binding.text|where {$_.id -eq "1"}).AppendChild($RawXml.CreateTextNode($ToastTitle)) > $null
($RawXml.toast.visual.binding.text|where {$_.id -eq "2"}).AppendChild($RawXml.CreateTextNode($ToastText)) > $null
$SerializedXml = New-Object Windows.Data.Xml.Dom.XmlDocument
$SerializedXml.LoadXml($RawXml.OuterXml)
$Toast = [Windows.UI.Notifications.ToastNotification]::new($SerializedXml)
$Toast.Tag = "My Notices"
$Toast.Group = "My Notices"
$Toast.ExpirationTime = [DateTimeOffset]::Now.AddMinutes(1)
$Notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("My Notices")
$Notifier.Show($Toast);
}
Show-Notification $Msg

最新更新