GetWindowPlacement gives the incorrect window position
Asked Answered
P

1

9

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.

Pierro answered 15/4, 2011 at 6:44 Comment(3)
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?Relationship
Looks like it may be a dupe of GetWindowRect too small on Windows 7Relationship
@ben it is my experience that GetWindowRect returns correct values but GetWindowPlacement is the one with problems.Fenton
F
8

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.

Fenton answered 15/4, 2011 at 7:7 Comment(7)
I don't care what the mechanism was used to resize the window. I just want to know its current position.Pierro
If you want to know where your window is, call GetWindowRect(). If you want to know that it has moved, listen for WM_SIZE.Fenton
@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".Fenton
@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.Fenton
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.Germiston
@cody not sure I agree. I like programs to remember.Fenton
@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.Chilblain

© 2022 - 2024 — McMap. All rights reserved.