2012-10-09 52 views
2

使用PowerShell代碼,我嘗試更改窗口的位置(工作正常),並將此窗口設置爲「Always on top」。PowerShell - SetForegroundWindow

請在下面找到我的代碼:

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 
         } 
        } 
       } 
      } 
     } 
    } 
} 

Currenlty,我稱之爲 「設置WindowActive $ currentHUDWindow」,但我還需要應用這種功能:

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

我嘗試添加這個函數給我的代碼並且調用SetForegroundWindow($ currentHUDWindow)。 但我遇到了一個錯誤。

你能幫我嗎?

我需要把窗口$ currentHUDWindow放在最上面!

感謝

+0

添加你的錯誤在你的問題,謝謝。 –

回答

7

這是怎麼P /調用和使用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) 
+0

完美!感謝您的回答。 – eldondano

相關問題