惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

I
Intezer
The Register - Security
The Register - Security
博客园 - 三生石上(FineUI控件)
博客园_首页
量子位
Hugging Face - Blog
Hugging Face - Blog
Engineering at Meta
Engineering at Meta
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
N
Netflix TechBlog - Medium
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
GbyAI
GbyAI
D
Docker
M
MIT News - Artificial intelligence
Jina AI
Jina AI
The GitHub Blog
The GitHub Blog
V
Visual Studio Blog
T
Tailwind CSS Blog
S
SegmentFault 最新的问题
Microsoft Azure Blog
Microsoft Azure Blog
G
Google Developers Blog
有赞技术团队
有赞技术团队
IT之家
IT之家
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Troy Hunt's Blog
Cyberwarzone
Cyberwarzone
Recorded Future
Recorded Future
H
Help Net Security
L
LINUX DO - 热门话题
罗磊的独立博客
Google DeepMind News
Google DeepMind News
NISL@THU
NISL@THU
SecWiki News
SecWiki News
T
Threat Research - Cisco Blogs
T
Threatpost
Cloudbric
Cloudbric
G
GRAHAM CLULEY
S
Security @ Cisco Blogs
Hacker News - Newest:
Hacker News - Newest: "LLM"
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
P
Proofpoint News Feed
V2EX - 技术
V2EX - 技术
L
LangChain Blog
The Cloudflare Blog
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
B
Blog
T
The Blog of Author Tim Ferriss

博客园 - 王琳

windows网络与通信设计 书签 书签 书签 书签 书签 心如莲花读后感 - 王琳 - 博客园 调试代码和扑做工具 学习底层 pe校验位 线性代数的好网站 内核编程书单 现在又放松警惕了,做人松松垮垮 书签 asp.net的外挂工具 目标要明确 节制 免费加班 克制自己的欲望,努力超越自我
Windows Shell扩展系列文章2
王琳 · 2011-03-28 · via 博客园 - 王琳

在“Windows Shell扩展系列文章 1 - .NET 4 编写Windows Shell上下文菜单扩展”一文中,我们介绍了如何使用.NET 4编写VC#或VB.NET代码创建Windows Shell上下文菜单扩展。
 


很多开发人员想进一步知道:如何为扩展的菜单项加上位图图标。本文便通过一个来自于微软一站式示例代码库的示例代码为你演示如何为扩展的菜单项加上位图图标。
 
示例代码下载:C#, VB.NET
 


实现细节
 
Windows Shell上下文菜单中的菜单项是通过实现IContextMenu.QueryContextMenu添加上去的。
 
public int QueryContextMenu(
    IntPtr hMenu,
    uint iMenu,
    uint idCmdFirst,
    uint idCmdLast,
    uint uFlags)
{
    ......
    // Use either InsertMenu or InsertMenuItem to add menu items.
    MENUITEMINFO mii = new MENUITEMINFO();
    mii.cbSize = (uint)Marshal.SizeOf(mii);
    mii.fMask = MIIM.MIIM_STRING | MIIM.MIIM_FTYPE | MIIM.MIIM_ID | MIIM.MIIM_STATE;
    mii.wID = idCmdFirst + IDM_DISPLAY;
    mii.fType = MFT.MFT_STRING;
    mii.dwTypeData = this.menuText;
    mii.fState = MFS.MFS_ENABLED;
    if (!NativeMethods.InsertMenuItem(hMenu, iMenu, true, ref mii))
    {
        return Marshal.GetHRForLastWin32Error();
    }
    ......
}
 
其中MENUITEMINFO结构可支持在菜单项文字旁添加位图图标。你只需要为MENUITEMINFO.fMask添加上MIIM_BITMAP,并将MSENUITEMINFO.hbmpItem指向一16x16的位图句柄。修改后的代码示例如下:
 
public int QueryContextMenu(
    IntPtr hMenu,
    uint iMenu,
    uint idCmdFirst,
    uint idCmdLast,
    uint uFlags)
{
    ......
    // Use either InsertMenu or InsertMenuItem to add menu items.
    MENUITEMINFO mii = new MENUITEMINFO();
    mii.cbSize = (uint)Marshal.SizeOf(mii);
    mii.fMask = MIIM.MIIM_BITMAP | MIIM.MIIM_STRING | MIIM.MIIM_FTYPE |
    MIIM.MIIM_ID | MIIM.MIIM_STATE;
    mii.wID = idCmdFirst + IDM_DISPLAY;
    mii.fType = MFT.MFT_STRING;
    mii.dwTypeData = this.menuText;
    mii.fState = MFS.MFS_ENABLED;
    mii.hbmpItem = this.menuBmp;
    if (!NativeMethods.InsertMenuItem(hMenu, iMenu, true, ref mii))
    {
        return Marshal.GetHRForLastWin32Error();
    }
    ......
}
 
"this.menuBmp" 在该上下文菜单扩展类的构造函数内被初始化:
 
public FileContextMenuExt()
{
    // Load the bitmap for the menu item.
    Bitmap bmp = Resources.OK; // A 16x16 bmp added to the Resources of the project.
    bmp.MakeTransparent(bmp.GetPixel(0, 0));
    this.menuBmp = bmp.GetHbitmap();
}
 
然后在析构函数内释放该句柄:
 
~FileContextMenuExt()
{
    if (this.menuBmp != IntPtr.Zero)
    {
        NativeMethods.DeleteObject(this.menuBmp);
        this.menuBmp = IntPtr.Zero;
    }
}
 
有了这些修改,上下文菜单项就会显示你所指定的位图图标。
 


注意
 
1. 务必将Bitmap.GetHbitmap返回的位图在该类对象被析构的时候释放掉,否则将造成句柄溢出。
 
下述示例代码演示了一个开发人员常犯的错误:
 
    // Use either InsertMenu or InsertMenuItem to add menu items.
    MENUITEMINFO mii = new MENUITEMINFO();
    mii.cbSize = (uint)Marshal.SizeOf(mii);
    mii.fMask = MIIM.MIIM_BITMAP | MIIM.MIIM_STRING | MIIM.MIIM_FTYPE |
    MIIM.MIIM_ID | MIIM.MIIM_STATE;
    mii.wID = idCmdFirst + IDM_DISPLAY;
    mii.fType = MFT.MFT_STRING;
    mii.dwTypeData = this.menuText;
    mii.fState = MFS.MFS_ENABLED;
    mii.hbmpItem = Resources.OK.GetHbitmap(); // This will leak the bitmap handle!
    if (!NativeMethods.InsertMenuItem(hMenu, iMenu, true, ref mii))
    {
          return Marshal.GetHRForLastWin32Error();
    }
 
2. 务必将MENUITEMINFO.fType 指向上述bmp的句柄。比如,
 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/MSCodeSample/archive/2011/03/28/6282554.aspx