Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从powershell脚本显示带焦点的WinForms对话框_Winforms_User Interface_Powershell_Focus - Fatal编程技术网

从powershell脚本显示带焦点的WinForms对话框

从powershell脚本显示带焦点的WinForms对话框,winforms,user-interface,powershell,focus,Winforms,User Interface,Powershell,Focus,我的一个同事带着一个有趣的问题来找我 他正在显示PowerShell脚本中的WinForms表单,当表单成功打开时,无法获得焦点。相反,PowerShell命令窗口将保持焦点,直到显式单击表单 正在使用从PowerShell命令行运行脚本。\ScriptName.ps1 我们尝试了dlg.ShowDialog()(有和没有传递$this)、dlg.Show()、dlg.Focus()等的各种组合,但都没有成功 有人知道如何在显示窗体时提供窗体焦点吗?这就是我们如何使其工作的(第一行是我们缺少的)

我的一个同事带着一个有趣的问题来找我

他正在显示PowerShell脚本中的WinForms表单,当表单成功打开时,无法获得焦点。相反,PowerShell命令窗口将保持焦点,直到显式单击表单

正在使用从PowerShell命令行运行脚本。\ScriptName.ps1

我们尝试了dlg.ShowDialog()(有和没有传递$this)、dlg.Show()、dlg.Focus()等的各种组合,但都没有成功


有人知道如何在显示窗体时提供窗体焦点吗?

这就是我们如何使其工作的(第一行是我们缺少的):


除了调用Show()之外,它还做其他事情吗?我想命令窗口会把焦点偷回来。举个例子,试试ShowDialog()。当他向我解释这个问题时,我也希望是这样,但事实上他已经在使用ShowDialog()。这个问题原来是更深层次的,但他找到的解决方案,请看下面我的答案。
$WinForm.Add_Shown({$WinForm.Activate()})
$WinForm.ShowDialog($this) | out-null