Home >Backend Development >C++ >How Can I Capture a Windows Application Screenshot Using the Win32 API?

How Can I Capture a Windows Application Screenshot Using the Win32 API?

DDD
DDDOriginal
2024-12-05 22:12:171006browse

How Can I Capture a Windows Application Screenshot Using the Win32 API?

Windows Application Screenshot Capture

In Windows programming, capturing a screenshot of the current screen is a common requirement. For this purpose, the Win32 API provides the necessary functions to acquire a bitmap representation of the screen.

Win32 Screenshot Code

To take a screenshot in a Windows application using Win32, follow these steps:

HDC hScreenDC = GetDC(nullptr);

Retrieve the device context of the entire screen.

HDC hMemoryDC = CreateCompatibleDC(hScreenDC);

Create a compatible device context for copying the screen bitmap.

int width = GetDeviceCaps(hScreenDC,HORZRES);
int height = GetDeviceCaps(hScreenDC,VERTRES);

Obtain the width and height of the screen in pixels.

HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC,width,height);

Create a compatible bitmap to hold the screen image.

HBITMAP hOldBitmap = static_cast<HBITMAP>(SelectObject(hMemoryDC,hBitmap));

Select the compatible bitmap into the compatible device context.

BitBlt(hMemoryDC,0,0,width,height,hScreenDC,0,0,SRCCOPY);

Copy the screen image to the compatible bitmap using bit block transfer (BitBlt).

hBitmap = static_cast<HBITMAP>(SelectObject(hMemoryDC,hOldBitmap));

Restore the previous bitmap selection to the memory device context.

DeleteDC(hMemoryDC);
DeleteDC(hScreenDC);

Delete both device contexts to release resources.

The above is the detailed content of How Can I Capture a Windows Application Screenshot Using the Win32 API?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn