我正在使用以下PowerShell 2.0代码从vb输入框中获取输入:
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$name = [Microsoft.VisualBasic.Interaction]::InputBox("What is your name?", "Name", "bob")
Run Code Online (Sandbox Code Playgroud)
有时当我运行它时,输入框出现在活动窗口后面.有没有办法让输入框最顶端?或者一个简单的方法来获得它的手柄,只需使用setforegroundwindow?
谢谢!!
考虑到InputBox调用是模态的,我不确定如何轻松地执行此操作,因此您无法轻松地尝试查找窗口句柄并在该窗口上执行set-foreground(除非您尝试使用后台作业).而不是使用这个VisualBasic文本输入框,如何使用WPF/XAML"滚动自己的"实现.它非常简单,但它确实需要WPF,必要时由PowerShell 2.0安装.
$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
}
Run Code Online (Sandbox Code Playgroud)
这可以很容易地包含在Read-GuiText函数中.
| 归档时间: |
|
| 查看次数: |
4341 次 |
| 最近记录: |