Copying the Screen Using StretchBlt Function
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
WNDCLASS wc = {0};
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpszClassName = TEXT("Screen copy");
wc.hInstance = hInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
RegisterClass(&wc);
CreateWindow(
wc.lpszClassName,
TEXT("Screen Copy using StretchBlt"),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
100,
100,
500,
400,
NULL,
NULL,
hInstance,
NULL);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam)
{
switch(msg)
{
case WM_LBUTTONDOWN:
{
int screenWidth;
int screenHeight;
int windowWidth;
int windowHeight;
RECT rect;
HDC screenDC;
HDC windowDC;
// Get screen dimensions
screenWidth = GetSystemMetrics(SM_CXSCREEN);
screenHeight = GetSystemMetrics(SM_CYSCREEN);
// Get application window client area size
GetClientRect(hwnd, &rect);
windowWidth = rect.right - rect.left;
windowHeight = rect.bottom - rect.top;
// Get desktop device context
screenDC = GetWindowDC(NULL);
// Get application window device context
windowDC = GetDC(hwnd);
// Copy and resize desktop image into application window
StretchBlt(
windowDC,
0,
0,
windowWidth,
windowHeight,
screenDC,
0,
0,
screenWidth,
screenHeight,
SRCCOPY);
// Release device contexts
ReleaseDC(NULL, screenDC);
ReleaseDC(hwnd, windowDC);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}