给定窗口控件名称,然后移动窗口到指定位置
import uiautomation as aui
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
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
if __name__ == '__main__':
move_window(name="Downloads", middle=True, right_up=True, point=(0, 0))