Delphi中如何获取屏幕的可用坐标

发布于 2024-08-04 22:52:37 字数 870 浏览 6 评论 0原文

通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花间憩 2024-08-11 22:52:37

我不确定 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.

也只是曾经 2024-08-11 22:52:37

您应该使用 Screen.WorkArea* 属性:

  Screen.WorkAreaRect
  Screen.WorkAreaHeight
  Screen.WorkAreaLeft
  Screen.WorkAreaTop
  Screen.WorkAreaWidth

Screen.Monitors[I].WorkareaRect

You should use Screen.WorkArea* properties:

  Screen.WorkAreaRect
  Screen.WorkAreaHeight
  Screen.WorkAreaLeft
  Screen.WorkAreaTop
  Screen.WorkAreaWidth

or

Screen.Monitors[I].WorkareaRect
是你 2024-08-11 22:52:37

要确定当前窗体的工作区域,请使用 Monitor.WorkareaRect。例如,

BoundsRect := Monitor.WorkareaRect;

将表单大小设置为最大区域而不最大化。

您还应该查看 TCustomForm.MakeFullyVisible 方法。

来自 D2006 帮助:

“MakeFullyVisible 检查表单是否完全适合指定的显示器。如果不适合,它会重新定位表单以使其适合(如果可能)。”

To determine the work area for the current form, use Monitor.WorkareaRect. e.g.

BoundsRect := Monitor.WorkareaRect;

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."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文