如何解决如何从图标句柄中提取图标像素图数据
我需要从正在运行的应用程序中提取任务栏图标。
到目前为止,从状态栏读取文本数据是有效的,但是,我不知道如何从 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 举报,一经查实,本站将立刻删除。