Home > Backend Development > C++ > How to Capture Screenshots of Windows Applications Using Win32 GDI?

How to Capture Screenshots of Windows Applications Using Win32 GDI?

Patricia Arquette
Release: 2024-12-08 13:23:12
Original
965 people have browsed it

How to Capture Screenshots of Windows Applications Using Win32 GDI?

Taking Screenshots in Windows Applications with Win32

Capturing the current screen display is a common need in application development. In Windows, this can be achieved efficiently using Win32's Graphics Device Interface (GDI) functions.

Solution

The following code snippet demonstrates how to take a screenshot using Win32:

HDC hScreenDC = GetDC(nullptr);
HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
int width = GetDeviceCaps(hScreenDC,HORZRES);
int height = GetDeviceCaps(hScreenDC,VERTRES);
HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC,width,height);
HBITMAP hOldBitmap = static_cast<HBITMAP>(SelectObject(hMemoryDC,hBitmap));
BitBlt(hMemoryDC,0,0,width,height,hScreenDC,0,0,SRCCOPY);
hBitmap = static_cast<HBITMAP>(SelectObject(hMemoryDC,hOldBitmap));
DeleteDC(hMemoryDC);
DeleteDC(hScreenDC);
Copy after login

Explanation

  • GetDC retrieves a device context for the screen.
  • CreateCompatibleDC creates a memory device context compatible with the screen DC.
  • GetDeviceCaps obtains the current screen resolution.
  • CreateCompatibleBitmap creates a bitmap compatible with the screen DC and matching the resolution.
  • SelectObject selects the bitmap into the memory DC.
  • BitBlt copies the pixels from the screen DC to the bitmap in the memory DC.
  • DeleteDC releases the memory and screen DCs.

The above is the detailed content of How to Capture Screenshots of Windows Applications Using Win32 GDI?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template