3

When a window is resized by aero snap, User32.GetWindowPlacement(hWnd).rcNormalPosition still stores its original rectangle, while User32.GetWindowRect is affected.
Since aero snap seems independent from WINDOWPLACEMENT, now we cannot collect the complete information of the actual placement simply using user32.dll. Thus I'm wondering if there's a way to get the aero snap state of a window, indicating whether the window is docked and which side the window is docked to.

0x269
  • 688
  • 8
  • 20

2 Answers2

1

Aero Snap is a feature of the Shell, not the windowing system. Thus, the windowing system cannot provide that information, because it is not aware of those states.

And the Shell doesn't make this information available either. So, in essence, the system doesn't provide the Aero Snap state of any given window through a public API.

IInspectable
  • 46,945
  • 8
  • 85
  • 181
  • That's a pity. Do you have any clue on why Microsoft doesn't provide such APIs from Shell? – 0x269 Apr 10 '21 at 13:24
  • 1
    @true_mogician: Because developers typically should never need that information. (Also: more work to document and support it. More things to abuse.) – Andreas Rejbrand Apr 10 '21 at 13:32
  • 1
    It's an opaque feature of the system, that client code need not be aware of. It coöperates with a window's information reported from `WM_GETMINMAXINFO`. It honors other limitations of window resizing requests. In all, there's no reason to even know that Aero Sanp exists. If you do have a justifiable use case, Microsoft accepts *"Design Change Requests"*. You're going to have to explain it, though. – IInspectable Apr 10 '21 at 13:38
  • 1
    Keep in mind that people have been asking for a public API [12 years ago](https://learn.microsoft.com/en-us/archive/blogs/e7/designing-aero-snap) already. While I don't know whether there were any formal requests to publish an API, it sure looks like it wouldn't get much priority treatment. – IInspectable Apr 10 '21 at 13:52
  • 1
    @IInspectable Thanks for the information. I'm just trying to restore the placement of some windows more accurately, It's OK to simply ignore this feature and use user32.dll – 0x269 Apr 10 '21 at 14:11
  • @IInspectable: "In all, there's no reason to even know that Aero Sanp exists." Consider an application like Outlook, which closes and recreates its main window from time to time. You can have Outlook's main window snapped to a monitor border only to have it appear to spontaneously "unsnap" at unpredictable moments. If the snap state were programmatically accessible, the replacement window could be put back exactly where the original one was. – Adrian McCarthy Dec 28 '22 at 08:28
1

I like having my main windows remember all of their placement information so that I it can be restored when they are restarted. In that past, it was enough to save a copy of the window placement structure and to set it back when recreating the window.

The introduction of snap required keeping some extra information. I detected whether a window appeared to be snapped by comparing its window rectangle to the work area rectangle of the monitor that contains the window. If it seemed to be snapped to one of the edges, I recorded that along with the placement information. Upon creating the window, I first restore the window placement, and then, if I have a snap state recorded, I change the window's size and position accordingly.

You can distinguish between a window that's been snapped to a monitor edge from one that's been carefully sized and placed there because the snapped window's rectangle won't match the one in the window placement.

This approach worked great in Windows 7. I recently discovered that Windows 10 added more flexibility to the snap locations and sizes as well as playing more games to achieve the annoyingly invisible resize borders. So my detection code doesn't always recognize a snapped window, but that should fixable.

Some popular software (like the major browsers) seem to remember that they were snapped, and I assume they use the same approach.

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175