windows GetWindowPlacement 给出了不正确的窗口位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5673242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GetWindowPlacement gives the incorrect window position
提问by Kirill V. Lyadvinsky
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:
如何确定是否使用Aero Snap 功能调整了窗口大小?GetWindowPlacement
函数给出最新的“恢复”窗口大小。我使用它如下:
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}
.
例如,它给出wp.rcNormalPosition = {top=208 bottom=520 left=152 right=510}
,什么时候应该
给出{top=0 bottom=1920 left=152 right=510}
。
Please, note that That was my fault, GetWindowRect
gives exactly the same incorrect result.GetWindowRect
gives the right result.
请注意,这那是我的错,GetWindowRect
给出了完全相同的错误结果。GetWindowRect
给出了正确的结果。
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.
好吧,我用notepad.exe
(和其他一些标准的 Windows 组件)做了几个测试,它以同样的方式保存它的状态——它不记得它是否被“捕捉”了。所以我想这是 Windows 程序的预期行为。
采纳答案by David Heffernan
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.
AeroSnap 只是窗口管理器发送到您的应用程序的另一种类型的调整大小。因此,您不会知道它是快照而不是任何其他类型的调整大小。
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.
评论已经揭示了更多关于你的问题。您正在尝试在应用程序关闭时保持应用程序的位置和大小,以便您可以在它重新启动时恢复它。您正在使用GetWindowPlacement()
它并发现当窗口的最后一次调整大小是 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.
我自己的应用程序完全相同,我遇到了完全相同的问题。我使用的解决方案是调用GetWindowRect()
代替GetWindowPlacement()
获取窗口位置和大小。你说这对你来说失败了,在这种情况下,我不知道该建议什么。我必须说我觉得有点难以相信GetWindowRect()
不会返回正确的窗口矩形。