简体   繁体   English

powershell - 开始 - 进程 - 等待 + 始终在顶部(最顶部)

[英]powershell - Start-Process -wait + always on top (topmost)

i have the following code:我有以下代码:

Start-Process -FilePath myprogram.exe -Wait
# ... some other code to be executed AFTER the previous process has ended

What i need我需要的

I need to start the process with -wait as you can see, but i also need that that the window of that process to be always on top (or "topmost").如您所见,我需要使用-wait开始该过程,但我还需要该过程的 window 始终位于顶部(或“最顶部”)。

What i tried我试过的

I've seen this question/answer: https://stackoverflow.com/a/73319269我看过这个问题/答案: https://stackoverflow.com/a/73319269

And i've tried what the solution suggests, like this:我已经尝试了解决方案的建议,如下所示:

$User32 = Add-Type -Debug:$False -MemberDefinition '
    [DllImport("user32.dll")] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X,int Y, int cx, int cy, uint uFlags);
' -Name "User32Functions" -namespace User32Functions -PassThru

$proc = Start-Process -FilePath myprogram.exe -Passthru

$Handle = ($proc).MainWindowHandle
[Void]$User32::SetWindowPos($Handle, -1, 0, 0, 0, 0, 0x53)

But

  • it doesn't work, the myprogram.exe window is not topmost它不起作用,myprogram.exe window 不是最顶层的
  • i had to remove -wait to add and utilise -passthru , so how can i detect when the execution of myprogram.exe is over?我必须删除-wait才能添加和使用-passthru ,那么如何检测 myprogram.exe 的执行何时结束?

Thank you for your help.谢谢您的帮助。

Taking cmd.exe for a Minimal, Reproducible Example :cmd.exe作为一个最小的、可重现的示例

$User32 = Add-Type -Debug:$False -MemberDefinition '
    [DllImport("user32.dll")] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X,int Y, int cx, int cy, uint uFlags);
' -Name "User32Functions" -namespace User32Functions -PassThru

Start-Process -FilePath cmd.exe
Start-Sleep 1 # wait for the process to initialize
$Proc = Get-Process cmd
$Handle = ($Proc).MainWindowHandle
[Void]$User32::SetWindowPos($Handle, -1, 0, 0, 0, 0, 0x53)
Wait-Process $Proc.id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM