2010-11-08 30 views
1

是否可以在PowerShell中使用Windows 7 TaskDialog?Powershell TaskDialog

我想下面的消息框轉換爲TaskDialog其實:

任何人都知道如何/是否可以做到這一點?

感謝,

回答

2

你需要使用微軟的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中,你基本上可以把它作爲一個字符串或一個int如果你喜歡。

當然,這幾乎不能抓住TaskDialog可以做什麼的表面 - 如果只用這個代碼來使用它,它的外觀和行爲將與您當前的對話框非常相似 - 但您可以探索其他的可能性 - 我可以推薦這個MSDN Magazine Article的TaskDialog構建器工具來學習許多選項。

0

可以使用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聲明將不得不請求一個特定的版本,以使其工作。抱歉。你可能仍然可以將必要的東西封裝到一個可以運行的小命令行應用程序中。不理想,但也許是最簡單的路線。

+0

對不起 - 仍然有點困惑。我將如何使用文章中的代碼,因爲這是c#?認爲我錯過了一些東西... – Ben 2010-11-08 13:54:25

+0

將其轉換爲等效的PowerShell代碼。你可以用'New-Object'創建對象,然後正常設置屬性。 – Joey 2010-11-08 14:21:46