Snippets: Taking a screenshot programatically

1,762 views
Skip to first unread message

Pravin Paratey

unread,
Sep 10, 2007, 1:59:00 PM9/10/07
to Win32 Programming
// Program to take a screenshot
// Pravin Paratey (January 04, 2004)

#include <windows.h>
#include <windowsx.h>

LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
void CaptureScreen(HWND);
void OnPaint(HWND hwnd);

char szClassName[ ] = "Screenshot";

HBITMAP hBitmap = NULL;

int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
HDC dc;

wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

if (!RegisterClassEx (&wincl))
return 0;

hwnd = CreateWindowEx (
0,
szClassName,
"Screenshot",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
400,
300,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL
);

CaptureScreen(hwnd);

ShowWindow (hwnd, SW_MAXIMIZE);


while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}

return messages.wParam;
}

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM
wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
HANDLE_WM_PAINT(hwnd,wParam,lParam,OnPaint);
break;
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}

void OnPaint(HWND hwnd)
{
HDC hBitmapdc, hWindowdc;
HBITMAP hOld;
PAINTSTRUCT ps;
RECT rc;
int nWid, nHt;

// If the bitmap is present, draw it
if (hBitmap)
{
// Get window dc and start paint
hWindowdc = BeginPaint(hwnd, &ps);
hWindowdc = GetDC(hwnd);
// Create compatible dc
hBitmapdc = CreateCompatibleDC(hWindowdc);
// Select the bitmap into the dc
hOld = SelectBitmap(hBitmapdc,hBitmap);
// Get width and height of this window
GetClientRect(hwnd,&rc);
// Get the dimensions of the bitmap
nWid = GetSystemMetrics(SM_CXSCREEN);
nHt = GetSystemMetrics(SM_CYSCREEN);
// Draw the bitmap to the window leaving a border of 20 pixels
StretchBlt(hWindowdc,0,0,rc.right,rc.bottom,
hBitmapdc,0,0,nWid,nHt,SRCCOPY);
// Delete bitmap dc
DeleteDC(hBitmapdc);
// Tell windows that you've updated the window
EndPaint(hwnd,&ps);
}
}

void CaptureScreen(HWND hParent)
{
HDC hDesktopdc,hBitmapdc;
int nWid, nHt; // Stores the height and width of the screen
HBITMAP hOriginal;

// Get a handle to the screen device context
hDesktopdc = GetWindowDC(HWND_DESKTOP);
if (hDesktopdc)
{
// Get width and height of screen
nWid = GetSystemMetrics(SM_CXSCREEN);
nHt = GetSystemMetrics(SM_CYSCREEN);

// Create a compatible bitmap
hBitmap = CreateCompatibleBitmap(hDesktopdc,nWid,nHt);
// Create compatible DC
hBitmapdc = CreateCompatibleDC(hDesktopdc);
// Select Bitmap
hOriginal = (HBITMAP)SelectBitmap(hBitmapdc, hBitmap);
// Copy pixels from screen to the BITMAP
BitBlt(hBitmapdc,0,0,nWid,nHt,
hDesktopdc,0,0,SRCCOPY);
// Delete the hBitmapdc as you no longer need it
DeleteDC(hBitmapdc);
// Release the desktop device context handle
ReleaseDC(HWND_DESKTOP, hDesktopdc);
// Invalidate the window
UpdateWindow(hParent);
}
}

Reply all
Reply to author
Forward
0 new messages