c++ - Qt Windows 获取鼠标光标图标

标签 c++ qt qpixmap mouse-cursor

我正在尝试在 QPixmap 中获取我的(全局)鼠标光标图标。

在阅读了 Qt 和 MSDN 文档后,我想到了这段代码:

我不确定混合使用 HCURSOR 和 HICON,但我看到了一些他们这样做的例子。

QPixmap MouseCursor::getMouseCursorIconWin()
{
    CURSORINFO ci;
    ci.cbSize = sizeof(CURSORINFO);

    if (!GetCursorInfo(&ci))
        qDebug() << "GetCursorInfo fail";

    QPixmap mouseCursorPixmap = QtWin::fromHICON(ci.hCursor);
    qDebug() << mouseCursorPixmap.size();

    return mouseCursorPixmap;
}

但是,我的 mouseCursorPixmap 大小始终是 QSize(0,0)。 出了什么问题?

最佳答案

我不知道为什么上面的代码不起作用。

但是下面的代码示例确实有效:

QPixmap MouseCursor::getMouseCursorIconWin()
{
    // Get Cursor Size
    int cursorWidth = GetSystemMetrics(SM_CXCURSOR);
    int cursorHeight = GetSystemMetrics(SM_CYCURSOR);

    // Get your device contexts.
    HDC hdcScreen = GetDC(NULL);
    HDC hdcMem = CreateCompatibleDC(hdcScreen);

    // Create the bitmap to use as a canvas.
    HBITMAP hbmCanvas = CreateCompatibleBitmap(hdcScreen, cursorWidth, cursorHeight);

    // Select the bitmap into the device context.
    HGDIOBJ hbmOld = SelectObject(hdcMem, hbmCanvas);

    // Get information about the global cursor.
    CURSORINFO ci;
    ci.cbSize = sizeof(ci);
    GetCursorInfo(&ci);

    // Draw the cursor into the canvas.
    DrawIcon(hdcMem, 0, 0, ci.hCursor);

    // Convert to QPixmap
    QPixmap cursorPixmap = QtWin::fromHBITMAP(hbmCanvas, QtWin::HBitmapAlpha);

    // Clean up after yourself.
    SelectObject(hdcMem, hbmOld);
    DeleteObject(hbmCanvas);
    DeleteDC(hdcMem);
    ReleaseDC(NULL, hdcScreen);

    return cursorPixmap;
}

关于c++ - Qt Windows 获取鼠标光标图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38125780/

相关文章:

c++ - QPixmap 绘制性能问题

python-3.x - 如何在 PySide2 中使用 QLabel 加载图像

android - 将 QVideoFrame 直接转换为 QPixmap

c++ - 如何编写具有不同数量的信息参数的 C++ 断言宏?

c++ - BlockingQueue 的 QWaitCondition : Destroyed while threads are still waiting

c++ - 在 Apache 服务器中设置指令时出现不兼容的指针类型错误

c++ - $QTDIR 指的是 Visual Studio 中的错误目录

c++ - 用于解析 xrandr 响应的正则表达式

c++ - C++ 中的运算符 [&]

c++ - QXmlStreamWriter,命名空间和前缀