当前位置:网站首页>Pywin32打开指定窗口
Pywin32打开指定窗口
2022-07-02 06:40:00 【蜗牛速度在更新】
def open_app():
try:
win32api.ShellExecute(0, 'open', self.app_path, '', '', 1)
except:
print(f'Program can not be opened...')
def get_windows(windowsname, filename):
# 获取窗口句柄
handle = win32gui.FindWindow(None, os.path.basename(windowsname).split('.')[0])
# 判断该应用是否打开,如果没有打开,没有打开handle为0
if handle == 0:
print('程序未打开'.center(50, '='))
open_app(windowsname)
time.sleep(2)
# app 打开以后,重新给handle赋值
handle = win32gui.FindWindow(None, os.path.basename(windowsname).split('.')[0])
# 应用最小化时,使用该指令可以让该应用重新最大化-->该语句非常重要
win32gui.ShowWindow(handle, win32con.SW_MAXIMIZE)
## 将窗口放在前台,并激活该窗口(窗口不能最小化)
win32gui.SetForegroundWindow(handle)
time.sleep(1)
# 获取窗口DC
hdDC = win32gui.GetWindowDC(handle)
# 根据句柄创建一个DC
newhdDC = win32ui.CreateDCFromHandle(hdDC)
# 创建一个兼容设备内存的DC
saveDC = newhdDC.CreateCompatibleDC()
# 创建bitmap保存图片
saveBitmap = win32ui.CreateBitmap()
# 获取窗口的位置信息
left, top, right, bottom = win32gui.GetWindowRect(handle)
# 窗口长宽
width = right - left
height = bottom - top
# bitmap初始化
saveBitmap.CreateCompatibleBitmap(newhdDC, width, height)
saveDC.SelectObject(saveBitmap)
saveDC.BitBlt((0, 0), (width, height), newhdDC, (0, 0), win32con.SRCCOPY)
saveBitmap.SaveBitmapFile(saveDC, filename)
get_windows(r"C:\Users\Administrator\Desktop\PyWin32.chm", "截图.png")
说明,参考的网络上其他大佬的代码,写文章时,找不到连接了,还请原谅,对代码进行了更改,主要是解决了调用窗口时,程序不存在,或者程序当前是最小化状态,从最小化状态重新返回桌面,仅供参考
边栏推荐
- Message mechanism -- getting to know messages and message queues for the first time
- Blender stone carving
- [MySQL] an exception occurs when connecting to MySQL: connection must be valid and open
- Delivery mode design of Spartacus UI of SAP e-commerce cloud
- This article takes you to learn in detail what is fiber to home FTTH
- 01安装虚拟机
- How does {} prevent SQL injection? What is its underlying principle?
- [200 Shengxin literatures] 96 joint biomarkers of immune checkpoint inhibitor response in advanced solid tumors
- Project practice, redis cluster technology learning (6)
- [ue5] two implementation methods of AI random roaming blueprint (role blueprint and behavior tree)
猜你喜欢

Pytest framework implements pre post

【避坑指南】使用UGUI遇到的坑:Text组件无法首行缩进两格

VLAN experiment

Basic notes of illusory AI blueprint (10000 words)

Test -- Summary of interview questions

【MySQL】连接MySQL时出现异常:Connection must be valid and open
![[Fantasy 4] introduction and use of UMG components (under update...)](/img/33/7e3b74192b7fd73d5145f85e061d1f.png)
[Fantasy 4] introduction and use of UMG components (under update...)

allure--常用配置项

ue虚幻引擎程序化植物生成器设置——如何快速生成大片森林

Aiphacode is not a substitute for programmers, but a tool for developers
随机推荐
2021-10-02
Blender海洋制作
2021-10-04
07数据导入Sqoop
2021-10-04
Basic usage of mock server
ERROR 1118 (42000): Row size too large (> 8126)
判断数组中是否存在重复元素
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
VLAN experiment
Webui automated learning
2021-09-12
Configuration programmée du générateur de plantes du moteur illusoire UE - - Comment générer rapidement une grande forêt
2021-09-12
Message mechanism -- getting to know messages and message queues for the first time
[200 Shengxin literatures] 95 multiomics exploration TNBC
Flink实时计算topN热榜
UE4 night lighting notes
Transport Optimization abstraction
Edge computing accelerates live video scenes: clearer, smoother, and more real-time