Powershell - SetForegroundWindow [英] Powershell - SetForegroundWindow

查看:104
本文介绍了Powershell - SetForegroundWindow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

With a powershell code I try to change the position of a window (is works correctly) and put this windows "Always on top".

请在下面找到我的代码:

Please find below my code:

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,我调用Set-WindowActive $currentHUDWindow",但我也需要应用这种函数:

Currenlty, I call "Set-WindowActive $currentHUDWindow" but I need to apply also this kind of function :

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

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

I try to added this function to my code and called SetForegroundWindow($currentHUDWindow). But I encountered an error.

你能帮我吗?

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

I need to put the window $currentHUDWindow on top !

谢谢

推荐答案

这就是 P/invoke 和使用 SetForegroundWindow

This is how P/invoke and 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)

这篇关于Powershell - SetForegroundWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆