Include mouse cursor in screen capture

99封情书 提交于 2019-12-21 05:44:15

问题


I use CreateDC / BitBlt / GetDIBits etc. to capture the screen, but the cursor is not captured. Is there some simple argument or something to have it included?


回答1:


Further to the discussion that occurred in the comments, I had the chance to further investigate the question. As a result, I came up with the following code that will grab the current cursor's HBITMAP and draw it to the screen.

Since the cursor is actually an HICON, it comes with a mask. Initially, I just did a simple BitBlt - however, I got a 32x32 black sqaure with the cursor in the top left 1/4 or so.

I then investigated using MaskBlt. Depending on where the cursor is when the app is started, I get either the wait cursor, a NS resize cursor, or the standard pointer. I guess you could start a timer and add a WM_TIMER handler to fire a couple of times a second in order to get a real-time update of the cursor as it was used in other windows in the system. It seemed like a mere curiosity to do something like that so I didn't bother.

EDIT: I actually did start a timer in WM_INITDIALOG and handle it in WM_TIMER. You can now see the image updated 10 times a second. For some reason, the I-beam cursor doesn't seem to be displayed at all - a case for further investigation as needed, I guess.

Here's the complete listing (except for resource.rc and resource.h - just create a dialog app and make sure the dialog's resource ID is used inside Main in the call to DialogBox)

#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include "resource.h"

HINSTANCE hInst;

HBITMAP getCursorHBITMAP(HBITMAP *maskBmp)
{
    CURSORINFO pci;
    ICONINFO iconinfo;
    HBITMAP result;

    pci.cbSize = sizeof(pci);
    GetCursorInfo(&pci);

    if (GetIconInfo(pci.hCursor,&iconinfo))
    {
        result = iconinfo.hbmColor;
        if (maskBmp)
            *maskBmp = iconinfo.hbmMask;
    }
    else
        result = NULL;

    return result;
}

BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_INITDIALOG:
        {
            SetTimer(hwndDlg, 1, 100, NULL);
        }
        return TRUE;

        case WM_TIMER:
        {
            InvalidateRect(hwndDlg, NULL, true);
        }
        return 0;

        case WM_ERASEBKGND:
        {
            HDC hdc = (HDC)wParam;
            RECT mRect;
            GetClientRect(hwndDlg, &mRect);
            FillRect(hdc, &mRect, (HBRUSH)GetStockObject(GRAY_BRUSH));
        }
        return 1;

        case WM_PAINT:
        {
            HBITMAP oldBm, cursorBmp, maskBmp;

            cursorBmp = getCursorHBITMAP(&maskBmp);
            if (cursorBmp)
            {
                HDC hdc;
                PAINTSTRUCT ps;
                HDC memDC;
                BITMAP bm;

                hdc = BeginPaint(hwndDlg, &ps);
                memDC = CreateCompatibleDC(hdc);
                oldBm = (HBITMAP) SelectObject(memDC, cursorBmp);

                GetObject(cursorBmp, sizeof(bm), &bm);
    //            printf("Cursor size: %d x %d\n", bm.bmWidth, bm.bmHeight);

    //            BitBlt(hdc, 10,10, 32,32, memDC, 0,0, SRCCOPY);
                MaskBlt(hdc, 10,10, bm.bmWidth, bm.bmHeight, memDC, 0,0, maskBmp, 0,0, MAKEROP4(SRCPAINT,SRCCOPY) );


                SelectObject(memDC, oldBm);
                DeleteDC(memDC);
                EndPaint(hwndDlg, &ps);
            }
        }
        return 0;

        case WM_CLOSE:
        {
            EndDialog(hwndDlg, 0);
        }
        return TRUE;

        case WM_COMMAND:
        {
            switch(LOWORD(wParam))
            {
            }
        }
        return TRUE;
    }
    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst=hInstance;
    InitCommonControls();
    return DialogBox(hInst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DlgMain);
}



回答2:


#include <Windows.h>
#include <stdio.h>
#include <assert.h>

void scrshot() {
    HWND hwnd = GetDesktopWindow();
    HDC hdc = GetWindowDC(hwnd);
    HDC hdcMem = CreateCompatibleDC(hdc);
    int cx = GetDeviceCaps(hdc, HORZRES);
    int cy = GetDeviceCaps(hdc, VERTRES);
    HBITMAP hbitmap(NULL);
    hbitmap = CreateCompatibleBitmap(hdc, cx, cy);
    SelectObject(hdcMem, hbitmap);
    BitBlt(hdcMem, 0, 0, cx, cy, hdc, 0, 0, SRCCOPY);
    CURSORINFO cursor = { sizeof(cursor) };
    GetCursorInfo(&cursor);
    if (cursor.flags == CURSOR_SHOWING) {
        RECT rect;
        GetWindowRect(hwnd, &rect);
        ICONINFO info = { sizeof(info) };
        GetIconInfo(cursor.hCursor, &info);
        const int x = cursor.ptScreenPos.x - rect.left - rect.left - info.xHotspot;
        const int y = cursor.ptScreenPos.y - rect.top - rect.top - info.yHotspot;
        BITMAP bmpCursor = { 0 };
        GetObject(info.hbmColor, sizeof(bmpCursor), &bmpCursor);
        DrawIconEx(hdcMem, x, y, cursor.hCursor, bmpCursor.bmWidth, bmpCursor.bmHeight,
            0, NULL, DI_NORMAL);
    }
}

int main(){
    scrshot();
    return 0;
}


来源:https://stackoverflow.com/questions/24348121/include-mouse-cursor-in-screen-capture

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