python中Pywin32库如何使用?

Pywin32有是三种很重要的模块,分别是win32api、win32gui和win32con。能够实现访问windows的API。因为Windows是不允许程序直接访问硬件的,所以我们需要通过一个媒介,实现传递,这里就是我们经常能够用到的Pywin32模块,下面来给大家介绍基本的使用技巧,一起来详细了解下吧。

Pywin32安装

pipinstallpywin32

使用方法:

我们使用pywin32激活窗口并对截图。

实例代码:

importwin32gui,win32ui,win32con
defget_windows(windowsname,filename):
handle=win32gui.FindWindow(None,windowsname)
win32gui.SetForegroundWindow(handle)
hdDC=win32gui.GetWindowDC(handle)
newhdDC=win32ui.CreateDCFromHandle(hdDC)
saveDC=newhdDC.CreateCompatibleDC()
saveBitmap=win32ui.CreateBitmap()
left,top,right,bottom=win32gui.GetWindowRect(handle)
width=right-left
height=bottom-top
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("PyWin32","截图.png")

输出结果:

python中Pywin32库如何使用?

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。