Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 抓屏_Windows_Screen_Redraw - Fatal编程技术网

Windows 抓屏

Windows 抓屏,windows,screen,redraw,Windows,Screen,Redraw,我一直在写一些代码来抓取一个窗口(在Windows中)。代码运行良好,但是在抓屏之前,我必须将窗口移到我想要捕获的前面,并强制重新绘制 我强制使用invalidate重新绘制,然后我必须从消息循环中抽取一些消息,以便处理WM_绘制。这显然有点蹩脚,因为我不知道要发送多少消息 我试着在RDW_ALLCHILDREN中使用RedrawWindow,但是我正在从中抓取屏幕的应用程序是一个MDI应用程序,并且似乎没有重画它的所有子项 所以我的问题是,有没有更好的方法在抓屏之前重新绘制窗口 干杯 Real

我一直在写一些代码来抓取一个窗口(在Windows中)。代码运行良好,但是在抓屏之前,我必须将窗口移到我想要捕获的前面,并强制重新绘制

我强制使用invalidate重新绘制,然后我必须从消息循环中抽取一些消息,以便处理WM_绘制。这显然有点蹩脚,因为我不知道要发送多少消息

我试着在RDW_ALLCHILDREN中使用RedrawWindow,但是我正在从中抓取屏幕的应用程序是一个MDI应用程序,并且似乎没有重画它的所有子项

所以我的问题是,有没有更好的方法在抓屏之前重新绘制窗口

干杯
Reale

< P>因为你没有提到你正在使用的语言,我希望下面的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 );
}
void getScreenShot(int-texWidth、int-texHeight、unsigned char*pBuffer、HWND句柄)
{
/*局部变量*/
HDC-screenDC;
RECT-screenRect;
int extraBytesPerRow;
BITMAPINFO BITMAPINFO;
HDC-bitmapDC;
void*bitmapDataPtr;
HBITMAP HBITMAP;
HBITMAP HPREV位图;
无符号字符*pIn;
无符号字符*pOut;
整数行索引;
int-colIndex;
/*从桌面窗口获取DC*/
screenDC=GetDC(句柄);
GetClientRect(handle和screenRect);
/*确定每行需要的额外字节数(每行位图数据必须以32位边界结束)*/
extraBytesPerRow=(texWidth*3)%4;
extraBytesPerRow=extraBytesPerRow?4-extraBytesPerRow:0;
/*设置位图信息结构*/
memset(&bitmapInfo,0,sizeof(bitmapInfo));
bitmapInfo.bmiHeader.biSize=sizeof(bitmapInfo头文件);
bitmapInfo.bmiHeader.biWidth=texWidth;
bitmapInfo.bmiHeader.biHeight=texHeight;
bitmapInfo.bmiHeader.biPlanes=1;
bitmapInfo.bmiHeader.biBitCount=24;
bitmapInfo.bmiHeader.biCompression=BI_RGB;
/*创建位图设备上下文(bitmapDataPtr将是指向位图中位的指针)*/
bitmapDC=CreateCompatibleDC(空);
hBitmap=CreateDIBSection(bitmapDC,(BITMAPINFO*)&BITMAPINFO.bmiHeader,DIB_RGB_颜色,&bitmapDataPtr,NULL,0);
hPrevBitmap=(HBITMAP)选择对象(bitmapDC,HBITMAP);
/*BitBlt或StretchBlt将图像从输入DC转换为位图DC*/
如果((texWidth!=screenRect.right)| |(texHeight!=screenRect.bottom))
{
SetStretchBltMode(位图、半色调);
StretchBlt(位图直流,0,0,texWidth,texHeight,screenDC,0,0,screenRect.right,screenRect.bottom,SRCCOPY);
}
其他的
{
BitBlt(bitmapDC,0,0,texWidth,texHeight,screenDC,0,0,SRCCOPY);
}
/*将数据从位图复制到用户的缓冲区(位图数据为BGR,每行4字节对齐,我们需要紧密压缩的RGB)*/
引脚=(无符号字符*)bitmapDataPtr;
pOut=pBuffer;
对于(rowIndex=0;rowIndex

实际上,你不需要强制重画。。但是如果窗口被最小化,您可能需要在使用窗口句柄调用函数之前将其调出。。!texWidth和texHeight是要捕获的窗口的尺寸;要获得此信息,您可以使用GetWindowRect(..)或查看此处的链接:

是否在
invalidate()
call?中将最后一个参数(
bErase
)设置为true,并在无效后尝试使用
UpdateWindow()
调用