win32Test.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. # def arrange_windows_6():
  45. # arrange_windows()
  46. # Example usage:
  47. # Assume chrome_hwnds is a list of Chrome window handles
  48. # win32gui.SetWindowPos(chromeHwnd,win32con.HWND_TOP,0,0,800,800,0)
  49. # win32gui.ShowWindow(chromeHwnd, win32con.SW_MAXIMIZE) # 确保窗口处于正常状态
  50. # win32gui.SetForegroundWindow(chromeHwnd)
  51. # 获取当前鼠标位置
  52. # original_position = target.position
  53. #
  54. # # 移动鼠标到新位置(相对当前位置)
  55. # new_position = (original_position[0] + 100, original_position[1] + 100)
  56. # target.position = new_position
  57. #
  58. # # 可以使用动作链进行平滑的移动
  59. # target.move(0, 1)
  60. #
  61. # # 休眠几秒钟,你可以根据需要调整
  62. # time.sleep(2)
  63. # target.position = (1,1)