Screen capturing on Windows

Roger Upole rupole at hotmail.com
Wed Jun 14 21:26:07 EDT 2006


"Rune Strand" <rune.strand at gmail.com> wrote in message news:1150192149.757557.163820 at y43g2000cwc.googlegroups.com...
>
> Is it possible by use of pyWin32 or ctypes to make a screen capture of
> an inactive, or a hidden window if the hwnd/WindowName/ClassName is
> known? I've seen dedicated screen capture software do this. While
> PIL.ImageGrab.grab() is excellent, it will only capture the foreground
> of the desktop. I've tried for hours, but I soon get helplessly lost in
> the labyrinths of the Win32API.
>

This will restore a minimized window, bring it to the top and save a bmp.

import time
import win32gui, win32ui, win32con, win32api

def window_capture(window_title):
    hwnd=win32gui.FindWindow(None, window_title)
    if not hwnd:
        raise 'Window not found'

    print hwnd
    win32gui.ShowWindow(hwnd,win32con.SW_RESTORE)
    win32gui.SetForegroundWindow(hwnd)
    time.sleep(0.1)
    l,t,r,b=win32gui.GetWindowRect(hwnd)
    h=b-t
    w=r-l
    hwndDC = win32gui.GetWindowDC(hwnd)
    mfcDC=win32ui.CreateDCFromHandle(hwndDC)
    saveDC=mfcDC.CreateCompatibleDC()
    saveBitMap = win32ui.CreateBitmap()
    saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
    saveDC.SelectObject(saveBitMap)
    saveDC.BitBlt((0,0),(w, h) , mfcDC, (0,0), win32con.SRCCOPY)
    temp_dir=win32api.GetTempPath()
    bmpname=win32api.GetTempFileName(temp_dir,'wc')[0]+'.bmp'
    saveBitMap.SaveBitmapFile(saveDC, bmpname)
    return bmpname

      Roger





More information about the Python-list mailing list