Powershell,关闭活动窗口



我找到了一个经过修改的代码片段,用于查找活动窗口并通过ps1脚本关闭它。我的目标是使我的键绑定与使用hotkeyD的Linux相同。

我对脚本有三个问题。一个是它不会关闭活动的资源管理器窗口,如果我正在浏览文件。我该怎么做呢?

第二个问题是。我想有winfetch执行终端,只要我启动它。但是,如果我将它添加到我的powershell配置文件中。它使脚本的执行时间更长。我如何使脚本省略代码,我有在我的powershell配置文件中的文档?

第三速度。剧本不是即时性的。就像按alt+F4一样。它需要一两秒钟才能激活。我该如何加快速度?

谢谢。期待看到一些回复。脚本如下:

[CmdletBinding()]            
Param(            
)            
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class UserWindows {
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
}
"@            
try {            
$ActiveHandle = [UserWindows]::GetForegroundWindow()
$Process = Get-Process | ? {$_.MainWindowHandle -eq $activeHandle}
$Process | Select ProcessName, @{Name="AppTitle";Expression= {($_.CloseMainWindow())}}
} catch {            
Write-Error "Failed to get active Window details. More Info: $_"            
}
exit

这种类型的任务(即创建一个关闭活动窗口的热键)看起来更适合于AutoHotkey而不是Powershell。


例如,这可以在AutoHotkey中使用:

#d::WinClose A ;remaps Super+d to a "close the Active Window" command

如果您想了解更多关于如何设置AHK或该脚本如何工作的信息,请随意访问。

更新了github项目,包含了一个窗口关闭函数。好像是在模拟alt + F4键

最新更新