visualtest/src/windows/windows_screenshot.c
/* Takes the screenshot of a window and saves it to a file. If only_client_area
   is true, then only the client area of the window is considered */
Takes the screenshot of a window and saves it to a file. If only_client_area
is true, then only the client area of the window is considered
Takes the screenshot of a window and saves it to a file . If only_client_area is true then only the client area of the window is considered
static int
ScreenshotWindow(HWND hwnd, char* filename, SDL_bool only_client_area)
{
    int width, height;
    RECT dimensions;
    HDC windowdc, capturedc;
    HBITMAP capturebitmap;
    HGDIOBJ select_success;
    BOOL blt_success;
    int return_code = 1;
    if(!filename)
    {
        SDLTest_LogError("filename argument cannot be NULL");
        return_code = 0;
        goto screenshotwindow_cleanup_generic;
    }
    if(!hwnd)
    {
        SDLTest_LogError("hwnd argument cannot be NULL");
        return_code = 0;
        goto screenshotwindow_cleanup_generic;
    }
    if(!GetWindowRect(hwnd, &dimensions))
    {
        LogLastError("GetWindowRect() failed");
        return_code = 0;
        goto screenshotwindow_cleanup_generic;
    }
    if(only_client_area)
    {
        RECT crect;
        if(!GetClientRect(hwnd, &crect))
        {
            SDLTest_LogError("GetClientRect() failed");
            return_code = 0;
            goto screenshotwindow_cleanup_generic;
        }
        width = crect.right;
        height = crect.bottom;
        windowdc = GetDC(hwnd);
        if(!windowdc)
        {
            SDLTest_LogError("GetDC() failed");
            return_code = 0;
            goto screenshotwindow_cleanup_generic;
        }
    }
    else
    {
        width = dimensions.right - dimensions.left;
        height = dimensions.bottom - dimensions.top;
        windowdc = GetWindowDC(hwnd);
        if(!windowdc)
        {
            SDLTest_LogError("GetWindowDC() failed");
            return_code = 0;
            goto screenshotwindow_cleanup_generic;
        }
    }
    capturedc = CreateCompatibleDC(windowdc);
    if(!capturedc)
    {
        SDLTest_LogError("CreateCompatibleDC() failed");
        return_code = 0;
        goto screenshotwindow_cleanup_windowdc;
    }
    capturebitmap = CreateCompatibleBitmap(windowdc, width, height);
    if(!capturebitmap)
    {
        SDLTest_LogError("CreateCompatibleBitmap() failed");
        return_code = 0;
        goto screenshotwindow_cleanup_capturedc;
    }
    select_success = SelectObject(capturedc, capturebitmap);
    if(!select_success || select_success == HGDI_ERROR)
    {
        SDLTest_LogError("SelectObject() failed");
        return_code = 0;
        goto screenshotwindow_cleanup_capturebitmap;
    }
    blt_success = BitBlt(capturedc, 0, 0, width, height, windowdc,
                         0, 0, SRCCOPY|CAPTUREBLT);
    if(!blt_success)
    {
        LogLastError("BitBlt() failed");
        return_code = 0;
        goto screenshotwindow_cleanup_capturebitmap;
    }
    if(!SaveBitmapToFile(windowdc, capturebitmap, filename))
    {
        SDLTest_LogError("SaveBitmapToFile() failed");
        return_code = 0;
        goto screenshotwindow_cleanup_capturebitmap;
    }
screenshotwindow_cleanup_capturebitmap:
    if(!DeleteObject(capturebitmap))
    {
        SDLTest_LogError("DeleteObjectFailed");
        return_code = 0;
    }
screenshotwindow_cleanup_capturedc:
    if(!DeleteDC(capturedc))
    {
        SDLTest_LogError("DeleteDC() failed");
        return_code = 0;
    }
screenshotwindow_cleanup_windowdc:
    if(!ReleaseDC(hwnd, windowdc))
    {
        SDLTest_LogError("ReleaseDC() failed");
        return_code = 0;;
    }
screenshotwindow_cleanup_generic:
    return return_code;
}
static int ScreenshotWindow ( HWND hwnd , char * filename , SDL_bool only_client_area ) { int width , height ; RECT dimensions ; HDC windowdc , capturedc ; HBITMAP capturebitmap ; HGDIOBJ select_success ; BOOL blt_success ; int return_code = 1 ; if ( ! filename ) { SDLTest_LogError ( " " ) ; return_code = 0 ; goto screenshotwindow_cleanup_generic ; } if ( ! hwnd ) { SDLTest_LogError ( " " ) ; return_code = 0 ; goto screenshotwindow_cleanup_generic ; } if ( ! GetWindowRect ( hwnd , & dimensions ) ) { LogLastError ( " " ) ; return_code = 0 ; goto screenshotwindow_cleanup_generic ; } if ( only_client_area ) { RECT crect ; if ( ! GetClientRect ( hwnd , & crect ) ) { SDLTest_LogError ( " " ) ; return_code = 0 ; goto screenshotwindow_cleanup_generic ; } width = crect . right ; height = crect . bottom ; windowdc = GetDC ( hwnd ) ; if ( ! windowdc ) { SDLTest_LogError ( " " ) ; return_code = 0 ; goto screenshotwindow_cleanup_generic ; } } else { width = dimensions . right - dimensions . left ; height = dimensions . bottom - dimensions . top ; windowdc = GetWindowDC ( hwnd ) ; if ( ! windowdc ) { SDLTest_LogError ( " " ) ; return_code = 0 ; goto screenshotwindow_cleanup_generic ; } } capturedc = CreateCompatibleDC ( windowdc ) ; if ( ! capturedc ) { SDLTest_LogError ( " " ) ; return_code = 0 ; goto screenshotwindow_cleanup_windowdc ; } capturebitmap = CreateCompatibleBitmap ( windowdc , width , height ) ; if ( ! capturebitmap ) { SDLTest_LogError ( " " ) ; return_code = 0 ; goto screenshotwindow_cleanup_capturedc ; } select_success = SelectObject ( capturedc , capturebitmap ) ; if ( ! select_success || select_success == HGDI_ERROR ) { SDLTest_LogError ( " " ) ; return_code = 0 ; goto screenshotwindow_cleanup_capturebitmap ; } blt_success = BitBlt ( capturedc , 0 , 0 , width , height , windowdc , 0 , 0 , SRCCOPY | CAPTUREBLT ) ; if ( ! blt_success ) { LogLastError ( " " ) ; return_code = 0 ; goto screenshotwindow_cleanup_capturebitmap ; } if ( ! SaveBitmapToFile ( windowdc , capturebitmap , filename ) ) { SDLTest_LogError ( " " ) ; return_code = 0 ; goto screenshotwindow_cleanup_capturebitmap ; } screenshotwindow_cleanup_capturebitmap : if ( ! DeleteObject ( capturebitmap ) ) { SDLTest_LogError ( " " ) ; return_code = 0 ; } screenshotwindow_cleanup_capturedc : if ( ! DeleteDC ( capturedc ) ) { SDLTest_LogError ( " " ) ; return_code = 0 ; } screenshotwindow_cleanup_windowdc : if ( ! ReleaseDC ( hwnd , windowdc ) ) { SDLTest_LogError ( " " ) ; return_code = 0 ; ; } screenshotwindow_cleanup_generic : return return_code ; }
Takes the screenshot of a window and saves it to a file.
Takes the screenshot of a window and saves it to a file .
/* save bitmap as file */ /* Free resources */