How can you set focus to an inputbox with PowerShell?
I'm using the following PowerShell 2.0 code to grab input from a vb inputbox:
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$name = [Microsoft.VisualBasic.Interaction]::InputBox("What is your name?", "Name", "bob")
Sometimes when I run it the input box appears behind the active window. Is there a way to make the input box top most? Or an easy way to get its handle and just use setforegroun开发者_运维百科dwindow?
Thanks!!
I'm not sure how to do this easily considering that the InputBox call is modal so you can't easily try to find the window handle and do a set-foreground on that window (unless you attempt to use a background job). Rather than use this VisualBasic text input box, how about a "roll your own" implementation using WPF/XAML. It is pretty easy but it does require WPF which is installed by PowerShell 2.0 if necessary.
$Xaml = @'
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window"
Title="Name" Height="137" Width="444" MinHeight="137" MinWidth="100"
FocusManager.FocusedElement="{Binding ElementName=TextBox}"
ResizeMode="CanResizeWithGrip" >
<DockPanel Margin="8">
<StackPanel DockPanel.Dock="Bottom"
Orientation="Horizontal" HorizontalAlignment="Right">
<Button x:Name="OKButton" Width="60" IsDefault="True"
Margin="12,12,0,0" TabIndex="1" >_OK</Button>
<Button Width="60" IsCancel="True" Margin="12,12,0,0"
TabIndex="2" >_Close</Button>
</StackPanel>
<StackPanel >
<Label x:Name="Label" Margin="-5,0,0,0" TabIndex="3">Label:</Label>
<TextBox x:Name="TextBox" TabIndex="0" />
</StackPanel>
</DockPanel>
</Window>
'@
if ([System.Threading.Thread]::CurrentThread.ApartmentState -ne 'STA')
{
throw "Script can only be run if PowerShell is started with -STA switch."
}
Add-Type -Assembly PresentationCore,PresentationFrameWork
$xmlReader = [System.Xml.XmlReader]::Create([System.IO.StringReader] $Xaml)
$form = [System.Windows.Markup.XamlReader]::Load($xmlReader)
$xmlReader.Close()
$window = $form.FindName("Window")
$window.Title = "My App Name"
$label = $form.FindName("Label")
$label.Content = "What is your name?"
$textbox = $form.FindName("TextBox")
$okButton = $form.FindName("OKButton")
$okButton.add_Click({$window.DialogResult = $true})
if ($form.ShowDialog())
{
$textbox.Text
}
This could be rather easily wrapped up into a Read-GuiText function.
If you set a default value for the input box it kind of makes it "modal", something like this:
$response = [Microsoft.VisualBasic.Interaction]::InputBox("Do you want to include servers in MANUAL REBOOT group ? If YES, please type: Include MANUAL reboot group","Warning!!!","")
Sub SetInputBoxFocus()
System.Threading.Thread.Sleep(300)
Microsoft.VisualBasic.AppActivate("Title)
''Console.WriteLine("Setting focus ") '"
End Sub
Dim strPW as String = ""
Dim tsStartInfo As New System.Threading.ThreadStart(AddressOf SetInputBoxFocus)
Dim tBackgroundJob As New System.Threading.Thread(tsStartInfo)
tBackgroundJob.Start()
strPW = Microsoft.VisualBasic.InputBox("Prompt: ", "Title", "", -1, -1)
tBackgroundJob = Nothing
tsStartInfo = Nothing
精彩评论