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
Release: 2024-12-05 22:12:17
Original
1012 people have browsed it

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);
Copy after login

Retrieve the device context of the entire screen.

HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
Copy after login

Create a compatible device context for copying the screen bitmap.

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

Obtain the width and height of the screen in pixels.

HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC,width,height);
Copy after login

Create a compatible bitmap to hold the screen image.

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

Select the compatible bitmap into the compatible device context.

BitBlt(hMemoryDC,0,0,width,height,hScreenDC,0,0,SRCCOPY);
Copy after login

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

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

Restore the previous bitmap selection to the memory device context.

DeleteDC(hMemoryDC);
DeleteDC(hScreenDC);
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template