You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python调用Win32 API:LoadImage获图标句柄后InsertMenuItem报错

问题:PyStray菜单项加载ICO图标时InsertMenuItem报WinError 87参数错误

在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)
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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 11:03:35