当前位置:网站首页>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")
说明,参考的网络上其他大佬的代码,写文章时,找不到连接了,还请原谅,对代码进行了更改,主要是解决了调用窗口时,程序不存在,或者程序当前是最小化状态,从最小化状态重新返回桌面,仅供参考
边栏推荐
- flume 190 INSTALL
- Blender camera surround motion, animation rendering, video synthesis
- Pytest framework implements pre post
- [unreal] animation notes of the scene
- flink 提交程序
- MySQL -- time zone / connector / driver type
- [unity3d] cannot correctly obtain the attribute value of recttransform, resulting in calculation error
- 01-spooldir
- This article takes you to learn in detail what is fiber to home FTTH
- Blender海洋制作
猜你喜欢
Solutions to a series of problems in sqoop job creation
Blender海洋制作
sqoop创建job出现的一系列问题解决方法
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
Basic notes of illusory AI blueprint (10000 words)
Ue5 - ai Pursuit (Blueprint, Behavior tree)
Blender camera surround motion, animation rendering, video synthesis
[jetbrain rider] an exception occurred in the construction project: the imported project "d:\visualstudio2017\ide\msbuild\15.0\bin\roslyn\microsoft.csh" was not found
Blender ocean production
Remember the use of add method once
随机推荐
[pit avoidance guide] pit encountered using ugui: the text component cannot indent the first line by two spaces
KS009基于SSH实现宠物管理系统
AutoCAD - layer Linetype
Ue5 - AI pursuit (blueprint, behavior tree)
Following nym, the new project Galaxy token announced by coinlist is gal
pytest学习--base
Project practice, redis cluster technology learning (13)
Test -- Summary of interview questions
[Lua] summary of common knowledge points (including common interview sites)
allure--常用配置项
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
UE4 night lighting notes
sqoop创建job出现的一系列问题解决方法
Commutateur Multi - lentilles Blender
flume 190 INSTALL
Stm32 et développement de moteurs (système supérieur)
Basic usage of mock server
[unity3d] nested use layout group to make scroll view with dynamic sub object height
01安装虚拟机
测试--面试题总结