环境:windows python3
# -*- coding: UTF-8 -*- import time import os, win32gui, win32ui, win32con, win32api def window_capture(dpath,aname, srcbmp=[0, 0, None, None]): hwnd = 0 hwndDC = win32gui.GetWindowDC(hwnd) mfcDC=win32ui.CreateDCFromHandle(hwndDC) saveDC=mfcDC.CreateCompatibleDC() saveBitMap = win32ui.CreateBitmap() MoniterDev=win32api.EnumDisplayMonitors(None,None) w = MoniterDev[0][2][2] h = MoniterDev[0][2][3] if srcbmp[2]==None or (srcbmp[0]+srcbmp[2]>w): srcbmp[2] = w if srcbmp[3]==None or (srcbmp[1]+srcbmp[3]>w): srcbmp[3] = h saveBitMap.CreateCompatibleBitmap(mfcDC, srcbmp[2], srcbmp[3]) saveDC.SelectObject(saveBitMap) #saveDC.BitBlt((0,0),(w, h) , mfcDC, (0,0), win32con.SRCCOPY) ddss = (srcbmp[2], srcbmp[3]) saveDC.BitBlt((0,0), ddss , mfcDC, (srcbmp[0], srcbmp[1]), win32con.SRCCOPY) bmpname=dpath+aname+'.bmp' saveBitMap.SaveBitmapFile(saveDC, bmpname) window_capture("E:\\pic\\", "1",[110, 540, 1515, 375])