2

ウィンドウの画面を取得するためのコードをいくつか書いています(Windowsで)。コードは正常に動作しますが、スクリーン グラブの前に、キャプチャして強制的に再描画するウィンドウを前面に移動する必要があります。

InvalidateRect を使用して再描画を強制します。次に、WM_PAINT を処理するために、メッセージ ループからいくつかのメッセージを送り出す必要があります。ポンプするメッセージの数がわからないため、これは明らかに少し不自由です。

RDW_ALLCHILDREN で RedrawWindow を使用してみましたが、画面を取得しているアプリは MDI アプリであり、すべての子を再描画していないようです。

私の質問は、スクリーングラブの前にウィンドウを再描画するより良い方法はありますか?

乾杯リッチ

4

1 に答える 1

4

使用している言語について言及していないので、C++の次のコードがお役に立てば幸いです。

void getScreenShot( int texWidth, int texHeight, unsigned char* pBuffer, HWND handle )
{
    /* Local variables */
    HDC screenDC;
    RECT screenRect;
    int extraBytesPerRow;
    BITMAPINFO bitmapInfo;
    HDC bitmapDC;
    void* bitmapDataPtr;
    HBITMAP hBitmap;
    HBITMAP hPrevBitmap;
    unsigned char* pIn;
    unsigned char* pOut;
    int rowIndex;
    int colIndex;
    /* Get a DC from the desktop window */
    screenDC = GetDC(handle);
    GetClientRect(handle, &screenRect );
    /* Determine the extra bytes we need per row (each row of bitmap data must end on a 32bit boundary) */
    extraBytesPerRow = ( texWidth * 3 ) % 4;
    extraBytesPerRow = extraBytesPerRow ? 4 - extraBytesPerRow : 0;
    /* Setup the bitmap info structure */
    memset( &bitmapInfo, 0, sizeof( bitmapInfo ) );
    bitmapInfo.bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
    bitmapInfo.bmiHeader.biWidth = texWidth;
    bitmapInfo.bmiHeader.biHeight = texHeight;
    bitmapInfo.bmiHeader.biPlanes = 1;
    bitmapInfo.bmiHeader.biBitCount = 24;
    bitmapInfo.bmiHeader.biCompression = BI_RGB;
    /* Create a bitmap device context (bitmapDataPtr will be a pointer to the bits in the bitmap) */
    bitmapDC = CreateCompatibleDC( NULL );
    hBitmap = CreateDIBSection( bitmapDC, ( BITMAPINFO* )&bitmapInfo.bmiHeader, DIB_RGB_COLORS, &bitmapDataPtr, NULL, 0 );
    hPrevBitmap = ( HBITMAP )SelectObject( bitmapDC, hBitmap );
    /* BitBlt or StretchBlt the image from the input DC into our bitmap DC */
    if ( ( texWidth != screenRect.right ) || ( texHeight != screenRect.bottom ) )
    {
        SetStretchBltMode( bitmapDC, HALFTONE );
        StretchBlt( bitmapDC, 0, 0, texWidth, texHeight, screenDC, 0, 0, screenRect.right, screenRect.bottom, SRCCOPY );
    }
    else
    {
        BitBlt( bitmapDC, 0, 0, texWidth, texHeight, screenDC, 0, 0, SRCCOPY);
    }
    /* Copy the data from the bitmap to the user's buffer (bitmap data is BGR and 4 byte aligned on each row, we want tightly-packed RGB) */
    pIn = ( unsigned char* )bitmapDataPtr;
    pOut = pBuffer;
    for ( rowIndex = 0; rowIndex < texHeight; rowIndex++ )
    {
        for ( colIndex = 0; colIndex < texWidth; colIndex++ )
        {
            pOut[ 0 ] = pIn[2];
            pOut[ 1 ] = pIn[1];
            pOut[ 2 ] = pIn[0];
            pOut += 3;
            pIn += 3;
        }
        pIn += extraBytesPerRow;
    }
    /* Free memory used by the bitmap */
    SelectObject( bitmapDC, hPrevBitmap );
    DeleteObject( hBitmap );
    DeleteDC( bitmapDC );
    /* Release the screen DC */
    ReleaseDC(handle, screenDC );
}

実際に再描画を強制する必要はありません。ただし、ウィンドウが最小化されている場合は、ウィンドウハンドルを使用して関数を呼び出す前に、ウィンドウを表示する必要があります。texWidthとtexHeightは、キャプチャしようとしているウィンドウのサイズです。これを取得するには、GetWindowRect(..)を使用するか、次のリンクを確認してください:link

于 2009-12-27T07:43:13.530 に答える