• 技术文章 >Python技术 >Python高级

    python中Pywin32库如何使用?

    十一十一2021-01-28 10:59:12原创9162

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

    Pywin32安装

    1

    pip install pywin32

    使用方法:

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

    实例代码:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    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库的基本用法到此就全部介绍完毕了,大家感兴趣的话,可以多多尝试学习下哦~

    专题推荐:pywin32库使用
    上一篇:python中pyqtgraph是什么? 下一篇:python创建多线程的两种方法

    相关文章推荐

    • Python tkinter库如何安装?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网