Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I take a screenshot in a windows application?

How can I take a screenshot of the current screen using Win32?

like image 319
user63898 Avatar asked Jul 20 '10 14:07

user63898


People also ask

How do I take a screenshot of a specific app in Windows?

Press Ctrl + PrtScn keys. The entire screen changes to gray including the open menu. Select Mode, or in earlier versions of Windows, select the arrow next to the New button. Select the kind of snip you want, and then select the area of the screen capture that you want to capture.

How do you screenshot on an application?

Take a scrolling screenshot Important: These steps work only on Android 12, on most screens that allow you to scroll. Open the screen that you want to capture. Press the Power and Volume down buttons at the same time. At the bottom, tap Capture more.

How do I take a simple screenshot in Windows?

The easiest way to take a screenshot on Windows 10 or Windows 11 is with the Print Screen (PrtScn) key. To capture your entire screen, simply press PrtScn on the upper-right side of your keyboard. In Windows 10, the screenshot will be copied to your clipboard.


2 Answers

HDC hScreenDC = GetDC(nullptr); // CreateDC("DISPLAY",nullptr,nullptr,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); 
like image 122
Woody Avatar answered Oct 01 '22 21:10

Woody


  1. Use GetDC(NULL); to get a DC for the entire screen.
  2. Use CreateCompatibleDC to create a DC compatible with the screen DC.
  3. Use CreateCompatibleBitmap to create a bitmap compatible with the screen DC to hold the result.
  4. Use SelectObject to select the compatible bitmap into the compatible DC.
  5. Use BitBlt to copy from the screen DC to the compatible DC.
  6. Use SelectObject to deselect the compatible bitmap from the compatible DC.
  7. Use DeleteDC to delete the compatible DC.

When you create the compatible bitmap, you want it compatible with the screen DC, not the compatible DC.

For example:

HDC dcScreen = GetDC(0); HDC dcTarget = CreateCompatibleDC(dcScreen); HBITMAP bmpTarget = CreateCompatibleBitmap(dcScreen); HGDIOBJ oldBmp = SelectObject(dcTarget, bmpTarget); BitBlt(dcTarget, 0, 0, cx, cy, dcDesktop, x, y, SRCCOPY | CAPTUREBLT); SelectObject(dcTarget, oldBmp); DeleteDC(dcTarget); ReleaseDC(dcScreen); 

The other important part is to get the size, and location, of the entire virtual screen:

int x  = GetSystemMetrics(SM_XVIRTUALSCREEN);  //left (e.g. -1024) int y  = GetSystemMetrics(SM_YVIRTUALSCREEN);  //top (e.g. -34) int cx = GetSystemMetrics(SM_CXVIRTUALSCREEN); //entire width (e.g. 2704) int cy = GetSystemMetrics(SM_CYVIRTUALSCREEN); //entire height (e.g. 1050) 
like image 29
Jerry Coffin Avatar answered Oct 01 '22 23:10

Jerry Coffin