8

How to find out whether a window was resized using Aero Snap feature? GetWindowPlacement function gives the latest "restored" window size. I use it as follows:

WINDOWPLACEMENT wp = {};
wp.length = sizeof(WINDOWPLACEMENT);
::GetWindowPlacement( hWnd, &wp );

For instance, it gives wp.rcNormalPosition = {top=208 bottom=520 left=152 right=510}, when it should be {top=0 bottom=1920 left=152 right=510}.

Please, note that GetWindowRect gives exactly the same incorrect result. That was my fault, GetWindowRect gives the right result.

I need to save window state on the program exit and load it on start, so I need to know how windows are placed. How can I find out actual window coordinates?


Well, I made several tests with notepad.exe(and some other standard Windows components) and it saves its state in the same way — it doesn't remember whether it was "snapped". So I suppose this is intended behavior of Windows programs.

Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
  • 2
    What does `GetClientRect` give? If the client size is larger than the window size, that's a clue that something is amiss. What about `DWMWA_EXTENDED_FRAME_BOUNDS`, which is mentioned in the `GetWindowRect` comments as helpful for getting the bounds of an Aero window? – Ben Voigt Apr 15 '11 at 15:15
  • Looks like it may be a dupe of [GetWindowRect too small on Windows 7](http://stackoverflow.com/questions/3192232/getwindowrect-too-small-on-windows-7) – Ben Voigt Apr 15 '11 at 15:23
  • @ben it is my experience that GetWindowRect returns correct values but GetWindowPlacement is the one with problems. – David Heffernan Apr 15 '11 at 16:16

1 Answers1

7

AeroSnap is just another type of resize sent to your app by the window manager. As such you don't get to know that it was snap rather than any other type of resize.

The best you can hope for is to detect that opposite edges have been moved in the course of a size event. You'd need to check that height or width changed to so as to distinguish it from a move event.

The reason why you don't get told that this is a snap is that it's hard to imagine why an app would care what the mechanism for resizing is.


The comments have revealed more about your problem. You are trying to persist your apps position and size when the app closes down so that you can restore it when it restarts. You are using GetWindowPlacement() to do so and have discovered that it returns an incorrect position when the last sizing of the Window was an Aero Snap.

My own app does exactly the same and I have encountered the exact same problem. The solution I used was to call GetWindowRect() in place of GetWindowPlacement() to obtain the window position and size. You state that this fails for you, in which case I have no idea what to suggest. I must say I find it a little hard to believe that GetWindowRect() does not return the correct window rect.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I don't care what the mechanism was used to resize the window. I just want to know its current position. – Kirill V. Lyadvinsky Apr 15 '11 at 07:34
  • If you want to know where your window is, call `GetWindowRect()`. If you want to know that it has moved, listen for `WM_SIZE`. – David Heffernan Apr 15 '11 at 10:00
  • 2
    @Kirill I think your question would be improved if you changed it from "How to find out whether a window was “snapped”?" and instead asked, "Why do GetWindowRect, GetWindowPlacement etc. report incorrect positions of my app following an AeroSnap". – David Heffernan Apr 15 '11 at 14:44
  • @Kirill Now that I understand what your problem really is, I recognise that I have myself fallen foul of it and have a work around. My workaround is in the context of a VCL app so I need to investigate a little to work out how to translate that into raw Win32 API. Please bear with me. – David Heffernan Apr 15 '11 at 14:55
  • 2
    Hmm, the built-in applications (Explorer, Notepad, etc.) don't "remember" their locations as a result of Aero Snap. I assume that's because it's a convenience feature for temporarily placing windows in a certain location. It's likely that users don't *want* the snapped size/position to be remembered for the window. Same as you don't remember the size/position of a window that's been maximized or minimized. You call `GetWindowPlacement` to get the last position of the window *before* it was maximized or minimized. I'd do the same for Aero Snap, rather than trying to write code to workaround it. – Cody Gray - on strike Apr 20 '11 at 03:43
  • 2
    @cody not sure I agree. I like programs to remember. – David Heffernan Apr 20 '11 at 04:54
  • 5
    @David, there is one big difference between a snap and a "normal" resize - when you "Snap" you can double click the caption and it reverts to its original (restore) location. In this sense it is much more like a Maximize. The problem is that unlike a maximize, the WINDOWPLACEMENT doesn't give you any indication that it is in this Snap state. – Maurice Flanagan Dec 03 '11 at 15:09