PowerToys icon indicating copy to clipboard operation
PowerToys copied to clipboard

[Command Palette] Hidden (DWM-cloaked) window stays at screen center and is still returned by IsWindowVisible, breaking per-window capture in screenshot tools

Open Sakugar opened this issue 1 week ago • 2 comments

Microsoft PowerToys version

0.102.3101.0

Installation method

PowerToys auto-update

Area(s) with issue?

Command Palette

Steps to reproduce

  1. Enable Command Palette and leave it in the default (centered) position. Open it once, then dismiss it with Esc so it gets cloaked.
  2. Start a screenshot tool that captures a single window by hit-testing the point under the cursor with IsWindowVisible and walking up to the top-level window (Snipaste, Windows Snipping Tool, ShareX, ...).
  3. Move the cursor to the middle of the screen.

✔️ Expected Behavior

Only windows the user can actually see are offered as capture targets. At the center of the screen the tool should pick whatever is visible there.

❌ Actual Behavior

The hidden Command Palette window is picked instead. The tool highlights/captures an 800x480 rectangle in the middle of the screen where nothing is visible. In Snipaste this shows up as an "invisible window" parked at the center of the screen.

The window is DWM-cloaked, but it keeps WS_VISIBLE, its last on-screen position and a high z-order position, so any third-party window picker that filters on IsWindowVisible will select it.

Additional Information

Measured on this machine (PowerToys 0.102.3101.0, Windows 11 26H2 build 26300.9974, 2560x1600 @150% -> 1707x1067 logical):

class                                WinUIDesktopWin32WindowClass
title                                Command Palette
process                              Microsoft.CmdPal.UI
rect                                 453,269  800x480   (screen center 854,534)
IsWindowVisible                      true
IsIconic                             false
DwmGetWindowAttribute(DWMWA_CLOAKED) 1
exStyle                              0x00000188
  • The geometry comes straight from settings.json: "LastWindowPosition": {"X":680,"Y":404,"Width":1200,"Height":720,"Dpi":144} / 1.5 = 453,269 800x480, i.e. it stays exactly where it was last shown, in the middle of the screen.
  • In EnumWindows z-order it was above every real window at that point (Telegram, Chrome, magpie, ...), so it is the first candidate a naive hit test returns.
  • Stable across repeated samples and across a PowerToys restart (pid changes, same state).

Two things worth calling out:

  1. #40444 ("Set Command Palette window as the topmost when shown and move to the bottom when hidden") is meant to move the cloaked window to the bottom of the z-order. It is not holding here: the window was measured at the top of the stack at that point, long after the last summon. Perhaps the demotion only happens on one code path.
  2. PowerToys Quick Access (Preview) shows the same pattern: 400x516 @ 1307,503, IsWindowVisible=true, cloaked=1.

This looks like the same class of bug as #40779 (Snipping Tool per-window capture broken by Command Palette), which was closed by the stale bot rather than fixed, and it is related to #46225 (the cloaked CmdPal window still being the foreground window, so Crop and Lock targets the wrong window).

Minimal way to observe it (PowerShell, no extra tools):

Add-Type @"
using System; using System.Text; using System.Runtime.InteropServices;
public class Ghost {
  delegate bool EP(IntPtr h, IntPtr l);
  [DllImport("user32.dll")] static extern bool EnumWindows(EP cb, IntPtr l);
  [DllImport("user32.dll")] static extern bool IsWindowVisible(IntPtr h);
  [DllImport("user32.dll")] static extern bool GetWindowRect(IntPtr h, out RECT r);
  [DllImport("user32.dll", CharSet=CharSet.Unicode)] static extern int GetWindowTextW(IntPtr h, StringBuilder s, int n);
  [DllImport("dwmapi.dll")] static extern int DwmGetWindowAttribute(IntPtr h, int a, out int v, int s);
  public struct RECT { public int Left, Top, Right, Bottom; }
  public static void Run() {
    EnumWindows(delegate(IntPtr h, IntPtr l) {
      var t = new StringBuilder(256); GetWindowTextW(h, t, 256);
      if (t.ToString() != "Command Palette") return true;
      RECT r; GetWindowRect(h, out r);
      int cloaked = -1; DwmGetWindowAttribute(h, 14, out cloaked, 4);
      Console.WriteLine(r.Left + "," + r.Top + " " + (r.Right-r.Left) + "x" + (r.Bottom-r.Top) + " visible=" + IsWindowVisible(h) + " cloaked=" + cloaked);
      return true;
    }, IntPtr.Zero);
  }
}
"@
[Ghost]::Run()

Suggested fix

While the window is cloaked, park it outside the virtual desktop (the usual -32000,-32000) or shrink it to 1x1, and restore the real position when it is shown (or just before it is uncloaked). Cloaking alone leaves a WS_VISIBLE HWND at a real screen coordinate, which is enough for third-party window pickers to treat it as a live window.

Sakugar avatar Nov 27 '26 13:11 Sakugar

Hi @Sakugar,

This is intended behavior and is crucial for a seamless user experience in Command Palette. As you pointed out, the third-party picker is at fault here.

This issue is already tracked in #46457, so feel free to chime in there.

/dup #46457

jiripolasek avatar Nov 27 '26 14:11 jiripolasek

We've identified this issue as a duplicate of an existing one and are closing this thread so discussion stays in one place.

Please see the comment above for the link to the original tracking issue, and feel free to subscribe there for updates.