Delphi中如何获取屏幕的可用坐标
通过 Screen.DesktopHeight 和 Screen.DesktopWidth,我可以获得“虚拟”桌面的大小,并且我相信这适用于一台或多台显示器。
当应用程序关闭时,我将应用程序的位置(顶部和左侧)和大小(高度和宽度)保存到注册表中。当它打开时,我想确保它完全可见,因为它可能已部分移动到可见区域之外,或者屏幕尺寸可能已更改,例如通过屏幕设置或删除显示器。
我基本上可以用这段代码来做到这一点:
if MyForm.Width > screen.DesktopWidth then
MyForm.Width := screen.DesktopWidth;
if MyForm.Height > screen.DesktopHeight then
MyForm.Height := screen.DesktopHeight;
if (MyForm.Left + MyForm.Width) > screen.DesktopWidth then
MyForm.Left := screen.DesktopWidth - MyForm.Width;
if MyForm.Left < 0 then MyForm.Left := 0;
if (MyForm.Top + LogoAppForm.Height) > screen.DesktopHeight then
MyForm.Top := screen.DesktopHeight - LogoAppForm.Height;
if MyForm.Top < 0 then MyFormTop := 0;
这工作正常,除了它没有考虑通常(但并非总是)位于桌面底部的任务栏。因此,如果任务栏挡住了我的应用程序窗口,我的应用程序就会被遮挡。
如何获取不包括任务栏位置的屏幕可用位置和大小设置?
With Screen.DesktopHeight and Screen.DesktopWidth, I can get the size of the "virtual" desktop and I believe that works for one or multiple monitors.
I save the position (top and left) and size (height and width) of my application into the registry when it closes. When it opens, I want to ensure it is entirely visible, since it may have been moved partly outside the visible area, or the screen size may have changed for example via screen settings or removal of a monitor.
I can basically do it with this code:
if MyForm.Width > screen.DesktopWidth then
MyForm.Width := screen.DesktopWidth;
if MyForm.Height > screen.DesktopHeight then
MyForm.Height := screen.DesktopHeight;
if (MyForm.Left + MyForm.Width) > screen.DesktopWidth then
MyForm.Left := screen.DesktopWidth - MyForm.Width;
if MyForm.Left < 0 then MyForm.Left := 0;
if (MyForm.Top + LogoAppForm.Height) > screen.DesktopHeight then
MyForm.Top := screen.DesktopHeight - LogoAppForm.Height;
if MyForm.Top < 0 then MyFormTop := 0;
This works okay, except it does not take into account the taskbar that is usually (but not always) at the bottom of the desktop. So if the taskbar is in the way of my application's window, my application gets obscured.
How can I get the usable position and size settings of the screen that exclude the location of the taskbar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定 Windows 是否真的按照您想象的方式工作 - 我有多个显示器,但它们的高度不同 - 因此我的桌面在所有显示器上的高度并不相同。
我所做的是使用 Screen.MonitorCount 和 Screen.Monitors 数组来计算出哪个监视器包含大部分窗口,然后在该屏幕上找到合适的矩形。 TMonitor 的 WorkareaRect 属性为您提供特定监视器上工作区域的边界,其中不包括任何任务栏或工具栏。
I'm not sure that Windows really works the way you think it does - I have multiple monitors, but they don't have the same height - so my desktop doesn't have a uniform height across all monitors.
What I do is to use Screen.MonitorCount and the Screen.Monitors array to work out which monitor contains most of the window and then find a suitable rectangle on that screen. The WorkareaRect property of TMonitor gives you the bounds of the working area on a particular monitor, which excludes any taskbars or toolbars.
您应该使用 Screen.WorkArea* 属性:
或
You should use Screen.WorkArea* properties:
or
要确定当前窗体的工作区域,请使用 Monitor.WorkareaRect。例如,
将表单大小设置为最大区域而不最大化。
您还应该查看 TCustomForm.MakeFullyVisible 方法。
来自 D2006 帮助:
“MakeFullyVisible 检查表单是否完全适合指定的显示器。如果不适合,它会重新定位表单以使其适合(如果可能)。”
To determine the work area for the current form, use Monitor.WorkareaRect. e.g.
to set the form size to the maximum area without maximising it.
You should also look at the TCustomForm.MakeFullyVisible method.
From D2006 help:
"MakeFullyVisible checks whether the form fits entirely on the specified monitor. If not, it repositions the form so that it fits, if possible."