English 中文(简体)
1. 优化和编外特定窗口桌面方案
原标题:Maximize and cascade a specific windows desktop program
  • 时间:2023-09-05 16:50:00
  •  标签:
  • powershell

我想把任务栏中的所有笔记窗口放在最小化或无管理的最大限度上,然后把笔记放在顶上,以便我能够看一看一枪,要么就每一枪作出节省或删除决定。

下面这一文字是行之有效的,但是它能最大限度地发挥所有作用,然后把所有东西推到一起。 这 desktop了桌面,但我要再次着重谈谈我开过的许多笔记。

(Get-Process | Where {$_.MainWindowTitle}).MainWindowTitle -and {$_.Description -like  *note* } | % {
    $wshell = (New-Object -ComObject wscript.shell)
    $wshell.AppActivate($_)
    sleep 1
    $wshell.SendKeys("% x")
    sleep 1
    $ShellExp = New-Object -ComObject Shell.Application
    $ShellExp.CascadeWindows()
} | Out-Null
问题回答

你们再做些什么,要求温和会呼吁:

# Helper type for various windows-related WinAPI functions.
Add-Type -Namespace Util -Name WinApi -MemberDefinition @ 
    // The callback delegate, which receives:
    // - the hWnd being enumerated
    // - a custom LPARAM value passed on invocation to EnumWindows()
    // The delegate must return $true to keep enumerating; in other words: $false stops the enumeration.
    // Note the custom ArrayList parameter in lieu of an IntPtr (LPARAM) (using a generic list is NOT an option, because generic types cannot be marshalled).
    public delegate bool EnumWindowsProc(IntPtr hWnd, System.Collections.ArrayList lParam);

    // The EnumWindows() API function that enumerates *top-level windows*.
    // It too uses a custom ArrayList parameter instead of the IntPtr (LPARAM), which the callback receives.
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, System.Collections.ArrayList lParam);
    
    // Get the class name of a window by its handle.
    [DllImport("user32.dll")]
    public static extern int GetClassName(IntPtr hWnd, System.Text.StringBuilder classNameBuf, int nMaxCount);

    // Cascade the specified window(s).
    [DllImport("user32.dll")]
    public static extern ushort CascadeWindows(IntPtr hwndParent, uint wHow, IntPtr lpRect, uint cKids, IntPtr[] lpKids);
    
    // Show the specified window.
    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    // Make the specified window the foreground window, if allowed (see below).
    [DllImport("user32.dll")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);

    // Allow making the windows of other processes the foreground window 
    // for the remainder of the session.
    [DllImport("user32.dll", EntryPoint="SystemParametersInfo")]
    static extern bool SystemParametersInfo_Set_UInt32(uint uiAction, uint uiParam, UInt32 pvParam, uint fWinIni);
    public static void AllowWindowActivation()
    {
      if (! SystemParametersInfo_Set_UInt32(0x2001 /* SPI_SETFOREGROUNDLOCKTIMEOUT */, 0, 0 /* timeout in secs */, 0 /* non-persistent change */)) {
        throw new System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error(), "Unexpected failure calling SystemParametersInfo() with SPI_SETFOREGROUNDLOCKTIMEOUT");
      }
    }
    
 @

# Get all Notepad windows, on the assumption that their window class name is  Notepad .
[System.Collections.ArrayList] $matchingHwnds = @()
[Util.WinApi]::EnumWindows(
    {                                                                  # delegate (callback function)
      param([intptr] $hWnd, [System.Collections.ArrayList] $param) 
      $sb = [System.Text.StringBuilder]::new(1024)
      $null = [Util.WinApi]::GetClassName($hWnd, $sb, $sb.Capacity-1)
      if ($sb.ToString() -eq  Notepad ) {
        $param.Add($hWnd)
      }
      return $true # continue enumerating
    }, 
    $matchingHwnds                                                          # the array list to pass as custom data
)

# Reverse the matching window handles (if any).
[IntPtr[]] $matchingHwndsInReverse = [Linq.Enumerable]::Reverse([IntPtr[]] $matchingHwnds)

# Make sure all windows are restored (non-minimized, non-maximized), in reverse order.
foreach ($hwnd in $matchingHwndsInReverse) {
  $null = [Util.WinAPI]::ShowWindow($hwnd, 1)
}

# Cascade them.
$null = [Util.WinAPI]::CascadeWindows([IntPtr]::Zero, 0, [IntPtr]::Zero, $matchingHwnds.Count, $matchingHwnds)

# Activate them in reverse order.
# First, enable cross-process window activation...
[Util.WinAPI]::AllowWindowActivation()
# ... then activate them.
foreach ($hwnd in $matchingHwndsInReverse) {
  $null = [Util.WinAPI]::SetForegroundWindow($hwnd)
}




相关问题
Mutually exclusive powershell parameters

SCENARIO I m writing a cmdlet for Powershell 2.0 using Visual Studio 2008 and .NET 3.5 the cmdlet requires 3 arguments. my intended grammar of the cmdlet is something like this: cmdletname [foo|...

Run a program from PowerShell with timeout

I ll write a script that runs a program and wait for it finished. But if the program is not finished within a specified time I want that the program is killed.

How to transpose data in powershell

I have a file that looks like this: a,1 b,2 c,3 a,4 b,5 c,6 (...repeat 1,000s of lines) How can I transpose it into this? a,b,c 1,2,3 4,5,6 Thanks

Powershell v2 remoting and delegation

I have installed Powershell V2 on 2 machines and run Enable-PsRemoting on both of them. Both machines are Win 2003 R2 and are joined to the same active directory domain and I can successfully run ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

热门标签