Powershell - SetForegroundWindow

eld*_*ano 3 powershell powershell-2.0

使用PowerShell代码我尝试更改窗口的位置(正常工作)并将此窗口"始终在顶部".

请在下面找到我的代码:

Import-Module C:/install/WASP/wasp.dll

for($i=1; $i -le 300000; $i++)
{
    $allWindow = Select-Window MyCheck*
    if($allWindow)
    {
        foreach ($currentWindow in $allWindow) 
        {
            $positionWindow = WindowPosition $currentWindow
            foreach ($currentPosition in $positionWindow) 
            {
                #if we find the correct windows
                if ( $currentWindow.title -match "\([0-9]*\)#" )
                {
                    #write-host "@@##@@"$currentWindow.title",(@@#@@)"$currentPosition.x",(@@#@@)"$currentPosition.y",(@@#@@)"$currentPosition.width",(@@#@@)"$currentPosition.height",(@@#@@)"$currentWindow.title",(@@#@@)"$currentWindow.IsActive

                    $id = $currentWindow.title.Substring($currentWindow.title.IndexOf("(")+1, $currentWindow.title.IndexOf(")")-$currentWindow.title.IndexOf("(")-1)
                    $allHUDWindow = Select-Window * | where {$_.Title -match "\($id\).*.txt"}
                    #If we find the second window, we have to superimpose $currentHUDWindow to $currentWindow
                    if($allHUDWindow)
                    {
                        foreach ($currentHUDWindow in $allHUDWindow) 
                        {

                            #I need to set $currentHUDWindow "Always on top"
                            Set-WindowActive $currentHUDWindow
                            Set-WindowPosition -X ($currentPosition.x-10) -Y ($currentPosition.y-30) -WIDTH ($currentPosition.width+20) -HEIGHT ($currentPosition.height+30) -Window $currentHUDWindow
                        }
                    }
                }
            }
        }
    }
} 
Run Code Online (Sandbox Code Playgroud)

Currenlty,我称之为"Set-WindowActive $ currentHUDWindow",但我还需要应用这种功能:

 [DllImport("user32.dll")]
 [return: MarshalAs(UnmanagedType.Bool)]
 public static extern bool SetForegroundWindow(IntPtr hWnd);
Run Code Online (Sandbox Code Playgroud)

我尝试将此函数添加到我的代码中并调用SetForegroundWindow($ currentHUDWindow).但是我遇到了一个错误.

请你帮助我好吗 ?

我需要将窗口$ currentHUDWindow置于顶部!

谢谢

CB.*_*CB. 10

这就是P/invoke和use的用法 SetForegroundWindow

Add-Type @"
  using System;
  using System.Runtime.InteropServices;
  public class SFW {
     [DllImport("user32.dll")]
     [return: MarshalAs(UnmanagedType.Bool)]
     public static extern bool SetForegroundWindow(IntPtr hWnd);
  }
"@


$h =  (get-process NOTEPAD).MainWindowHandle # just one notepad must be opened!
[SFW]::SetForegroundWindow($h)
Run Code Online (Sandbox Code Playgroud)