windows 如何获得鼠标光标图标VS C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3509951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:03:37  来源:igfitidea点击:

How to get mouse cursor icon VS c++

c++windowsmousecursor

提问by Nikola

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?

我使用此代码在屏幕上获取鼠标位置并且它正在工作。我也得到光标的宽度和高度。我需要的是在调用函数 GetIconInfo 时的光标图标。在 ii iI 中有 ii.hbmColor 和 ii.hbmMask。hbmColor 的值为 0x0,hbmMask 的值为 0x2f0517f1。我可以从那两个指针中提取鼠标光标吗?如何提取?

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

        }
    }
  }

回答by tenfour

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

回答by Alesk

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

光标信息的格式如下所述: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.

您必须从数据缓冲区的每一位而不是每个字节中获取每个像素,因此 1 个字节 = 8 个像素。此外,请注意某些可能具有特殊尺寸游标(不是 8 的倍数)的应用程序,例如 26x23 在这种情况下,您必须忽略每行的最后一位。一行 26 个像素,你会得到 4 个字节,你将读取前 3 个字节以获得前 24 个像素,然后读取第 4 个字节的 2 位以获得最后 2 个像素,然后忽略最后一个跳转到下一行之前的 6 位。