HICON/HBITMAP 到 QIcon/QPixmap/QImage/任何带有 Q 的东西 [英] HICON/HBITMAP to QIcon/QPixmap/QImage/Anything with a Q in it

查看:107
本文介绍了HICON/HBITMAP 到 QIcon/QPixmap/QImage/任何带有 Q 的东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Windows 中的文件中获取 48x48 或 256x256 的图标,但遇到了似乎是一个死胡同.目前我在 python 中有一个 HICON 句柄(因为 PySides QFileIconProvider 只返回 32x32 图标),我想在 pyside 窗口中显示它,但是像 QPixmap.fromHICON/HBITMAP 这样的函数没有实现,而且似乎已经从源代码中删除从 Qt 4.8(?) 开始.另外,我试图避免将图标保存到文件中.

I'm trying to get 48x48 or 256x256 icons from files in Windows and have come across what seems like a dead-end. At the moment I have a HICON handle(since PySides QFileIconProvider only returns 32x32 icons) in python which I would like to show in a pyside window but functions like QPixmap.fromHICON/HBITMAP are not implemented and also seems to have been removed from the source since Qt 4.8(?). Also, I'm trying to avoid having to save the icon to a file.

那么,有没有什么办法可以将 HICON 或任何其他可以将其转换为任何类型的 PySide 对象的东西?

So, is there any way to get a HICON or possibly any other things you can turn it into, to any kind of PySide object?

我一直在尝试简单地重写 python 中的 WinHBITMAP 函数中的旧函数,但效果不佳.我不确定我应该如何将 src 行转换为 python,我也不知道如何更改 QImage.scanLine() 返回的内存缓冲区的值

I've been trying to simply rewrite the old function fromWinHBITMAP function in python but it isn't going great. I'm uncertain how I should translate the src line into python and I don't either have any idea how I change the value of the memory buffer returned by QImage.scanLine()

for (int y=0; y<h; ++y) {
            QRgb *dest = (QRgb *) image.scanLine(y);
            const QRgb *src = (const QRgb *) (data + y * bytes_per_line);
            for (int x=0; x<w; ++x) {
                dest[x] = src[x] | mask;
            }
        }

目前我使用 win32api 从 HICON 创建一个 PyCBITMAP 并检索位列表.

At the moment I create a PyCBITMAP from the HICON with the win32api and retrieves the list of bits.

for y in range(0, hIcon.height):
    dest = i.scanLine(y)
    src = bitmapbits[y*hIcon.widthBytes:(y*hIcon.widthBytes)+hIcon.widthBytes]

    for x in range(0, hIcon.width):
        dest[x] = bytes(ctypes.c_uint32(src[x] | 0))

这会导致ValueError: cannot modify size of memoryview object"

This results in "ValueError: cannot modify size of memoryview object"

该函数的来源在这里:http://www.qtcentre.org/threads/19188-Converting-from-HBitmap-to-a-QPixmap?p=94747#post94747

推荐答案

已修复!

def iconToQImage(hIcon):
    hdc = win32ui.CreateDCFromHandle(win32gui.GetDC(0))
    hbmp = win32ui.CreateBitmap()
    hbmp.CreateCompatibleBitmap(hdc, hIcon.width, hIcon.height)
    hdc = hdc.CreateCompatibleDC()
    hdc.SelectObject(hbmp)

    win32gui.DrawIconEx(hdc.GetHandleOutput(), 0, 0, hIcon.hIcon, hIcon.width, hIcon.height, 0, None, 0x0003)

    bitmapbits = hbmp.GetBitmapBits(True)
    image = QtGui.QImage(bitmapbits, hIcon.width, hIcon.height, QtGui.QImage.Format_ARGB32_Premultiplied)
    return image

这篇关于HICON/HBITMAP 到 QIcon/QPixmap/QImage/任何带有 Q 的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆