GetWindowPlacement/SetWindowPlacement 在 WinForms 中不适用于高 DPI
我有一个旧版 WinForms 应用程序,我想跨会话保存窗口位置和大小。我一直在使用 GetWindowPlacement 和 < a href="http://msdn.microsoft.com/en-us/library/ms633544%28VS.85%29.aspx" rel="nofollow noreferrer">SetWindowPlacement 在 FormClosing 和 Load 事件期间。我遇到的问题是,在较高的 DPI 设置(例如“中”,尺寸为 125%)下,尺寸不断增大。我将以一定的大小对其调用 SetWindowPlacement,但是当调用 GetWindowPlacement 时,这些数字会变大 25%,即使窗口始终大小相同。当保存表单中可调整大小的元素的大小时,也存在同样的问题。
现在,如果我创建一个新的 WinForms 项目,效果很好:即使在更高的 DPI 下运行,大小也能保持稳定。我猜测项目内部存在一些遗留设置或一些神秘的表单设置将其搞乱,但我无法找出位置。
我在两个项目上都调用了 IsProcessDPIAware两者都是true
。有谁知道可能是什么原因造成的?
I've got a legacy WinForms app and I want to save the window position and size across sessions. I've been using GetWindowPlacement and SetWindowPlacement during the FormClosing and Load events. The problem I'm getting is that at higher DPI settings (Such as Medium, size at 125%) the sizes keep inflating. I'll call SetWindowPlacement on it with a certain size, but when GetWindowPlacement is called, those numbers come back 25% bigger, even though the window was the same size all along. The same sort of problem exists when saving the size of a resizable element within the form.
Now this works fine if I create a new WinForms project: The size stays stable even when running at the higher DPI. I'm guessing there's some legacy setting in the bowels of the project or some arcane Form setting that's messing it up, but I can't find out where.
I've called IsProcessDPIAware on both projects and both are true
. Does anyone know what might be causing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来好像您正在以某种方式触发缩放,正如表单的 AutoScaleMode 属性所选择的那样。两个项目之间的区别在于 AutoScaleDimensions 属性,该属性在 Designer.cs 文件中可见。
不知道为什么这会导致问题,但 Form 类已经在内部使用 GetWindowPlacement()、RecreateHandleCore() 和 UpdateWindowState() 方法。为了获得真正的帮助,我假设您需要在某处发布一个重现项目。
Sounds like you are somehow triggering scaling, as selected by the AutoScaleMode property of the form. The difference between your two projects would be the AutoScaleDimensions property, visible in the Designer.cs file.
Not sure why this would cause a problem, but the Form class already uses GetWindowPlacement() internally, RecreateHandleCore() and UpdateWindowState() methods. To get real help, I assume you'll need to post a repro project somewhere.
我在表单的 .resx 文件中发现了这个有问题的设置:
当存在此设置时,VS 会自动更改 AutoScaleBaseSize 以适用于您的 DPI,但不适用其他人的 DPI。对于其他人来说,形状会不断增大或缩小。
在设计器属性面板中选择 AutoScaleMode = Font 会导致 VS 启动并“现代化”字体缩放设置。现在它适用于所有 DPI。
I found this offending setting in the form's .resx file:
When this was present, VS would automatically change the AutoScaleBaseSize to work for your DPI, but no one else's. For everyone else, the form would constantly grow or shrink.
Choosing AutoScaleMode = Font in the designer properties panel caused VS to kick in and "modernize" the font scaling settings. Now it works for all DPIs.