在 Powershell/wasp 中通过鼠标光标获取 window 标题?
Get window title via mouse cursor in Powershell/wasp?
我构建了一个 powershell 脚本(using wasp),它将 any window 设置为“永远在最前面”模式。
我运行脚本通过:
Get-WindowByTitle *emul* | Set-TopMost
- 为什么我需要它?*
当我在 Eclipse/Androidstudio
中编程时 - 我希望模拟器始终在前面。所以脚本正在寻找所有具有 emul
标题的 windows ( 是实际标题的一部分 "emulator.exe"
)并设置它永远在最前面。
好的。
但现在我想在不更改脚本的情况下为每个 window 执行此操作。
我将如何选择window?通过鼠标光标(仅限悬停)。 (当我将鼠标放在 calc.exe 上,然后按一些键序列 - 这将激活 PS 脚本 - 它会搜索哪个 window 的光标位于 )
问题
我怎样才能 select 有鼠标光标的 window 的 title
? (window 不必 处于活动状态)
示例:
正在看:
我想得到MyChromeBrowserTitle
虽然它在后台,(记事本在前面)。它应该是 return chrome 的标题,因为光标在 chrome window.
以下可能不是执行此操作的最佳方法,它不适用于资源管理器 windows,因为资源管理器是 运行 桌面 + 某些特定文件夹资源管理器 windows.但是它适用于其余部分。
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class Utils
{
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll")]
public static extern bool GetWindowRect(
HandleRef hWnd,
out RECT lpRect);
}
"@
Add-Type -AssemblyName System.Windows.Forms
$p = [Windows.Forms.Cursor]::Position
Get-Process | %{
if ($_.MainWindowHandle)
{
$o = New-Object -TypeName System.Object
$href = New-Object -TypeName System.RunTime.InteropServices.HandleRef -ArgumentList $o, $_.MainWindowHandle
$rect = New-Object utils+RECT
[Void][Utils]::GetWindowRect($href, [ref]$rect)
if ($p.X -ge $rect.Left -and $p.X -le $rect.Right -and
$p.Y -ge $rect.Top -and $p.Y -le $rect.Bottom
)
{
$_.MainWindowTitle
}
}
}
编辑
因为我是 运行 Powershell V3,上面的代码对我有用。
我尝试设置 Set-StrictMode -Version 2
所以我们 运行 是同一个版本。
以下在 V2 中对我有用:
$def = @'
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll")]
public static extern bool GetWindowRect(
HandleRef hWnd,
out RECT lpRect);
'@
Add-Type -MemberDefinition $def -Namespace Utils -Name Utils
Add-Type -AssemblyName System.Windows.Forms
$p = [Windows.Forms.Cursor]::Position
Get-Process | %{
if ($_.MainWindowHandle)
{
$o = New-Object -TypeName System.Object
$href = New-Object -TypeName System.RunTime.InteropServices.HandleRef -ArgumentList $o, $_.MainWindowHandle
$rect = New-Object Utils.Utils+RECT
[Void][Utils.Utils]::GetWindowRect($href, [ref]$rect)
if ($p.X -ge $rect.Left -and $p.X -le $rect.Right -and
$p.Y -ge $rect.Top -and $p.Y -le $rect.Bottom
)
{
$_.MainWindowTitle
}
}
}
我构建了一个 powershell 脚本(using wasp),它将 any window 设置为“永远在最前面”模式。
我运行脚本通过:
Get-WindowByTitle *emul* | Set-TopMost
- 为什么我需要它?*
当我在 Eclipse/Androidstudio
中编程时 - 我希望模拟器始终在前面。所以脚本正在寻找所有具有 emul
标题的 windows ( 是实际标题的一部分 "emulator.exe"
)并设置它永远在最前面。
好的。
但现在我想在不更改脚本的情况下为每个 window 执行此操作。
我将如何选择window?通过鼠标光标(仅限悬停)。 (当我将鼠标放在 calc.exe 上,然后按一些键序列 - 这将激活 PS 脚本 - 它会搜索哪个 window 的光标位于 )
问题
我怎样才能 select 有鼠标光标的 window 的 title
? (window 不必 处于活动状态)
示例:
正在看:
我想得到MyChromeBrowserTitle
虽然它在后台,(记事本在前面)。它应该是 return chrome 的标题,因为光标在 chrome window.
以下可能不是执行此操作的最佳方法,它不适用于资源管理器 windows,因为资源管理器是 运行 桌面 + 某些特定文件夹资源管理器 windows.但是它适用于其余部分。
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class Utils
{
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll")]
public static extern bool GetWindowRect(
HandleRef hWnd,
out RECT lpRect);
}
"@
Add-Type -AssemblyName System.Windows.Forms
$p = [Windows.Forms.Cursor]::Position
Get-Process | %{
if ($_.MainWindowHandle)
{
$o = New-Object -TypeName System.Object
$href = New-Object -TypeName System.RunTime.InteropServices.HandleRef -ArgumentList $o, $_.MainWindowHandle
$rect = New-Object utils+RECT
[Void][Utils]::GetWindowRect($href, [ref]$rect)
if ($p.X -ge $rect.Left -and $p.X -le $rect.Right -and
$p.Y -ge $rect.Top -and $p.Y -le $rect.Bottom
)
{
$_.MainWindowTitle
}
}
}
编辑
因为我是 运行 Powershell V3,上面的代码对我有用。
我尝试设置 Set-StrictMode -Version 2
所以我们 运行 是同一个版本。
以下在 V2 中对我有用:
$def = @'
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll")]
public static extern bool GetWindowRect(
HandleRef hWnd,
out RECT lpRect);
'@
Add-Type -MemberDefinition $def -Namespace Utils -Name Utils
Add-Type -AssemblyName System.Windows.Forms
$p = [Windows.Forms.Cursor]::Position
Get-Process | %{
if ($_.MainWindowHandle)
{
$o = New-Object -TypeName System.Object
$href = New-Object -TypeName System.RunTime.InteropServices.HandleRef -ArgumentList $o, $_.MainWindowHandle
$rect = New-Object Utils.Utils+RECT
[Void][Utils.Utils]::GetWindowRect($href, [ref]$rect)
if ($p.X -ge $rect.Left -and $p.X -le $rect.Right -and
$p.Y -ge $rect.Top -and $p.Y -le $rect.Bottom
)
{
$_.MainWindowTitle
}
}
}