win32Test.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import math
  2. import time
  3. import win32api
  4. import win32con
  5. import win32gui
  6. from pynput.mouse import Controller
  7. target = Controller()
  8. def get_all_windows():
  9. windows = []
  10. win32gui.EnumWindows(lambda hwnd, windows: windows.append(hwnd), windows)
  11. return windows
  12. def get_window_title(hwnd):
  13. return win32gui.GetWindowText(hwnd)
  14. def find_chrome():
  15. windows = get_all_windows()
  16. target = []
  17. for hwnd in windows:
  18. title = get_window_title(hwnd)
  19. if "Google Chrome" in title:
  20. target.append(hwnd)
  21. return target
  22. def get_screen_resolution():
  23. return win32api.GetSystemMetrics(win32con.SM_CXSCREEN), win32api.GetSystemMetrics(win32con.SM_CYSCREEN)
  24. def arrange_windows():
  25. window_handles = find_chrome()
  26. num_windows = len(window_handles)
  27. middle_point = []
  28. screen_width, screen_height = get_screen_resolution()
  29. if num_windows == 0:
  30. return
  31. base_width = screen_width // (num_windows // 2)
  32. base_height = screen_height // 2 - 5
  33. for i, hwnd in enumerate(window_handles):
  34. y = 0
  35. x = i * base_width
  36. if i > 2:
  37. y = base_height - 1
  38. x = (i - 3) * base_width
  39. middle_point.append((x + math.ceil(base_width / 2), y + math.ceil(base_height / 2)))
  40. win32gui.SetWindowPos(hwnd, win32con.HWND_TOP, x, y, base_width, base_height, 0)
  41. win32gui.ShowWindow(hwnd, win32con.SW_SHOWNORMAL)
  42. win32gui.SetForegroundWindow(hwnd)
  43. return middle_point, base_width, base_height
  44. # arrange_windows()
  45. # Example usage:
  46. # Assume chrome_hwnds is a list of Chrome window handles
  47. # win32gui.SetWindowPos(chromeHwnd,win32con.HWND_TOP,0,0,800,800,0)
  48. # win32gui.ShowWindow(chromeHwnd, win32con.SW_MAXIMIZE) # 确保窗口处于正常状态
  49. # win32gui.SetForegroundWindow(chromeHwnd)
  50. # 获取当前鼠标位置
  51. # original_position = target.position
  52. #
  53. # # 移动鼠标到新位置(相对当前位置)
  54. # new_position = (original_position[0] + 100, original_position[1] + 100)
  55. # target.position = new_position
  56. #
  57. # # 可以使用动作链进行平滑的移动
  58. # target.move(0, 1)
  59. #
  60. # # 休眠几秒钟,你可以根据需要调整
  61. # time.sleep(2)
  62. # target.position = (1,1)