Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include mouse cursor in screen capture

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?

like image 804
Tez Avatar asked Jun 22 '14 04:06

Tez


People also ask

How do I show my mouse pointer in screenshot?

From the Options menu, select Capture/Screenshot. This will bring up the Capture Setup window. In the Capture Setup window, in the Options section, check Include mouse cursor.

How do you snip with mouse pointer?

You can see a Task Settings window, select the option “Capture”. Here make sure that “Show cursor in the Screenshots” is checked. Now place the cursor when you want to take the screenshot and press ctrl + PrtSc, now you can move the mouse and select the portion you want to take a screenshot.

How do I drag and screenshot with mouse?

Press “Windows + Shift + S”. Your screen will appear grayed out and your mouse cursor will change. Click and drag on your screen to select the part of your screen you want to capture. A screenshot of the screen region you selected will be copied to your clipboard.


1 Answers

#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;
}
like image 67
Sushant Avatar answered Oct 15 '22 22:10

Sushant