首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell - SetForegroundWindow

Powershell - SetForegroundWindow
EN

Stack Overflow用户
提问于 2012-10-09 21:47:05
回答 1查看 16.3K关注 0票数 5

使用powershell代码,我尝试更改窗口的位置(可以正常工作),并将此窗口“始终放在最前面”。

请在下面找到我的代码:

代码语言:javascript
复制
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
                        }
                    }
                }
            }
        }
    }
} 

目前,我调用了"Set-WindowActive $currentHUDWindow“,但我也需要应用这种函数:

代码语言:javascript
复制
 [DllImport("user32.dll")]
 [return: MarshalAs(UnmanagedType.Bool)]
 public static extern bool SetForegroundWindow(IntPtr hWnd);

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

你能帮帮我吗?

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

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-09 22:14:48

这就是P/invoke和使用SetForegroundWindow的方式

代码语言:javascript
复制
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)
票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12801563

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档