SetForegroundWindow,AllowSetForegroundWindow& LockSetForegroundWindow

时间:2012-10-18 11:58:19

标签: winapi powershell powershell-v2.0

使用powershell代码我尝试更改窗口的位置(工作正常)并将此窗口“始终位于顶部”。

请在下面找到我的代码:

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

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);
  }
"@


Add-Type @"
  using System;
  using System.Runtime.InteropServices;
  public class Tricks {
    [DllImport("user32.dll")]
    public static extern IntPtr GetForegroundWindow();
}
"@

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


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




do
{
    Start-Sleep -m 3000
    $allWindow = Select-Window Perso*
    if($allWindow)
    {
        foreach ($currentWindow in $allWindow) 
        {
            $positionWindow = WindowPosition $currentWindow
            foreach ($currentPosition in $positionWindow) 
            {
                #Loop on each windows
                if ( $currentWindow.title -match "\([0-9]*\)#" )
                {
                    # Retrieve windows ID
                    $id = $currentWindow.title.Substring($currentWindow.title.IndexOf("(")+1, $currentWindow.title.IndexOf(")")-$currentWindow.title.IndexOf("(")-1)
                    $allHUDWindow = Select-Window * | where {$_.Title -match "\($id\).*.txt"}

                    if($allHUDWindow)
                    {
                        foreach ($currentHUDWindow in $allHUDWindow) 
                        {
                            $currentForgr = [Tricks]::GetForegroundWindow()
                            if ( ($currentForgr -match $currentWindow.Handle -or  $currentForgr -match $currentHUDWindow.Handle) )
                            {
                                write-host "Pus currentHUDWindow in Front"


                                $hud =  (get-process -Id $currentHUDWindow.ProcessId).MainWindowHandle

                                [allowFor]::AllowSetForegroundWindow(-1)
                                [SFW]::SetForegroundWindow($hud)

                                #Modification de la position
                                Set-WindowPosition -X ($currentPosition.x-10) -Y ($currentPosition.y-30) -WIDTH ($currentPosition.width+20) -HEIGHT ($currentPosition.height+30) -Window $currentHUDWindow          
                            }
                        }
                    }
                }
            }
        }
    }
} while(1)

但是:

                                [allowFor]::AllowSetForegroundWindow(-1)
                                [SFW]::SetForegroundWindow($hud)

让我失误。

来自文档:

An application cannot force a window to the foreground while the user is working with another window. Instead, Windows flashes the taskbar button of the window to notify the user.

你知道我怎么能逼迫$ currentHUDWindow前进?而且不仅闪烁任务栏按钮?

感谢您的帮助

0 个答案:

没有答案