win32操作windows应用(未完成)

win32 脚本制作

一 、获取窗口句柄

首先获取句柄,其次扫描组件,然后对按钮和文本进行操作

安装依赖

复制代码
pip install pywin32 -i https://pypi.doubanio.com/simple

扫描全部的句柄

复制代码
import win32gui
 
# GetDesktopWindow 获得代表整个屏幕的一个窗口(桌面窗口)句柄
hd = win32gui.GetDesktopWindow()

# 获取所有子窗口
hwndChildList = []
win32gui.EnumChildWindows(hd, lambda hwnd, param: param.append(hwnd), hwndChildList)

for hwnd in hwndChildList:
    print("句柄:", hwnd, "标题:", win32gui.GetWindowText(hwnd))
    # f.write("句柄:" + str(hwnd) + " 标题:" + win32gui.GetWindowText(hwnd) + '\n'

将句柄写入文本

复制代码
lst = [1, 2, 3]
str_lst = [str(item) for item in lst]

# 假设有一个列表
my_list = ['这', '是', '一个', '测试']
 
# 将列表中的每个元素转换为字符串,并用换行符连接
list_as_string = '\n'.join(my_list)
 
# 打开文件进行写入
with open('output.txt', 'w', encoding='utf-8') as file:
    file.write(list_as_string)

最终写入

复制代码
def find_all_window():
    hd = win32gui.GetDesktopWindow()
    # 获取所有子窗口
    hwnd_child_list = []
    win32gui.EnumChildWindows(hd, lambda hwnd, param: param.append(hwnd), hwnd_child_list)
    str_list = []
    for hwnd in hwnd_child_list:
        print("句柄:", hwnd, "标题:", win32gui.GetWindowText(hwnd))
        str_list.append("句柄:" + str(hwnd)+"标题:" + win32gui.GetWindowText(hwnd))
        # f.write("句柄:" + str(hwnd) + " 标题:" + win32gui.GetWindowText(hwnd) + '\n'
    # 将句柄生成文件

    list_as_string = '\n'.join(str_list)
    # 打开文件进行写入
    with open('output.txt', 'w', encoding='utf-8') as file:
        file.write(list_as_string)

在文件中查询到

复制代码
句柄:267410标题:
句柄:1905680标题:
句柄:1512536标题:Import Excel File V1.0
句柄:332172标题:
句柄:70010标题:
句柄:70002标题:CWebviewHostWnd
句柄:70004标题:
句柄:70018标题:Chrome Legacy Window



其中Import Excel File V1.0的句柄为
句柄:1512536标题:Import Excel File V1.0

使用句柄查找应用窗口, 并前置

复制代码
# 将查询的窗口前置,handle是句柄
def preposition_windows(handle):
    hwnd = win32gui.FindWindow(0, win32gui.GetWindowText(handle))  # 寻找窗口
    if not hwnd:
        print("找不到该窗口")
    else:
        win32gui.SetForegroundWindow(hwnd)  # 前置窗口

完整代码

复制代码
import win32gui
import win32con
import pyautogui
from pynput import keyboard, mouse
from loguru import logger
from threading import Thread
import time
import re
from openpyxl import load_workbook
from openpyxl import Workbook
from openpyxl import styles
from openpyxl.styles import *
import pandas as pd
import string
import re
import os
import datetime


def find_all_window():
    hd = win32gui.GetDesktopWindow()
    # 获取所有子窗口
    hwnd_child_list = []
    win32gui.EnumChildWindows(hd, lambda hwnd, param: param.append(hwnd), hwnd_child_list)
    str_list = []
    for hwnd in hwnd_child_list:
        print("句柄:", hwnd, "标题:", win32gui.GetWindowText(hwnd))
        str_list.append("句柄:" + str(hwnd)+"标题:" + win32gui.GetWindowText(hwnd))
        # f.write("句柄:" + str(hwnd) + " 标题:" + win32gui.GetWindowText(hwnd) + '\n'
    # 将句柄生成文件

    list_as_string = '\n'.join(str_list)
    # 打开文件进行写入
    with open('output.txt', 'w', encoding='utf-8') as file:
        file.write(list_as_string)


# 将查询的窗口前置,handle是句柄
def preposition_windows(handle):
    hwnd = win32gui.FindWindow(0, win32gui.GetWindowText(handle))  # 寻找窗口
    if not hwnd:
        print("找不到该窗口")
    else:
        win32gui.SetForegroundWindow(hwnd)  # 前置窗口


# 通过名字查询句柄
# hwnd = win32gui.FindWindow(None, "窗口标题")
if __name__ == '__main__':
    # 查询所有窗口,当名字查不到句柄时,使用
    # find_all_window()

    # 函数
    hwnd = win32gui.FindWindow(None, "frm_ImportExcel")
    # 显示窗口
    # win32gui.ShowWindow(hwnd, win32con.SW_SHOW)
    # win32gui.SetForegroundWindow(hwnd)  # 前置窗口
    handle = 1643806
    前置窗口
    preposition_windows(handle)

注意

应用名称和窗口名称不同

通过窗口名称返回句柄

复制代码
import win32gui
import win32con

if __name__ == '__main__':
    # 查询所有窗口,当名字查不到句柄时,使用
    # find_all_window()
    # 函数
    hwnd = win32gui.FindWindow(None, "frm_ImportExcel")
    # 显示窗口
    win32gui.ShowWindow(hwnd, win32con.SW_SHOW)
    win32gui.SetForegroundWindow(hwnd)  # 前置窗口
    print(hwnd)

二、查询窗口的控件

复制代码
#Only for Windows!!!!
import win32gui as wg #pip install pypiwin32
result = []
handle = wg.FindWindow(classname, name) #classname是窗口类名,name是窗口名,可以只写一个,另一个为None,也可以两个都写
def callback(handle, res):
    result.append(handle)
    return True
wg.EnumChildWindows(handle, callback, None)
#result就是控件名

获取树形控件的句柄

获取活动窗口的句柄

复制代码
# 获取当前活动窗口句柄
hParent = win32gui.GetForegroundWindow()

# 获取TreeView句柄
hTreeView = win32gui.FindWindowEx(hParent, 0, "SysTreeView", None)

# 获取选中项的标题
selectedIndex = win32gui.SendMessage(hTreeView, win32con.TVM_GETNEXTITEM, win32con.TVGN_CARET, 0)
title = win32gui.SendMessage(hTreeView, win32con.TVM_GETITEMTEXT, selectedIndex, buffer)

在这段代码中,selectedIndex是选中项的索引,title是选中项的标题。win32gui.SendMessage函数用于发送消息给TreeView控件,win32con.TVM_GETNEXTITEM和win32con.TVM_GETITEMTEXT是获取下一个项和获取项文本的消息常量。win32con.TVGN_CARET表示获取当前选中项的常量

通过spy++获取类名,然后通过

复制代码
# 获取TreeView类名获取句柄
hTreeView = win32gui.FindWindowEx(hParent, 0, "SysTreeView", None)

三、通过Send Message对不同控件进行操作

完整代码

复制代码
import win32gui
import win32con
import win32api
import pyautogui
from pynput import keyboard, mouse
from loguru import logger
from threading import Thread
import time
import re
from openpyxl import load_workbook
from openpyxl import Workbook
from openpyxl import styles
from openpyxl.styles import *
import pandas as pd
import string
import re
import os
import datetime
import time



def find_all_window():
    hd = win32gui.GetDesktopWindow()
    # 获取所有子窗口
    hwnd_child_list = []
    win32gui.EnumChildWindows(hd, lambda hwnd, param: param.append(hwnd), hwnd_child_list)
    str_list = []
    for hwnd in hwnd_child_list:
        print("句柄:", hwnd, "标题:", win32gui.GetWindowText(hwnd))
        str_list.append("句柄:" + str(hwnd) + "标题:" + win32gui.GetWindowText(hwnd))
        # f.write("句柄:" + str(hwnd) + " 标题:" + win32gui.GetWindowText(hwnd) + '\n'
    # 将句柄生成文件

    list_as_string = '\n'.join(str_list)
    # 打开文件进行写入
    with open('output.txt', 'w', encoding='utf-8') as file:
        file.write(list_as_string)


# 将查询的窗口前置,handle是句柄
def preposition_windows(handle):
    hwnd = win32gui.FindWindow(0, win32gui.GetWindowText(handle))  # 寻找窗口
    if not hwnd:
        print("找不到该窗口")
    else:
        win32gui.SetForegroundWindow(hwnd)  # 前置窗口


def find_control(handle):
    result = []

    # class_name是窗口类名,name是窗口名,可以只写一个,另一个为None,也可以两个都写
    # handle = wg.FindWindow(class_name, name)
    def callback(handle, res):
        result.append(str(handle) + ": " + win32gui.GetWindowText(handle))
        return True

    win32gui.EnumChildWindows(handle, callback, None)
    return result
    # result就是控件名


if __name__ == '__main__':
    # 查询所有窗口,当名字查不到句柄时,使用
    # find_all_window()
    # 函数
    hwnd = win32gui.FindWindow(None, "frm_ImportExcel")
    # 验证是否找到窗口
    win32gui.ShowWindow(hwnd, win32con.SW_SHOW)
    win32gui.SetForegroundWindow(hwnd)  # 前置窗口
    print(hwnd)
    # 查找矩形容器
    hTreeView = win32gui.FindWindowEx(hwnd, 0, "TRzSizePanel", None)
    # 查找panel中的树形组件
    hTreeView2 = win32gui.FindWindowEx(hTreeView, 0, "TRzTreeView", None)
    # 模拟点击,点开树形组件
    position = win32api.MAKELONG(0, 0)  # x,y为点击点相对于该窗口的坐标
    win32api.SendMessage(hTreeView2, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, position)  # 向窗口发送模拟鼠标点
    # win32api.SendMessage(hTreeView2, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, position)  # 向窗口发送模拟鼠标点
    # 睡眠60秒
    time.sleep(3)
    # 选中零售销售单,TreeView控制
    position = win32api.MAKELONG(154, -180)  # x,y为点击点相对于该窗口的坐标
    win32api.SendMessage(hTreeView2, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, position)  # 向窗口发送模拟鼠标点
	# 选中工具, ToolBar控制
    result = find_control(hTreeView2)
    list_as_string = '\n'.join(result)
    print(list_as_string)
相关推荐
AI街潜水的八角1 小时前
Python电脑屏幕&摄像头录制软件(提供源代码)
开发语言·python
hadage2331 小时前
--- git 的一些使用 ---
开发语言·git·python
百***86466 小时前
新版 WSL2 2.0 设置 Windows 和 WSL 镜像网络教程
windows
qq_479875436 小时前
X-Macros(1)
linux·服务器·windows
笨笨聊运维7 小时前
CentOS官方不维护版本,配置python升级方法,无损版
linux·python·centos
Gerardisite8 小时前
如何在微信个人号开发中有效管理API接口?
java·开发语言·python·微信·php
小毛驴8508 小时前
软件设计模式-装饰器模式
python·设计模式·装饰器模式
王 富贵8 小时前
Conda常用命令大全
windows·conda