[JavaScript] 纯文本查看 复制代码 Add-Type @"
using System;
using System.Runtime.InteropServices;
public class WindowHelper {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowText(IntPtr hWnd, System.Text.StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
public static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
}
"@
$windowList = New-Object System.Collections.Generic.List[System.Object]
$callback = {
param([IntPtr]$hWnd, [IntPtr]$lParam)
$length = [WindowHelper]::GetWindowTextLength($hWnd)
if ($length -gt 0) {
$sb = New-Object System.Text.StringBuilder($length + 1)
[WindowHelper]::GetWindowText($hWnd, $sb, $sb.Capacity) | Out-Null
$title = $sb.ToString()
if (![string]::IsNullOrEmpty($title) -and [WindowHelper]::ShowWindow($hWnd, 6)) { # 6 = SW_MINIMIZE
$windowList.Add(@{
Handle = $hWnd
Title = $title
})
}
}
return $true
}
$delegate = [WindowHelper+EnumWindowsProc]$callback
[WindowHelper]::EnumWindows($delegate, [IntPtr]::Zero) | Out-Null
$sortedWindows = $windowList | Sort-Object -Property Title
foreach ($window in $sortedWindows) {
[WindowHelper]::ShowWindow($window.Handle, 9) # 9 = SW_RESTORE
[WindowHelper]::ShowWindow($window.Handle, 6) # 6 = SW_MINIMIZE
}
Write-Host "任务栏窗口已按标题首字母排序"
打开 PowerShell(以管理员身份运行)
复制上面的代码并粘贴到 PowerShell 中执行
|