Focus window created by PowerShell script

╄→гoц情女王★ 提交于 2019-12-25 05:12:29

问题


I can display a simple Visual Basic inputbox from a PowerShell script like this:

$null = [reflection.assembly]::loadwithpartialname("microsoft.visualbasic")
$input = [microsoft.visualbasic.interaction]::inputbox($question, "bla", $text)

However, the inputbox window does not get focus which remains with the PowerShell window.

Is there a way to give focus to the inputbox window?


回答1:


You could focus the InputBox from a job.

For example:

$null = [reflection.assembly]::loadwithpartialname("microsoft.visualbasic")
$activateWindow = {
        $null = [reflection.assembly]::loadwithpartialname("microsoft.visualbasic")
        $isWindowFound = $false
        while(-not $isWindowFound) {
            try {
                [microsoft.visualbasic.interaction]::AppActivate($args[0])
                $isWindowFound = $true
            }
            catch {
                sleep -Milliseconds 100
            }
        }
    } 

$job = Start-Job $activateWindow -ArgumentList "Unique Title"
$input = [microsoft.visualbasic.interaction]::inputbox("What is your answer?", "Unique Title", "none")
Remove-Job $job -Force
Write-Host $input -ForegroundColor Yellow


来源:https://stackoverflow.com/questions/9978727/focus-window-created-by-powershell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!