Screen capture with mouse cursor

I used the following code to get a screenshot on Windows.

 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);

However, the mouse cursor is not displayed.

How do I get the cursor? or can a library do this?

Thanks in advance.

+3
source share
1 answer

After your BitBlt and before you select a bitmap from hdcMem, you can do this:

CURSORINFO cursor = { sizeof(cursor) };
::GetCursorInfo(&cursor);
if (cursor.flags == CURSOR_SHOWING) {
    RECT rcWnd;
    ::GetWindowRect(hwnd, &rcWnd);
    ICONINFOEXW info = { sizeof(info) };
    ::GetIconInfoExW(cursor.hCursor, &info);
    const int x = cursor.ptScreenPos.x - rcWnd.left - rc.left - info.xHotspot;
    const int y = cursor.ptScreenPos.y - rcWnd.top  - rc.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);
}

, , , , , ( ) . . , . . , , .

, , :

  • , .
  • , . , .
+2

Source: https://habr.com/ru/post/1721153/


All Articles