保留双显示器的表单位置和大小
我有代码在关闭时保留表单的大小和位置,并在加载时定位表单并调整其大小。
我想考虑到加载时持久位置可能超出屏幕范围。在单个显示器设置中很容易进行测试,但对于多个显示器,我该如何测试。我不知道屏幕是水平设置还是垂直设置等。不知何故,我需要确定所有屏幕的组合区域,并查明表单的持久位置是否在该区域内。
I have code that persists a forms size and location when it closes, and positions and sizes the form when it loads.
I want to take into account that on load it is possible that the persisted location is outside of the bounds of the screen. Easy to test in a single monitor setup but for multiple monitors, how can I test that. I don't know if the screens are set up horizontally or vertically etc. Somehow I need to determine the combined area of all screens and find out if the forms persisted location is within that area.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用于记住和恢复窗口状态的一对好函数是
GetWindowPlacement< /code>
和
SetWindowPlacement
。GetWindowPlacement
不仅检索普通窗口的当前状态和位置,而且对于最大化窗口,还检索其最大化之前的位置。出于您的目的,
SetWindowPlacement
非常有用,因为如果要求将窗口恢复到屏幕外,它知道将窗口微移到最近的屏幕上:不幸的是,我不知道这两个函数的现成 .NET 等效项,尽管它们可以通过 P/Invoke 直接调用(参见 http://pinvoke.net)。
A good pair of functions to use for remembering and restoring window state is
GetWindowPlacement
andSetWindowPlacement
.GetWindowPlacement
retrieves not only the current state and position of a normal window, but, for a maximised window, its location before it was maximised.For your purposes,
SetWindowPlacement
is useful because knows to nudge a window onto the nearest screen if it's asked to restore it off screen:Unfortunately I don't know of a ready-made .NET equivalent of these two functions, although they're straightforward to call via P/Invoke (cf http://pinvoke.net).
看看C#:如何使表单记住其边界和窗口状态(考虑双显示器设置)
Take a look at C#: How to make a form remember its Bounds and WindowState (Taking dual monitor setups into account)