使用BitBlt不显示边框的Win32窗口捕获

时间:2010-03-19 01:55:56

标签: winapi windows-vista bitmap bitblt

我已经编写了一些c ++代码来捕获.bmp文件的窗口。

BITMAPFILEHEADER get_bitmap_file_header(int width, int height)
{
 BITMAPFILEHEADER hdr;
 memset(&hdr, 0, sizeof(BITMAPFILEHEADER));
 hdr.bfType  = ((WORD) ('M' << 8) | 'B'); // is always "BM"
 hdr.bfSize  = 0;//sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (width * height * sizeof(int));
 hdr.bfReserved1 = 0;
 hdr.bfReserved2 = 0;
 hdr.bfOffBits = (DWORD)(sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER));

 return hdr;
}

BITMAPINFO get_bitmap_info(int width, int height)
{
 BITMAPINFO bmi;
 memset(&bmi.bmiHeader, 0, sizeof(BITMAPINFOHEADER));
 //initialize bitmap header
 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
 bmi.bmiHeader.biWidth = width;
 bmi.bmiHeader.biHeight = height;
 bmi.bmiHeader.biPlanes = 1;
 bmi.bmiHeader.biBitCount = 4 * 8;
 bmi.bmiHeader.biCompression = BI_RGB;
 bmi.bmiHeader.biSizeImage = width * height * 4;

 return bmi;
}

void get_bitmap_from_window(HWND hWnd, int * imageBuff)
{
 HDC hDC = GetWindowDC(hWnd);
 SIZE size = get_window_size(hWnd);
 HDC hMemDC = CreateCompatibleDC(hDC);
 RECT r;

 HBITMAP hBitmap = CreateCompatibleBitmap(hDC, size.cx, size.cy);
 HBITMAP hOld = (HBITMAP)SelectObject(hMemDC, hBitmap);

 BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC, 0, 0, SRCCOPY);
    //PrintWindow(hWnd, hMemDC, 0);

 BITMAPINFO bmi = get_bitmap_info(size.cx, size.cy);
 GetDIBits(hMemDC, hBitmap, 0, size.cy, imageBuff, &bmi, DIB_RGB_COLORS);

 SelectObject(hMemDC, hOld);
    DeleteDC(hMemDC);
    ReleaseDC(NULL, hDC);
}

void save_image(HWND hWnd, char * name)
{
 int * buff;
 RECT r;
 SIZE size;

 GetWindowRect(hWnd, &r);
 size.cx = r.right-r.left;
 size.cy = r.bottom-r.top;

 buff = (int*)malloc(size.cx * size.cy * sizeof(int));

 get_bitmap_from_window(hWnd, buff);

 BITMAPINFO bmi = get_bitmap_info(size.cx, size.cy);
 BITMAPFILEHEADER hdr = get_bitmap_file_header(size.cx, size.cy);

 FILE * fout = fopen(name, "w");
 fwrite(&hdr, 1, sizeof(BITMAPFILEHEADER), fout);
 fwrite(&bmi.bmiHeader, 1, sizeof(BITMAPINFOHEADER), fout);
 fwrite(buff, 1, size.cx * size.cy * sizeof(int), fout);

 fflush(fout);
 fclose(fout);
 free(buff);
}

它可以在XP下找到,但在Vista下,窗口的边框是透明的。

使用PrintWindow可以解决问题,但由于性能原因,这是不可接受的。

是否有高效的代码更改,或者可以更改设置以使边框不透明?

2 个答案:

答案 0 :(得分:0)

原来这是由于在窗口上设置了WS_EX_LAYERED属性。

有关相关问题的讨论,请参阅http://www.eggheadcafe.com/software/aspnet/31543575/dwm-composition--get-par.aspx

奇怪地使用PrintWindow一次,然后BitBlt解决了这个问题......

答案 1 :(得分:0)

使用以下代替

BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC, 0, 0, SRCCOPY | CAPTUREBLT);