How to take a screenshot of a second screen monitor?

独自空忆成欢 提交于 2021-02-04 16:23:05

问题


I need get a screenshot of a second screen monitor. I found a simple way to make a screen shot, but only works for screen shot of a primary monitor. Here is the code:

#include <windows.h>
void ScreenShot(char*BmpName){
    HWND DesktopHwnd = GetDesktopWindow();
    RECT DesktopParams;
    HDC DevC = GetDC(DesktopHwnd);
    GetWindowRect(DesktopHwnd, &DesktopParams);
    DWORD Width = DesktopParams.right - DesktopParams.left;
    DWORD Height = DesktopParams.bottom - DesktopParams.top;

    DWORD FileSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (sizeof(RGBTRIPLE) + 1 * (Width*Height * 4));
    char *BmpFileData = (char*)GlobalAlloc(0x0040, FileSize);

    PBITMAPFILEHEADER BFileHeader = (PBITMAPFILEHEADER)BmpFileData;
    PBITMAPINFOHEADER  BInfoHeader = (PBITMAPINFOHEADER)&BmpFileData[sizeof(BITMAPFILEHEADER)];

    BFileHeader->bfType = 0x4D42; // BM
    BFileHeader->bfSize = sizeof(BITMAPFILEHEADER);
    BFileHeader->bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

    BInfoHeader->biSize = sizeof(BITMAPINFOHEADER);
    BInfoHeader->biPlanes = 1;
    BInfoHeader->biBitCount = 24;
    BInfoHeader->biCompression = BI_RGB;
    BInfoHeader->biHeight = Height;
    BInfoHeader->biWidth = Width;

    RGBTRIPLE *Image = (RGBTRIPLE*)&BmpFileData[sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)];
    RGBTRIPLE color;

    HDC CaptureDC = CreateCompatibleDC(DevC);
    HBITMAP CaptureBitmap = CreateCompatibleBitmap(DevC, Width, Height);
    SelectObject(CaptureDC, CaptureBitmap);
    BitBlt(CaptureDC, 0, 0, Width, Height, DevC, 0, 0, SRCCOPY | CAPTUREBLT);
    GetDIBits(CaptureDC, CaptureBitmap, 0, Height, Image, (LPBITMAPINFO)BInfoHeader, DIB_RGB_COLORS);

    DWORD Junk;
    HANDLE FH = CreateFileA(BmpName, GENERIC_WRITE, FILE_SHARE_WRITE, 0, CREATE_ALWAYS, 0, 0);
    WriteFile(FH, BmpFileData, FileSize, &Junk, 0);
    CloseHandle(FH);
    GlobalFree(BmpFileData);
}

int main()
{
    ScreenShot("Hello.bmp");
}

But this take a screen shot only of the default monitor.

If has in presentation mode, with 2 monitors I need to take a screen shot of a second monitor.

I'm try make this same code on the

EnumDisplayMonitors

like this:

#include <windows.h>
BOOL CALLBACK MonitorEnumProcCallback(_In_  HMONITOR hMonitor, _In_  HDC DevC, _In_  LPRECT lprcMonitor, _In_  LPARAM dwData) {
    screenCounter++;

    char*BmpName;

    if (screenCounter == 1){
        BmpName = "1 screen.bmp";
    }
    else {
        BmpName = "2 screen.bmp";
    }


    MONITORINFO  info;
    info.cbSize = sizeof(MONITORINFO);

    BOOL monitorInfo = GetMonitorInfo(hMonitor, &info);

    if (monitorInfo) {

        DWORD Width = info.rcMonitor.right - info.rcMonitor.left;
        DWORD Height = info.rcMonitor.bottom - info.rcMonitor.top;

        DWORD FileSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (sizeof(RGBTRIPLE) + 1 * (Width*Height * 4));
        char *BmpFileData = (char*)GlobalAlloc(0x0040, FileSize);

        PBITMAPFILEHEADER BFileHeader = (PBITMAPFILEHEADER)BmpFileData;
        PBITMAPINFOHEADER  BInfoHeader = (PBITMAPINFOHEADER)&BmpFileData[sizeof(BITMAPFILEHEADER)];

        BFileHeader->bfType = 0x4D42; // BM
        BFileHeader->bfSize = sizeof(BITMAPFILEHEADER);
        BFileHeader->bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

        BInfoHeader->biSize = sizeof(BITMAPINFOHEADER);
        BInfoHeader->biPlanes = 1;
        BInfoHeader->biBitCount = 24;
        BInfoHeader->biCompression = BI_RGB;
        BInfoHeader->biHeight = Height;
        BInfoHeader->biWidth = Width;

        RGBTRIPLE *Image = (RGBTRIPLE*)&BmpFileData[sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)];
        RGBTRIPLE color;

        HDC CaptureDC = CreateCompatibleDC(DevC);
        HBITMAP CaptureBitmap = CreateCompatibleBitmap(DevC, Width, Height);
        SelectObject(CaptureDC, CaptureBitmap);
        BitBlt(CaptureDC, 0, 0, Width, Height, DevC, 0, 0, SRCCOPY | CAPTUREBLT);
        GetDIBits(CaptureDC, CaptureBitmap, 0, Height, Image, (LPBITMAPINFO)BInfoHeader, DIB_RGB_COLORS);

        DWORD Junk;
        HANDLE FH = CreateFileA(BmpName, GENERIC_WRITE, FILE_SHARE_WRITE, 0, CREATE_ALWAYS, 0, 0);
        WriteFile(FH, BmpFileData, FileSize, &Junk, 0);
        CloseHandle(FH);
        GlobalFree(BmpFileData);
    }

    return TRUE;
} 
int main() { 
    EnumDisplayMonitors(NULL, NULL, MonitorEnumProcCallback, 0);
    return 0;
}

but this return to me two black images.


回答1:


Thanks for help @Hans Passant.

I change the upper-left corner value

But I made another mistake, I send NULL in the EnumDisplayMonitors for the first argument.

Here is the changes:

BOOL CALLBACK MonitorEnumProcCallback(_In_  HMONITOR hMonitor, _In_  HDC DevC, _In_  LPRECT lprcMonitor, _In_  LPARAM dwData) {
    screenCounter++;

    char*BmpName;

    if (screenCounter == 1){
        BmpName = "1 screen.bmp";
    }
    else {
        BmpName = "2 screen.bmp";
    }

    MONITORINFO  info;
    info.cbSize = sizeof(MONITORINFO);

    BOOL monitorInfo = GetMonitorInfo(hMonitor, &info);

    if (monitorInfo) {

        DWORD Width = info.rcMonitor.right - info.rcMonitor.left;
        DWORD Height = info.rcMonitor.bottom - info.rcMonitor.top;

        DWORD FileSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (sizeof(RGBTRIPLE) + 1 * (Width*Height * 4));
        char *BmpFileData = (char*)GlobalAlloc(0x0040, FileSize);

        PBITMAPFILEHEADER BFileHeader = (PBITMAPFILEHEADER)BmpFileData;
        PBITMAPINFOHEADER  BInfoHeader = (PBITMAPINFOHEADER)&BmpFileData[sizeof(BITMAPFILEHEADER)];

        BFileHeader->bfType = 0x4D42; // BM
        BFileHeader->bfSize = sizeof(BITMAPFILEHEADER);
        BFileHeader->bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

        BInfoHeader->biSize = sizeof(BITMAPINFOHEADER);
        BInfoHeader->biPlanes = 1;
        BInfoHeader->biBitCount = 24;
        BInfoHeader->biCompression = BI_RGB;
        BInfoHeader->biHeight = Height;
        BInfoHeader->biWidth = Width;

        RGBTRIPLE *Image = (RGBTRIPLE*)&BmpFileData[sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)];
        RGBTRIPLE color;

        HDC CaptureDC = CreateCompatibleDC(DevC);
        HBITMAP CaptureBitmap = CreateCompatibleBitmap(DevC, Width, Height);
        SelectObject(CaptureDC, CaptureBitmap);
        BitBlt(CaptureDC, 0, 0, Width, Height, DevC, info.rcMonitor.left, info.rcMonitor.top, SRCCOPY | CAPTUREBLT);
        GetDIBits(CaptureDC, CaptureBitmap, 0, Height, Image, (LPBITMAPINFO)BInfoHeader, DIB_RGB_COLORS);

        DWORD Junk;
        HANDLE FH = CreateFileA(BmpName, GENERIC_WRITE, FILE_SHARE_WRITE, 0, CREATE_ALWAYS, 0, 0);
        WriteFile(FH, BmpFileData, FileSize, &Junk, 0);
        CloseHandle(FH);
        GlobalFree(BmpFileData);
    }

    return TRUE;
}

int main() {

    HDC DevC = GetDC(NULL);
    BOOL b = EnumDisplayMonitors(DevC, NULL, MonitorEnumProcCallback, 0);

    return 0;
}


来源:https://stackoverflow.com/questions/29045966/how-to-take-a-screenshot-of-a-second-screen-monitor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!