我使用这种可靠的方法来截取屏幕截图。我称之为可靠,因为从XP开始,它适用于所有版本的Windows。但是在Win10中,它现在给了我透明的部分。某些类型的窗户中的某些区域只是透明的,我无法解释。
这是一个屏幕截图,键盘上有正常的打印屏幕键 -
这是使用我的代码 -
的同一部分的屏幕截图似乎是文件类型的窗口"文件资源管理器"某些领域变得透明。上面的截图是Notepad ++。
这是我的算法:
#include <Windows.h>
#include <stdio.h>
int WINAPI WinMain(HINSTANCE hinst, HINSTANCE, LPSTR, int)
{
MessageBox(0, L"Hello World", L"Unipen", MB_ICONINFORMATION);
int i = 0;
DISPLAY_DEVICE device;
device.cb = sizeof(device);
while (EnumDisplayDevices(NULL, i, &device, 0) && ++i) {
if ((device.StateFlags & DISPLAY_DEVICE_ACTIVE) != DISPLAY_DEVICE_ACTIVE) {
MessageBox(0, device.DeviceName, L"CONTINUE", MB_ICONINFORMATION);
continue;
}
MessageBox(0, device.DeviceName, L"BREAK", MB_ICONINFORMATION);
break;
}
size_t screenWidth = 1920;
size_t screenHeight = 1200;
size_t colorLen = 4;
HDC hdcScreen;
hdcScreen = CreateDC(NULL, device.DeviceName, NULL, NULL);
if (hdcScreen == (HDC)NULL) {
MessageBox(0, L"UnableToCreateDC", L"ERROR", MB_ICONINFORMATION);
return 0;
}
HDC hdcMemoryDC;
hdcMemoryDC = CreateCompatibleDC(hdcScreen);
if (hdcMemoryDC == (HDC)NULL) {
DeleteDC(hdcScreen);
MessageBox(0, L"UnableToCreateCompatibleDC", L"ERROR", MB_ICONINFORMATION);
return 0;
}
BITMAPINFO bmi;
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = (LONG)screenWidth;
bmi.bmiHeader.biHeight = (-1)*(LONG)screenHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
BYTE *pixelBuffer;
HBITMAP hbmp;
hbmp = CreateDIBSection(hdcScreen, &bmi, DIB_RGB_COLORS, (void **)&pixelBuffer, NULL, 0);
if (hbmp == (HBITMAP)NULL) {
DeleteDC(hdcScreen);
DeleteDC(hdcMemoryDC);
MessageBox(0, L"UnableToCreateDIBSection", L"ERROR", MB_ICONINFORMATION);
return 0;
}
//HGDIOBJ rez_selected;
HBITMAP rez_selected_bmp;
rez_selected_bmp = (HBITMAP)SelectObject(hdcMemoryDC, hbmp);
if (rez_selected_bmp == (HBITMAP)NULL) {
DeleteDC(hdcScreen);
DeleteDC(hdcMemoryDC);
DeleteObject(hbmp);
MessageBox(0, L"UnableToCreateDIBSection", L"ERROR", MB_ICONINFORMATION);
return 0;
}
BitBlt(hdcMemoryDC, 0, 0, screenWidth, screenHeight, hdcScreen, 0, 0, SRCCOPY);
//(void) SelectObject(hdcMemoryDC, rez_selected_bmp);
if (fopen_s(&stream, "C:\\Users\Vayeate\\Desktop\\blah.txt", "w") == 0) {
for(size_t px = 0 ; px < (screenHeight * screenWidth * 4) ; ++px) {
fprintf(stream, "%hhu, ", pixelBuffer[px]);
}
fclose(stream);
}
DeleteDC(hdcScreen);
DeleteDC(hdcMemoryDC);
DeleteObject(hbmp);
MessageBox(0, L"DONE", L"Unipen", MB_ICONINFORMATION);
return 0;
}