首页
社区
课程
招聘
[旧帖] [求助]请问和截屏相关的API有那些呢 0.00雪花
发表于: 2010-6-18 17:09 1913

[旧帖] [求助]请问和截屏相关的API有那些呢 0.00雪花

2010-6-18 17:09
1913
收藏
免费 0
支持
分享
最新回复 (5)
雪    币: 72
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
调用HANDLE hDib = Capture(GetDesktopWindow());
就可以截取屏幕啦。相关api如下:

HANDLE Capture(CWnd *pWnd)
{
        HANDLE hDib;
        CBitmap         bitmap;

        CWindowDC        dc(pWnd);
        CDC                 memdc;
        CRect                rect;
        WORD nWid,nHei;
        memdc.CreateCompatibleDC(&dc);

        pWnd->GetWindowRect(rect);
    nWid=rect.Width();
        nHei=rect.Height();
    bitmap.CreateCompatibleBitmap(&dc, nWid,nHei);
        CBitmap* poldbitmap = memdc.SelectObject(&bitmap);
        memdc.StretchBlt(0, 0, nWid,nHei, &dc, 0, 0, rect.Width(),rect.Height(),SRCCOPY);

        // create logical palette if device support a palette
        CPalette pal;
        if( dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE)
        {
                UINT nsize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256);
                LOGPALETTE *plp = (LOGPALETTE *) new BYTE[nsize];
                plp->palVersion = 0x300;

                plp->palNumEntries = GetSystemPaletteEntries( dc, 0, 255, plp->palPalEntry );

                // create the palette
                pal.CreatePalette( plp );

                delete[] plp;
        }

        memdc.SelectObject(poldbitmap);

        // convert the bitmap to a dib
         hDib = DDBtoDIB( bitmap, BI_RGB, &pal );

         return hDib;

}

HANDLE DDBtoDIB( CBitmap& bitmap, DWORD dwCompression, CPalette* ppal )
{
        BITMAP                        bm;
        BITMAPINFOHEADER        bi;
         LPBITMAPINFOHEADER         lpbi;
        DWORD                        dwLen;
        HANDLE                        hDib;
        HANDLE                        handle;
        HDC                         hdc;
        HPALETTE                hPal;

        ASSERT( bitmap.GetSafeHandle() );

        // the function has no arg for bitfields
        if( dwCompression == BI_BITFIELDS )
                return NULL;

        // if a palette has not been supplied use default palette
        hPal = (HPALETTE) ppal->GetSafeHandle();
        if (hPal==NULL)
                hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE );

        // get bitmap information
        bitmap.GetObject(sizeof(bm),(LPSTR)&bm);

        // initialize the bitmapinfoheader
        bi.biSize                = sizeof(BITMAPINFOHEADER);
        bi.biWidth                = bm.bmWidth;
        bi.biHeight                 = bm.bmHeight;
        bi.biPlanes                 = 1;
        bi.biBitCount                = bm.bmPlanes * bm.bmBitsPixel;
        bi.biCompression        = dwCompression;
        bi.biSizeImage                = 0;
        bi.biXPelsPerMeter        = 0;
        bi.biYPelsPerMeter        = 0;
        bi.biClrUsed                = 0;
        bi.biClrImportant        = 0;

        // compute the size of the  infoheader and the color table
        int ncolors = (1 << bi.biBitCount); if( ncolors> 256 )
                ncolors = 0;
        dwLen  = bi.biSize + ncolors * sizeof(RGBQUAD);

        // we need a device context to get the dib from
        hdc = GetDC(NULL);
        hPal = SelectPalette(hdc,hPal,FALSE);
        RealizePalette(hdc);

        // allocate enough memory to hold bitmapinfoheader and color table
        hDib = GlobalAlloc(GMEM_FIXED,dwLen);

        if (!hDib){
                SelectPalette(hdc,hPal,FALSE);
                ReleaseDC(NULL,hdc);
                return NULL;
        }

        lpbi = (LPBITMAPINFOHEADER)hDib;

        *lpbi = bi;//////看清楚

        // call getdibits with a NULL lpbits param, so the device driver
        // will calculate the bisizeimage field
        GetDIBits(hdc, (HBITMAP)bitmap.GetSafeHandle(), 0L, (DWORD)bi.biHeight,
                        (LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS );

        bi = *lpbi;//看清楚

        // if the driver did not fill in the bisizeimage field, then compute it
        // each scan line of the image is aligned on a dword (32bit) boundary
        if (bi.biSizeImage == 0){
                bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)
                                                * bi.biHeight;

                // if a compression scheme is used the result may infact be larger
                // increase the size to account for this.
                if (dwCompression != BI_RGB)
                        bi.biSizeImage = (bi.biSizeImage * 3) / 2;
        }

        // realloc the buffer so that it can hold all the bits
        dwLen += bi.biSizeImage;
        if (handle = GlobalReAlloc(hDib, dwLen, GMEM_MOVEABLE))
                hDib = handle;
        else
        {
                GlobalFree(hDib);

                // reselect the original palette
                SelectPalette(hdc,hPal,FALSE);
                ReleaseDC(NULL,hdc);
                return NULL;
        }

        // get the bitmap bits
        lpbi = (LPBITMAPINFOHEADER)hDib;

        // finally get the dib
        BOOL bgotbits = GetDIBits( hdc, (HBITMAP)bitmap.GetSafeHandle(),
                                0L,                                // start scan line
                                (DWORD)bi.biHeight,                // # of scan lines
                                (LPBYTE)lpbi                         // address for bitmap bits
                                + (bi.biSize + ncolors * sizeof(RGBQUAD)),
                                (LPBITMAPINFO)lpbi,                // address of bitmapinfo
                                (DWORD)DIB_RGB_COLORS);                // use rgb for color table

        if( !bgotbits )
        {
                GlobalFree(hDib);
               
                SelectPalette(hdc,hPal,FALSE);
                ReleaseDC(NULL,hdc);
                return NULL;
        }

        SelectPalette(hdc,hPal,FALSE);
        ReleaseDC(NULL,hdc);

        return hDib;
}
2010-6-18 17:43
0
雪    币: 14
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
我也想好好学习一下
2010-6-18 17:51
0
雪    币: 55
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
谢谢了 学习一下
2010-6-19 08:35
0
雪    币: 431
活跃值: (10)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
5
学习了~~~
2010-6-19 09:39
0
雪    币: 261
活跃值: (55)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
6
学习了............................
2010-6-20 22:04
0
游客
登录 | 注册 方可回帖
返回
//