gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:25:43 38 4
gpt4 key购买 nike

我正在尝试在 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/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com