拉出所有打开的文件资源管理器窗口的快捷方式

问题描述 投票:0回答:1

嗨,我正在尝试编写一个脚本来同时打开所有最小化的文件资源管理器窗口。我下面使用的脚本不起作用。

$shell = New-Object -ComObject Shell.Application
$shell.Windows() | ForEach-Object {
    if ($_.Name -eq 'File Explorer') {
        $_.Visible = $true
        $_.Document.Focus()
    }
}

我将文件保存为 text.ps1,并通过创建一个分配有宏的快捷方式来运行它。在快捷方式的位置字段中我有这个位置:

powershell -ExecutionPolicy Bypass -File "C:\Desktop\text.ps1"

我需要对脚本进行更改,还是我所描述的内容无法做到?谢谢!

windows powershell explorer
1个回答
0
投票

我怀疑你对

Shell.Application
的实现是否会起作用,撇开
.Focus()
上没有
.Document
方法不谈。您可以做的是调用本机函数来恢复窗口,此解决方案应该始终有效,但实现要困难得多,因为您正在使用 C# 实现调用低级函数。如果你想尝试一下,它会是这样的:

Add-Type @'
using System;
using System.Runtime.InteropServices;

public static class Native
{
    private delegate bool EnumWindowsProc(IntPtr hWnd, int lParam);

    [DllImport("user32.dll")]
    private static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);

    [DllImport("user32.dll")]
    private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

    [DllImport("user32.dll")]
    private static extern bool IsWindowVisible(IntPtr hWnd);

    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    public static extern int GetWindowTextLength(IntPtr hWnd);

    [DllImport("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);

    [DllImport("user32.dll")]
    public static extern IntPtr GetShellWindow();

    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    private static extern int GetWindowText(IntPtr hWnd, char[] lpString, int nMaxCount);

    public static void RestoreWindowByProcessId(uint id)
    {
        IntPtr shellWindowhWnd = GetShellWindow();
        EnumWindows((IntPtr handle, int lParam) =>
        {
            if (handle == shellWindowhWnd)
            {
                return true;
            }

            if (!IsWindowVisible(handle))
            {
                return true;
            }

            uint processId;
            if (GetWindowThreadProcessId(handle, out processId) == 0
                || processId != id)
            {
                return true;
            }

            int len;
            if ((len = GetWindowTextLength(handle)) == 0)
            {
                return true;
            }

            char[] buffer = new char[len];
            if (GetWindowText(handle, buffer, len + 1) == 0)
            {
                return true;
            }

            Console.WriteLine(string.Format(
                "Restoring window with Title: '{0}'",
                new string(buffer)));

            SendMessage(handle, 0x0112, 0xF120, 0);
            return true;
        }, 0);
    }
}
'@

$process = (Get-Process explorer).Id
[Native]::RestoreWindowByProcessId($process)
© www.soinside.com 2019 - 2024. All rights reserved.