可以在PowerShell中使用Windows7 TaskDialog吗?
我想将以下消息框转换为TaskDialog:
[System.Windows.Forms.MessageBox]::Show(
"There are currently one or more Microsoft Office applications running.`n`nYou must close down all open Office applications before the template update can continue.",
"Updating Templates",
[System.Windows.Forms.MessageBoxButtons]::RetryCancel,
[System.Windows.Forms.MessageBoxIcon]::Warning )有人知道如何/是否可以做到这一点吗?
谢谢,
本
发布于 2010-11-11 03:42:54
你需要使用微软的Windows API CodePack,它很简单,但是尽管它可以在PowerShell ISE,PoshConsole,PowerGUI等上运行得很好--我不相信它可以在PowerShell.exe上运行,因为控制台加载了错误版本的comctl32.dll (公共控件库)。
# import the library dll from wherever you put it:
add-type -path .\Libraries\Microsoft.WindowsAPICodePack.dll
# Create and configure the TaskDialog
$td = New-Object Microsoft.WindowsAPICodePack.Dialogs.TaskDialog
$td.Caption = "Updating Templates"
$td.Text = "There are currently one or more Microsoft Office applications running.`n`nYou must close down all open Office applications before the template update can continue."
$td.StandardButtons = "Retry,Cancel"
$td.Icon = "Warning"
# Show the dialog and capture the resulting choice
$result = $td.Show() # will return either "Retry" or "Cancel" 希望很明显,$result值实际上是一个枚举值(类型为[Microsoft.WindowsAPICodePack.Dialogs.TaskDialogResult])……但在PowerShell中,您基本上可以将其视为字符串或整数,如果您喜欢的话。
当然,这仅仅触及了您使用TaskDialog所能做的事情的皮毛--如果您只在这段代码中使用它,它的外观和行为将与您当前的对话框非常相似--但是您可以自己探索其他可能性--我可以推荐这篇MSDN Magazine Article中的TaskDialog构建器工具作为学习许多选项的一种方法。
发布于 2010-11-08 20:25:00
可以使用Add-Type cmdlet动态编译C#类并导入该类型。因此,您只需编写C#代码来与本机TaskDialog函数接口,然后从PowerShell使用它。例如,您可以使用this library from CodeProject。构建它,然后使用
Add-Type -File TaskDialog.dll然后,您可以重新创建本文中显示的示例。
$taskDialog = New-Object Microsoft.Samples.TaskDialog
$taskDialog.WindowTitle = "My Application"
$taskDialog.MainInstruction = "Do you want to do this?"
$taskDialog.CommonButtons = [Microsoft.Samples.TaskDialogCommonButtons]::Yes -bor [Microsoft.Samples.TaskDialogCommonButtons]::No
$result = $taskDialog.Show()
if ($result -eq 6)
{
# Do it.
}但是,我注意到PowerShell无法找到进入公共控件DLL的入口点。这方面没有太多线索,也许C#代码中的P/Invoke声明必须请求一个特定的版本才能工作。抱歉的。您可能仍然可以将必要的内容封装到一个小型命令行应用程序中,然后就可以运行了。不是很理想,但可能是最简单的路线。
https://stackoverflow.com/questions/4122560
复制相似问题