2

I using GetCursorInfo to capture cursor but while saving the cursor as icon getting some black rectangle over on icon.

For windows default cursors are fine but few custom cursor I facing this issue http://www.cursors-4u.com/

Placed one example cursor icon in link https://www.google.com/search?q=cursor+icon&rlz=1C1CHBD_enIN789IN789&source=lnms&tbm=isch&sa=X&ved=0ahUKEwix9aj_gq_iAhWCXCsKHcusD0oQ_AUIDigB&biw=1366&bih=657#imgrc=rOxlbRoBfnKs8M:

HRESULT SaveIcon(HICON hIcon, const char* path) 
{
    // Create the IPicture intrface
    PICTDESC desc = { sizeof(PICTDESC) };
    desc.picType = PICTYPE_ICON;
    desc.icon.hicon = hIcon;
    IPicture* pPicture = 0;
    HRESULT hr = OleCreatePictureIndirect(&desc, IID_IPicture, FALSE, (void**)&pPicture);
    if (FAILED(hr)) return hr;

    // Create a stream and save the image
    IStream* pStream = 0;
    CreateStreamOnHGlobal(0, TRUE, &pStream);
    LONG cbSize = 0;
    hr = pPicture->SaveAsFile(pStream, TRUE, &cbSize);

    // Write the stream content to the file
    if (!FAILED(hr)) 
    {
        HGLOBAL hBuf = 0;
        GetHGlobalFromStream(pStream, &hBuf);
        void* buffer = GlobalLock(hBuf);
        HANDLE hFile = CreateFileA(path, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
        if (!hFile) 
        {
            hr = HRESULT_FROM_WIN32(GetLastError());
        }
        else 
        {
            DWORD written = 0;
            WriteFile(hFile, buffer, cbSize, &written, 0);
            CloseHandle(hFile);
        }
        GlobalUnlock(buffer);
    }
    // Cleanup
    pStream->Release();
    pPicture->Release();
    return hr;
}

//Capture cursor.
CURSORINFO getHCursor()
{
  CURSORINFO cursorInfo;
  cursorInfo.cbSize = sizeof(CURSORINFO);

  if (GetCursorInfo(&cursorInfo) == 0) 
  { 
    MessageBox(NULL, _T("Exception : GetCursorInfo creation failed"),_T("message"),MB_OK|MB_SYSTEMMODAL);       
    cursorInfo.hCursor = NULL;
    return cursorInfo;
  }

  return cursorInfo;
}

//Main Call
int _tmain(int argc, _TCHAR* argv[])
{
    while (true)
    {
        CURSORINFO CursorInfo = getHCursor();
        if (CursorInfo.hCursor == NULL) 
        {           
            ::Sleep(MinSleep);
            continue;
        }       

        SaveIcon((HICON)CursorInfo.hCursor, "C:\\Users\\Desktop\\myicon.ico");

        Sleep(MaxSleep);
    }   
    return 0;
}

My agenda is to capture cursor and save cursor in to icon(.ico) file or load in to buffer.

Is any other way I can write cursor data in to icon file or buffer ?

Krish
  • 376
  • 3
  • 14
  • This saves all icons correctly but fails on cursors? The on-disk .cur format is not exactly the same as .ico – Anders May 22 '19 at 13:05
  • @Anders Then how can I identify the captured data is icon or cursor, Is any generic way. – Krish May 23 '19 at 05:38
  • @Anders For windows all standard cursors its working fine but some custom cursors like photoshop have their own type of cursors their my writing part fails it writes cursor with black rectangle box and even few hand shaped cursors in gmail. – Krish May 23 '19 at 06:43

1 Answers1

0

The ICONINFO struct contains two members, hbmMask and hbmColor, that contain the mask and color bitmaps, respectively, for the cursor (see the MSDN page for ICONINFO for the official documentation).

When you call GetIconInfo() for the default cursor, the ICONINFO struct contains both valid mask and color bitmaps.

There is probably a better way to render the cursor that the BitBlt() - BitBlt() - MakeTransparent() combination of method calls.

Refer to the @Tarsier approach, although it's C #, but the idea is the same.

Link: Capturing the Mouse cursor image

Strive Sun
  • 5,988
  • 1
  • 9
  • 26