python uiautomation:给定窗口控件名称,然后移动窗口到指定位置

给定窗口控件名称,然后移动窗口到指定位置

import uiautomation as aui # VERSION = "2.0.18"


def move_window(name: str, point: Union[Tuple, None] = None, **kwargs) -> bool:
    """

    Args:
        name: 窗口控件名称
        point: (0,0) 移动的坐标点(窗口的左上角) #优先级高于位置参数
        **kwargs:
            right_up: bool
            right_bottom: bool
            left_up: bool
            left_bottom:  bool
            middle: bool
            # 同时给多个的情况下,以第一个位置参数为准

    Returns: bool 移动成功返回True

    Examples:
          move_window(name="xx", point=(1, 1)) =》将窗口xx 移动到 (1,1)
          move_window(name="xx",right_bottom=True) => 将窗口xx 移动到右下角
          move_window(name="xx", middle=True, right_up=True, point=(0, 0)) => 优先级 point > middle > right_up

    """
    right_up = kwargs.get("right_up", None)
    right_bottom = kwargs.get("right_bottom", None)
    left_up = kwargs.get("left_up", None)
    left_bottom = kwargs.get("left_bottom", None)
    middle = kwargs.get("middle", None)

    target_window = aui.WindowControl(Name=name)

    screen_w, screen_h = aui.GetScreenSize()
    # 获取窗口的位置和大小信息
    rect = target_window.BoundingRectangle
    window_width = rect.right - rect.left
    window_height = rect.bottom - rect.top
    # 输出窗口的位置和大小信息
    right_up = (screen_w - window_width, 0) if right_up else None  # 右上角
    right_bottom = (screen_w - window_width, screen_h - window_height) if right_bottom else None  # 右下角
    left_up = (0, 0) if left_up else None  # 左上
    left_bottom = (0, screen_h - window_height) if left_bottom else None  # 左下角
    middle = ((screen_w - window_width) // 2, (screen_h - window_height) // 2) if middle else None

    # next内置函数只能接受位置参数,不能接受关键字参数
    position = next(filter(lambda x: x, [right_up, right_bottom, left_bottom, left_up, middle]), None)
    if point:
        target_window.MoveWindow(*point, width=rect.right - rect.left,
                                 height=rect.bottom - rect.top,
                                 repaint=False)
        target_window.SwitchToThisWindow()
        return True

    if position:
        target_window.MoveWindow(*position, width=rect.right - rect.left,
                                 height=rect.bottom - rect.top,
                                 repaint=False)
        target_window.SwitchToThisWindow()
        return True

    return False


# move_window()
if __name__ == '__main__':
    move_window(name="Downloads", middle=True, right_up=True, point=(0, 0))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值