2016-06-30 327 views
0

我想让我的(全局)鼠标光标图标在QPixmap中。Qt Windows获取鼠标光标图标

阅读Qt和MSDN文档后,我想出了这样一段代码:

我不确定混合HCURSOR和惠康,但我已经看到了一些例子,他们做到这一点。

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)。 出了什么问题?

+0

为什么你认为'CURSORINFO'结构的'hCursor'成员是图标的处理? – mvidelgauz

+0

是的,HCURSOR和HICON是相同的。我不知道为什么这不起作用。 'ci.hCursor'实际上是否包含有效的句柄?如果是这样,我想'问题在于'QtWin :: fromHICON',因为我已经多次使用相同的代码来获取鼠标光标位图。 –

+0

根据这个答案:http://stackoverflow.com/questions/10469538/winapi-get-mouse-cursor-icon 他们在DrawIcon()中使用HCURSOR – eKKiM

回答

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