如何从图标句柄中提取图标像素图数据

问题描述

我需要从正在运行的应用程序中提取任务栏图标。 到目前为止,从状态栏读取文本数据是有效的,但是,我不知道如何从 sb_retcode = win32api.SendMessage(handle,SB_GETTEXTLENGTH,item,0) 返回的句柄中提取图标数据。

这是我的示例代码

MAX_LEN = 4096

def get_process_handle(dwProcessId,dwDesiredAccess,bInheritHandle=False):
    handle = OpenProcess(dwDesiredAccess,bInheritHandle,dwProcessId)
    if handle is None or handle == 0:
        raise Exception('Error: %s' % GetLastError())
    return handle

def allocate(hProcess,lpAddress,dwSize,flAllocationType,flProtect):
    lpBuffer = VirtualAllocEx(hProcess,flProtect)
    if lpBuffer is None or lpBuffer == 0:
        raise Exception('Error: %s' % GetLastError())
    return lpBuffer

# -- get pid for valid window handle
_,pid = win32process.GetwindowThreadProcessId(hWindow) 
handle = get_process_handle(pid,win32con.PROCESS_ALL_ACCESS,False)

p_buffer = allocate(handle,MAX_LEN,win32con.MEM_RESERVE | win32con.MEM_COMMIT,win32con.PAGE_EXECUTE_READWRITE)

item = 2  # valid item no

sb_retcode = win32api.SendMessage(hWindow,0) 
sb_length = sb_retcode & 0xFFFF 
sb_type = (sb_retcode >> 16) & 0xFFFF

if sb_type == 0 and sb_length > 0:
        # text data
        bufferSize = sb_retcode
        buffer = ctypes.create_string_buffer(bufferSize)
        bytesRead = c_ulong(0)

        win32api.SendMessage(hWindow,SB_GETTEXT,i,p_buffer)
        if ReadProcessMemory(handle,p_buffer,buffer,bufferSize,bytesRead):
            print('-> ',buffer.value)
        else:
            ErrorNum = win32api.GetLastError()
            print("Error Read Memory: ",ErrorNum,win32api.FormatError(ErrorNum))

    elif sb_type == SBT_OWNERDRAW:
        # icon data
        # sb_retcode should be handle to the icon
        print('-> handle for icon',sb_retcode)

        # ?? how to get bitmap data from icon handle ??

SendMessage 确实返回了图标的有效句柄,但如何将其转换为位图?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)