Code:
/*screen capture example*/
#include <windows.h>
#define SCREENWIDTH GetSystemMetrics(SM_CXSCREEN)
#define SCREENHEIGHT GetSystemMetrics(SM_CYSCREEN)
HBITMAP g_hDeskBmp;
HDC g_hMemDC;
int g_nDCdata;
void CaptureDesktop()
{
/*capture desktop and store for later use*/
HDC hdcDesk;
hdcDesk=GetDC(HWND_DESKTOP); /*equivalent to hdcDesk=GetDC((HWND)0);*/
g_hMemDC=CreateCompatibleDC(0);
g_hDeskBmp=CreateCompatibleBitmap(hdcDesk, SCREENWIDTH, SCREENHEIGHT);
/*fill the bitmap with contents of desktop*/
g_nDCdata=SaveDC(g_hMemDC);
SelectObject(g_hMemDC,g_hDeskBmp);
BitBlt(g_hMemDC, 0, 0, SCREENWIDTH, SCREENHEIGHT,hdcDesk,0,0,SRCCOPY);
ReleaseDC(HWND_DESKTOP,hdcDesk);
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
{
CaptureDesktop();
break;
}
case WM_PAINT:
{
PAINTSTRUCT ps;
BeginPaint(hwnd,&ps);
BitBlt(ps.hdc,0,0,SCREENWIDTH,SCREENHEIGHT,g_hMemDC,0,0, SRCCOPY);
EndPaint(hwnd,&ps);
break;
}
case WM_DESTROY:
/*restore memory dc to original state
and free up gdi resources*/
RestoreDC(g_hMemDC,g_nDCdata);
DeleteDC(g_hMemDC);
DeleteObject(g_hDeskBmp);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
HWND hWindow;
MSG msg;
WNDCLASSEX wc;
/*registering the window class*/
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName= "windowclass";
wc.hIconSm = NULL;
/*make sure window class has registered*/
if(!RegisterClassEx(&wc))
{
MessageBox(NULL,"Window Registration Failed","Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
/*create screensaver window*/
hWindow=CreateWindowEx(WS_EX_CLIENTEDGE,wc.lpszClassName,"screensaver",
WS_OVERLAPPEDWINDOW,0,0,
SCREENWIDTH,SCREENHEIGHT,
NULL,NULL,hInstance,NULL);
ShowWindow(hWindow,nCmdShow);
UpdateWindow(hWindow);
if(hWindow == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
/*main message loop*/
while(GetMessage(&msg,NULL,0,0)>0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}