Python调用Win32 API:LoadImage获图标句柄后InsertMenuItem报错
在Python的pystray系统托盘库中,尝试通过Win32 API为每个菜单项添加图标。使用LoadImage加载.ico文件能成功获取HBITMAP句柄,但将该句柄填入MENUITEMINFO结构后,调用InsertMenuItem时出现WinError 87(参数错误)。调试确认hbmpItem已正确赋值,但加载.bmp文件并使用IMAGE_BITMAP类型时,InsertMenuItem可正常运行。寻求Win32 API相关的问题排查提示。
加载ICO的代码示例
icon = win32.LoadImage( None, iconpath, win32.IMAGE_ICON, ico_x, ico_y, win32.LR_LOADFROMFILE)
MENUITEMINFO结构填充代码
menu_item = win32.MENUITEMINFO( cbSize=ctypes.sizeof(win32.MENUITEMINFO), fMask=win32.MIIM_ID | win32.MIIM_STRING | win32.MIIM_STATE | win32.MIIM_FTYPE | win32.MIIM_SUBMENU | win32.MIIM_BITMAP , wID=len(callbacks), dwTypeData=descriptor.text, fState=0 | (win32.MFS_DEFAULT if descriptor.default else 0) | (win32.MFS_CHECKED if descriptor.checked else 0) | (win32.MFS_DISABLED if not descriptor.enabled else 0), fType=0 | (win32.MFT_RADIOCHECK if descriptor.radio else 0) | (win32.MFT_STRING if descriptor.text else 0) , hbmpItem= icon if descriptor.icon else None, hSubMenu=self._create_menu(descriptor.submenu, callbacks) if descriptor.submenu else None)
调用InsertMenuItem的代码
win32.InsertMenuItem(hmenu, i, True, ctypes.byref(menu_item))
可正常运行的加载BMP代码
icon = win32.LoadImage( None, bmppath, win32.IMAGE_BITMAP, ico_x, ico_y, win32.LR_LOADFROMFILE)
内容的提问来源于stack exchange,提问作者digfish
相关产品推荐
相关产品推荐

