Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用shell扩展将图标插入上下文菜单_Python_Windows_Shell_Winapi_Pywin32 - Fatal编程技术网

Python 使用shell扩展将图标插入上下文菜单

Python 使用shell扩展将图标插入上下文菜单,python,windows,shell,winapi,pywin32,Python,Windows,Shell,Winapi,Pywin32,我正在使用pywin32制作一个Windows shell扩展,并通过QueryContextMenu添加一个右键单击上下文菜单项。我正试图从imageres.dll中插入一个系统图标。我基本上是在尝试使用ExtractIconEx和GetIconInfo: def QueryContextMenu(self, hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags): # ... Some other code ...

我正在使用pywin32制作一个Windows shell扩展,并通过
QueryContextMenu
添加一个右键单击上下文菜单项。我正试图从
imageres.dll
中插入一个系统图标。我基本上是在尝试使用
ExtractIconEx
GetIconInfo

def QueryContextMenu(self, hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags):

        # ... Some other code ...

        item_text = 'New context menu entry'

        large, small = win32gui.ExtractIconEx(r'C:\Windows\System32\imageres.dll', 202)
        bitmap = win32gui.GetIconInfo(small[0])[4]

        item, extras = win32gui_struct.PackMENUITEMINFO(text=item_text,
                                                        hbmpItem=bitmap,
                                                        wID=idCmdFirst)

        win32gui.InsertMenuItem(hMenu, indexMenu+1, 1, item)

        return 1

此代码不会引发任何错误,但不会显示图标,并且最终也不会调用该命令。谢谢您的帮助。

如果您只关心图标,那么您只需创建一个普通的静态注册表菜单项,并在注册表中设置图标值即可。这将在Windows7和更高版本上运行,并且不需要创建shell扩展。在旧版本中,如果需要图标,则必须创建shell扩展

在Vista上,您可以将其设置为菜单项“位图”

在XP和更早版本上,您应该使用或自定义绘图


仅使用普通位图存在透明度问题。Windows95和NT4?不支持普通位图方法,并且使用选中/未选中位图存在大小和透明度问题。

InsertMenu()
长期以来一直被弃用,您应该改用
InsertMenuItem()
,它允许您在单个函数调用中同时设置文本和位图。非常感谢。我想我是从一个过时的样品开始工作的。我将尝试使用InsertMenuItem()实现,看看是否还有更多的成功。我现在将其更改为InsertMenuItem(),但它仍然不起作用。使用InsertMenuItem然后设置位图与使用InsertMenuItem相同,当显示菜单时,该项将以相同的方式运行。是的,我做了更改以响应Remy Lebeau的评论,但我的基本问题仍然存在。谢谢,但我的扩展包含了更多的逻辑。为了回答这个问题,我简化了一些事情。ARGB位图方法在7和更高版本上也是有效的。