【问题标题】:How to get the height of Windows Taskbar using Python/PyQT/Win32如何使用 Python/PyQT/Win32 获取 Windows 任务栏的高度
【发布时间】:2010-12-05 03:47:47
【问题描述】:

我正在尝试使我的 GUI 程序与 Windows 屏幕的右下角对齐。当任务栏不隐藏时,我的程序只会站在任务栏的顶部!

在使用 Python/PyQT/Win32 时,我该怎么做:

  1. 检查任务栏的自动隐藏功能是否开启
  2. 获取任务栏的高度

【问题讨论】:

标签: python windows winapi pyqt


【解决方案1】:

作为David Heffernan mentioned,您可以使用GetMonitorInfopywin32 来检索显示器大小。特别是,工作区将不包括任务栏的大小。

获取工作区大小(桌面减去任务栏):

from win32api import GetMonitorInfo, MonitorFromPoint

monitor_info = GetMonitorInfo(MonitorFromPoint((0,0)))
work_area = monitor_info.get("Work")
print("The work area size is {}x{}.".format(work_area[2], work_area[3]))

工作区大小为 1366x728。

获取任务栏高度:

from win32api import GetMonitorInfo, MonitorFromPoint

monitor_info = GetMonitorInfo(MonitorFromPoint((0,0)))
monitor_area = monitor_info.get("Monitor")
work_area = monitor_info.get("Work")
print("The taskbar height is {}.".format(monitor_area[3]-work_area[3]))

任务栏高度为40。

说明

首先,我们需要创建一个引用主监视器的句柄。主监视器always has its upper left corner at 0,0,所以我们可以使用:

primary_monitor = MonitorFromPoint((0,0))

我们使用GetMonitorInfo()检索有关监视器的信息。

monitor_info = GetMonitorInfo(primary_monitor)
# {'Monitor': (0, 0, 1366, 768), 'Work': (0, 0, 1366, 728), 'Flags': 1, 'Device': '\\\\.\\DISPLAY1'}

监视器信息以dict 形式返回。前两个条目将监视器大小和工作区大小表示为元组(x 位置、y 位置、高度、宽度)。

work_area = monitor_info.get("Work")
# (0, 0, 1366, 728)

【讨论】:

    【解决方案2】:

    我认为您需要致电GetMonitorInfo 以获取感兴趣的监视器。然后您需要从MONITORINFO.rcWork 中读取工作区。这将排除为任务栏保留的监视器的任何部分以及任何其他保留区域。

    我认为您不必担心自动隐藏,因为 GetMonitorInfo 应该考虑到这一点。换句话说,当启用自动隐藏时,工作区域将等于监视器区域。

    【讨论】:

    • 非常感谢!但是我发现pywin32的用法和MSDN有点不一样;这是我的代码:screeninfo = win32api.GetMonitorInfo(1)
    • @good man 显然你需要在原始 Win32 和 pywin32 之间进行映射,但原理和底层 API 调用肯定是相同的
    • 你能举个例子说明如何在 Python 中使用GetMonitorInfo 吗?
    【解决方案3】:

    您可以使用QDesktopWidget 检索有关系统屏幕的信息,并从总屏幕区域中减去工作区域。

    import sys
    from PyQt5.QtWidgets import QApplication
    
    app = QApplication(sys.argv)
    dw = app.desktop()  # dw = QDesktopWidget() also works if app is created
    taskbar_height = dw.screenGeometry().height() - dw.availableGeometry().height()
    

    但是,如果任务栏位于屏幕的两侧,这将返回零,这并不是特别有用。要解决此问题,请找出screenGeometry()availableGeometry() 之间的差异以找出任务栏(以及任何其他保留空间)的大小。

    当任务栏设置为自动隐藏时,可用的几何图形不知道任务栏的大小。

    【讨论】:

      猜你喜欢
      • 2016-02-27
      • 1970-01-01
      • 1970-01-01
      • 2010-12-16
      • 2013-07-10
      • 1970-01-01
      • 2012-09-08
      • 2020-01-26
      相关资源
      最近更新 更多