欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > python操作桌面软件记录

python操作桌面软件记录

2024/10/25 19:36:46 来源:https://blog.csdn.net/weixin_42771529/article/details/140107252  浏览:    关键词:python操作桌面软件记录
# import win32con
# import win32api
# import win32gui
#
# # 查找目标窗口
# hwnd = win32gui.FindWindow("Chrome_WidgetWin_0", None)
# print(hwnd)
#
# # 设置窗口为最前端并置顶
# win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE)
#
# # 设置为活动窗口
# win32gui.SetActiveWindow(hwnd)
#
# # 获取并打印窗口矩形区域位置
# rect = win32gui.GetWindowRect(hwnd)
# print(rect)
#
# # 移动并调整窗口大小
# win32gui.MoveWindow(hwnd, 0, 0, 440, 900, True)
#
# # 确保窗口获取焦点(在某些情况下可能需要)
# win32gui.SetForegroundWindow(hwnd)
#
#
import timeimport win32con
import win32api
import win32guidef move_mouse(hwnd, x, y):"""Move the mouse cursor to the specified coordinates within the given window.:param hwnd: The handle of the window.:param x: The x-coordinate to move the mouse to.:param y: The y-coordinate to move the mouse to."""# Get the window's client area rectangle# rect = win32gui.GetClientRect(hwnd)# Map client coordinates to screen coordinatespoint = win32gui.ClientToScreen(hwnd, (x, y))# Move the mouse cursor to the mapped coordinateswin32api.SetCursorPos((point[0], point[1]))# def scroll_down(hwnd, scroll_times):
#     mouse_wheel_message = win32con.WM_MOUSEWHEEL
#     # Each scroll is 120 units, adjust for each scroll event
#     w_param = 120 << 16  # Initial delta for a single scroll down
#
#     # Loop to send the scroll message for the specified number of times
#     for _ in range(scroll_times):
#         # Get the current cursor position within the window's client area
#         client_point = win32gui.ScreenToClient(hwnd, win32api.GetCursorPos())
#         lParam = win32api.MAKELONG(client_point[0], client_point[1])
#
#         # Send the message to the window
#         win32api.PostMessage(hwnd, mouse_wheel_message, w_param, lParam)
#         # Increment the wheel delta for the next iteration to simulate continuous scrolling
#         w_param += 120 << 16# 查找目标窗口
hwnd = win32gui.FindWindow("Chrome_WidgetWin_0", None)
print(hwnd)# 设置窗口为最前端并置顶
win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE)# 设置为活动窗口
win32gui.SetActiveWindow(hwnd)# 获取并打印窗口矩形区域位置
rect = win32gui.GetWindowRect(hwnd)
print(rect)# 移动并调整窗口大小
win32gui.MoveWindow(hwnd, 0, 0, 440, 900, True)
# 确保窗口获取焦点(在某些情况下可能需要)
win32gui.SetForegroundWindow(hwnd)
# # 在指定坐标处模拟向下滚动
# scroll_down(hwnd,330, 600)# # 确保窗口获取焦点(在某些情况下可能需要)
# win32gui.SetForegroundWindow(hwnd)
#
# win32api.SendMessage(hwnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON,win32api.MAKELONG(330, 600) )
# win32api.SendMessage(hwnd, win32con.WM_LBUTTONUP, 0,win32api.MAKELONG(330, 600) )
#
# 将鼠标移动到指定坐标,例如窗口内部的(100, 200)位置
move_mouse(hwnd, 50, 600)
# time.sleep(10)import ctypes
from ctypes import wintypes
import time# 定义必要的常量和结构体
PUL = ctypes.POINTER(ctypes.c_ulong)
class MOUSEINPUT(ctypes.Structure):_fields_ = [("dx", wintypes.LONG),("dy", wintypes.LONG),("mouseData", wintypes.DWORD),("dwFlags", wintypes.DWORD),("time", wintypes.DWORD),("dwExtraInfo", PUL)]class INPUT(ctypes.Structure):_fields_ = [("type", wintypes.DWORD),("mi", MOUSEINPUT)]# 定义SendInput函数原型
SendInput = ctypes.windll.user32.SendInput# 定义常量
INPUT_MOUSE = 0
MOUSEEVENTF_WHEEL = 0x0800
WHEEL_DELTA = 120# 获取当前线程的ID,用于dwExtraInfo
GetCurrentThreadId = ctypes.windll.kernel32.GetCurrentThreadId
extra_info = ctypes.c_ulong()def scroll_down(hwnd, scroll_lines):"""Simulate scrolling down by the specified number of lines."""# Convert lines to wheel delta units (WHEEL_DELTA is usually 120 per notch)mouse_data = -WHEEL_DELTA * scroll_lines  # Negative for scrolling down# Prepare MOUSEINPUT structuremouse_input = MOUSEINPUT(dx=0, dy=0, mouseData=mouse_data, dwFlags=MOUSEEVENTF_WHEEL, time=0, dwExtraInfo=ctypes.cast(GetCurrentThreadId(), PUL))# Prepare INPUT structureinput_structure = INPUT(type=INPUT_MOUSE, mi=mouse_input)# Set focus to the target windowwin32gui.SetForegroundWindow(hwnd)# Send the scroll eventSendInput(1, ctypes.byref(input_structure), ctypes.sizeof(INPUT))scroll_down(hwnd, 50)  # 模拟在hwnd窗口下,向下滚动100行
time.sleep(1)move_mouse(hwnd, 300, 600)
scroll_down(hwnd, 50)  # 模拟在hwnd窗口下,向下滚动100行

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com