c++多显示器屏幕截图代码
时间: 2023-10-20 17:17:17 浏览: 96
C++ 屏幕截图源码
3星 · 编辑精心推荐
以下是在C语言中实现多显示器屏幕截图的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
int main()
{
// 获取所有显示器的句柄
int numMonitors = GetSystemMetrics(SM_CMONITORS);
HMONITOR* hMonitors = (HMONITOR*)malloc(numMonitors * sizeof(HMONITOR));
EnumDisplayMonitors(NULL, NULL, MonitorEnumCallback, (LPARAM)hMonitors);
// 截取每个显示器的屏幕并保存为文件
for (int i = 0; i < numMonitors; i++)
{
HDC hDC = CreateDC(NULL, MonitorDevicePath(hMonitors[i]), NULL, NULL);
RECT rect;
GetMonitorRect(hMonitors[i], &rect);
HBITMAP hBitmap = CreateCompatibleBitmap(hDC, rect.right - rect.left, rect.bottom - rect.top);
HDC hMemDC = CreateCompatibleDC(hDC);
HGDIOBJ hOldObject = SelectObject(hMemDC, hBitmap);
BitBlt(hMemDC, 0, 0, rect.right - rect.left, rect.bottom - rect.top, hDC, 0, 0, SRCCOPY);
SelectObject(hMemDC, hOldObject);
DeleteDC(hMemDC);
SaveBitmapToFile(hBitmap, i);
DeleteObject(hBitmap);
DeleteDC(hDC);
}
free(hMonitors);
return 0;
}
BOOL CALLBACK MonitorEnumCallback(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
HMONITOR* hMonitors = (HMONITOR*)dwData;
int index = GetMonitorCount(hMonitors);
hMonitors[index] = hMonitor;
return TRUE;
}
int GetMonitorCount(HMONITOR* hMonitors)
{
int count = 0;
while (hMonitors[count] != NULL)
{
count++;
}
return count;
}
TCHAR* MonitorDevicePath(HMONITOR hMonitor)
{
MONITORINFOEX monitorInfo;
monitorInfo.cbSize = sizeof(MONITORINFOEX);
GetMonitorInfo(hMonitor, &monitorInfo);
return monitorInfo.szDevice;
}
void GetMonitorRect(HMONITOR hMonitor, LPRECT lpRect)
{
MONITORINFOEX monitorInfo;
monitorInfo.cbSize = sizeof(MONITORINFOEX);
GetMonitorInfo(hMonitor, &monitorInfo);
*lpRect = monitorInfo.rcMonitor;
}
void SaveBitmapToFile(HBITMAP hBitmap, int index)
{
TCHAR fileName[256];
wsprintf(fileName, TEXT("screenshot%d.bmp"), index);
HANDLE hFile = CreateFile(fileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
BITMAPFILEHEADER bmpFileHeader;
BITMAPINFO bmpInfo;
BITMAP bmp;
GetObject(hBitmap, sizeof(BITMAP), &bmp);
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = bmp.bmWidth;
bmpInfo.bmiHeader.biHeight = bmp.bmHeight;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = bmp.bmBitsPixel;
bmpInfo.bmiHeader.biCompression = BI_RGB;
bmpInfo.bmiHeader.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;
bmpInfo.bmiHeader.biXPelsPerMeter = 0;
bmpInfo.bmiHeader.biYPelsPerMeter = 0;
bmpInfo.bmiHeader.biClrUsed = 0;
bmpInfo.bmiHeader.biClrImportant = 0;
bmpFileHeader.bfType = 0x4d42;
bmpFileHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + bmp.bmWidthBytes * bmp.bmHeight;
bmpFileHeader.bfReserved1 = 0;
bmpFileHeader.bfReserved2 = 0;
bmpFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
DWORD bytesWritten;
WriteFile(hFile, &bmpFileHeader, sizeof(BITMAPFILEHEADER), &bytesWritten, NULL);
WriteFile(hFile, &bmpInfo.bmiHeader, sizeof(BITMAPINFOHEADER), &bytesWritten, NULL);
WriteFile(hFile, bmp.bmBits, bmp.bmWidthBytes * bmp.bmHeight, &bytesWritten, NULL);
CloseHandle(hFile);
}
```
阅读全文