5

I use this code to get mouse position on screen and it's working. I also get cursor width and height. What I need is cursor icon in the moment I call function GetIconInfo. In ii iI have ii.hbmColor and ii.hbmMask. Value of hbmColor is 0x0, hbmMask is 0x2f0517f1. Can I extract mouse cursor from that two pointer and how?

  CURSORINFO cursorInfo = { 0 };
  cursorInfo.cbSize = sizeof(cursorInfo);

  HDC memoryDC = (HDC)malloc(100);
  memset(memoryDC, 0x00, 100);

  if (::GetCursorInfo(&cursorInfo))  {
    ICONINFO ii = {0};
    GetIconInfo(cursorInfo.hCursor, &ii);

    BITMAP bm;
    GetObject(ii.hbmMask,sizeof(BITMAP),&bm);

    DeleteObject(ii.hbmColor);
    DeleteObject(ii.hbmMask);
    ::DrawIcon(memoryDC, cursorInfo.ptScreenPos.x - ii.xHotspot, cursorInfo.ptScreenPos.y - ii.yHotspot, cursorInfo.hCursor);


    for(int i = 0; i < bm.bmWidth; i++){
        for(int j = 0; j < bm.bmHeight; j++){
            COLORREF c = GetPixel(memoryDC, i, j);
            printf("%x", c);

        }
    }
  }
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Nikola
  • 155
  • 2
  • 8
  • 3
    whoa, you cannot just cast random memory as a HDC. You need `CreateDC` / `CreateCompatibleDC` / `GetDC`. Windows GDI is tricky to get used to, but it makes sense eventually. Make sure you do error checking and handle problems one at a time, keeping MSDN close by. – tenfour Aug 18 '10 at 16:02
  • @tenfour: my eye popped out when I saw that. Happily, I wear glasses... – peterchen Aug 18 '10 at 17:48

2 Answers2

1
  CURSORINFO cursorInfo = { 0 };
  cursorInfo.cbSize = sizeof(cursorInfo);

  if (::GetCursorInfo(&cursorInfo))
  {
    ICONINFO ii = {0};
    GetIconInfo(cursorInfo.hCursor, &ii);
    DeleteObject(ii.hbmColor);
    DeleteObject(ii.hbmMask);
    ::DrawIcon(memoryDC, cursorPos.x - ii.xHotspot, cursorPos.y - ii.yHotspot, cursorInfo.hCursor);
  }
tenfour
  • 36,141
  • 15
  • 83
  • 142
  • hi - sorry for the short answer. This is code I had laying around in a project. It doesn't really matter what memoryDC is - it's the DC where you want to draw the icon to. The important thing is that the handle to the icon is cursorInfo.hCursor. – tenfour Aug 18 '10 at 13:54
  • I use the code up (my post, I do edit) and in console a get just ffffffffffffffffff. Where I made a mistake? I just want to access cursor icon in memory. Thank you very much for all your answers. – Nikola Aug 18 '10 at 15:26
  • bmBits of Bitmap are 0x000000 – Nikola Aug 18 '10 at 15:32
  • I had the same result of you, but I solved it according to the this answer. http://stackoverflow.com/a/10469709/1837846 – sMiLo Oct 23 '14 at 06:31
-2

the cursor informations are formatted like explained here : http://www.daubnet.com/en/file-format-cur

you have to get each pixel from each bit of the data buffer, and not from each byte, so 1 byte = 8 pixels. Also, be careful with some applications wich may have special sized cursors (not multiple of 8), like 26x23 In this case you'll have to ignore the last bits of each line. with a line of 26 pixels, you'll get 4 bytes, you'll read the first 3 bytes to get the 24 first pixels, and then read 2 bits of the 4th byte to get the last 2 pixels, and then ignore the last 6 bits before jumping to the next line.

Alesk
  • 147
  • 1
  • 2