strive-sun

直接上代码:

import win32gui
import win32ui
from ctypes import windll
import Image

hwnd = win32gui.FindWindow(None, \'Calculator\')

# Change the line below depending on whether you want the whole window
# or just the client area. 
#left, top, right, bot = win32gui.GetClientRect(hwnd)
left, top, right, bot = win32gui.GetWindowRect(hwnd)
w = right - left
h = bot - top

hwndDC = win32gui.GetWindowDC(hwnd)
mfcDC  = win32ui.CreateDCFromHandle(hwndDC)
saveDC = mfcDC.CreateCompatibleDC()

saveBitMap = win32ui.CreateBitmap()
saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)

saveDC.SelectObject(saveBitMap)

# Change the line below depending on whether you want the whole window
# or just the client area. 
#result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 1)
result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 0)
print(result)

bmpinfo = saveBitMap.GetInfo()
bmpstr = saveBitMap.GetBitmapBits(True)

im = Image.frombuffer(
    \'RGB\',
    (bmpinfo[\'bmWidth\'], bmpinfo[\'bmHeight\']),
    bmpstr, \'raw\', \'BGRX\', 0, 1)

win32gui.DeleteObject(saveBitMap.GetHandle())
saveDC.DeleteDC()
mfcDC.DeleteDC()
win32gui.ReleaseDC(hwnd, hwndDC)

if result == 1:
    #PrintWindow Succeeded
    im.save("test.png")

对于其他应用程序,会发现复制的截图为黑色,解决方法请参阅另一个帖子:解决使用复制浏览器的屏幕截图出现黑色窗口的问题

分类:

技术点:

相关文章:

  • 2021-12-23
  • 2021-10-14
  • 2021-11-27
  • 2021-11-01
  • 2021-11-06
  • 2021-11-11
  • 2021-11-28
猜你喜欢
  • 2021-10-26
  • 2021-11-11
  • 2021-11-28
  • 2021-11-12
  • 2021-10-28
  • 2021-12-10
相关资源
相似解决方案