Information Systems



Code for Screen Capture with wxWidgets

This is a weird sort of mixture. It uses both native Windows code as well as wxWidgets code. If anybody knows if there's a wxWidgets equivalent to GetDesktopWindow, please let me know. Then there wouldn't have to be any native (yuc) GDI code. Unbelievably stupid when you are faced with coding in that stuff. I know there's a such thing as GDI+, however it's definitely too little too late. Anything that won't run on Linux as well as Windows for me is problematic.

RECT rect;
HWND wnd = GetDesktopWindow();
HDC dc = GetDC(wnd);
::GetClientRect(wnd, &rect);
HDC dc1 = CreateCompatibleDC(dc);

BITMAPINFO bi;
memset( &bi, 0, sizeof(BITMAPINFO) );
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biWidth = rect.right;
bi.bmiHeader.biHeight = rect.bottom;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 24;
bi.bmiHeader.biCompression = BI_RGB;
void *bits = NULL;
HBITMAP bm = CreateDIBSection(dc1, &bi, DIB_RGB_COLORS,
(void **)(&bits), NULL, 0 );

SelectObject(dc1, bm);
BitBlt(dc1, 0, 0, rect.right, rect.bottom, dc, 0, 0, SRCCOPY);
wxImage image;
image.Create(rect.right, rect.bottom);
for (int x = 0; x < rect.right; x++)
{
for (int y = 0; y < rect.bottom; y++)
{
COLORREF c = GetPixel(dc1, x, y);
image.SetRGB(x, y, GetRValue(c), GetGValue(c), GetBValue(c));
}
}
image.SaveFile("c:/test.bmp", wxBITMAP_TYPE_BMP);