VFP 截取被遮掩的窗口图像!

首先声明,以下代码是学习 dkfdtf 老师的原截图代码基础上 加入 PrintWindow 这个API 而成的,在这里再次多谢 dkfdtf    老师!

DECLARE INTEGER FindWindow IN user32;
    STRING lpClassName,;
    STRING lpWindowName

DECLARE INTEGER GetDC IN user32 INTEGER hwnd   

Declare SHORT GetWindowRect IN user32 INTEGER hwnd, STRING @ lpRect
    DECLARE Long GetWindowDC IN WIN32API ;
        Long hWnd
    DECLARE Long ReleaseDC IN WIN32API ;
        Long hWnd, Long hDc
    DECLARE Long CreateCompatibleDC IN WIN32API ;
        Long hDc
    DECLARE Long DeleteDC IN WIN32API ;
        Long hDc
    DECLARE Long CreateCompatibleBitmap IN WIN32API ;
        Long hDc, Long nWidth, Long nHeight
    DECLARE Long BitBlt IN WIN32API ;
        Long hDestDC, ;
        Long nXDest, Long nYDest, Long nWidth, Long nHeight, ;
        Long hSrcDC, ;
        Long nXSrc, Long nYSrc, ;
        Long dwRop
    DECLARE Long SelectObject IN WIN32API ;
        Long hDc, Long hObject
    DECLARE Long DeleteObject IN WIN32API ;
        Long hObject
    
    DECLARE Long GdiplusStartup IN gdiplus ;
        Long @ token, String @ inputbuf, Long @ outputbuf
    DECLARE Long GdiplusShutdown IN gdiplus ;
        Long token
    DECLARE Long GdipCreateBitmapFromHBITMAP IN gdiplus ;
        Long hbitmap, Long hpalette, Long @ hGpBitmap
    DECLARE Long GdipDisposeImage IN gdiplus ;
        Long image
    DECLARE Long GdipSaveImageToFile IN gdiplus ;
        Long nImage, String FileName, ;
        String clsIdEncoder, Long encoderParams
**以上的API 不是全部用上 只是复制进来,懒得删除了!

DECLARE INTEGER PrintWindow IN user32 INTEGER,INTEGER ,INTEGER

#define SRCCOPY 0x00CC0020

LOCAL cFileExtName, cEncoder, iInputBuf, iResult
LOCAL hDesktopDc, hVDc, hBitmap, hToken, hGdipBitmap
m.hWnd=FindWindow("Notepad",NULL) &&截取一个文本窗口试一试
tcFile=LOWER('I:\TUT.JPG') &&jpg 格式图像
m.cFileExtName =LOWER( JUSTEXT( m.tcFile ))
lpRect = REPLI (Chr(0), 16)
GetWindowRect (m.hWnd, @lpRect)
lnWidth=ctob(SUBS(lpRect,9,4),'4rs')-ctob(SUBS(lpRect,1,4),'4rs')&&窗口宽度
lnHeight=ctob(SUBS(lpRect,13,4),'4rs')-ctob(SUBS(lpRect,5,4),'4rs')&&窗口高度

m.hDesktopDc = GetDC( m.hWnd )
m.hVdc = CreateCompatibleDC( m.hDesktopDc )
m.hBitmap = CreateCompatibleBitmap( m.hDesktopDc,lnWidth,lnHeight )
ABC=SelectObject( m.hVdc, m.hBitmap )
PrintWindow(m.hWnd,m.hVdc,0)
SelectObject( m.hVdc, ABC )
*BitBlt( m.hVdc, 0,0,lnWidth,lnHeight, m.hDesktopDc,0,0, SRCCOPY )

m.hToken = 0
m.iInputBuf = 0h01 + REPLICATE( CHR(0),15 )
m.iResult = -1
IF ( 0 == GdiplusStartup( @ m.hToken, @ m.iInputBuf, 0 ))
    m.hGdipBitmap = 0
    IF ( 0 == GdipCreateBitmapFromHBITMAP( ;
            m.hBitmap, 0, @ m.hGdipBitmap ))
        m.cEncoder = ICASE( ;
            'jpg' == m.cFileExtName, 0h01, ;
            'gif' == m.cFileExtName, 0h02, ;
            'tif' == m.cFileExtName, 0h05, ;
            'png' == m.cFileExtName, 0h06, 0h00 ) ;
            + 0hF47C55041AD3119A730000F81EF32E
        m.iResult = GdipSaveImageToFile( ;
            m.hGdipBitmap, ;
            STRCONV( m.tcFile+CHR(0), 5 ), ;
            m.cEncoder, 0 )
        GdipDisposeImage( m.hGdipBitmap )
    ENDIF
    GdiplusShutdown( m.hToken )
ENDIF

DeleteObject( m.hBitmap )
DeleteDC( m.hVdc )
ReleaseDC( 0, m.hDesktopDc )


我是测试成功的 !

 

 

你可能感兴趣的:(VFP 截取被遮掩的窗口图像!)