python中Pywin32库如何使用?

1,086次阅读
没有评论
python中Pywin32库如何使用?

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

Pywin32安装:

pip install pywin32

使用方法:

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

实例代码:

import win32gui,win32ui,win32con
def get_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")

输出结果:

关于Pywin32库的基本用法到此就全部介绍完毕了,大家感兴趣的话,可以多多尝试学习下哦~

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-07-14发表,共计969字。
新手QQ群:570568346,欢迎进群讨论 Python51学习