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

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
V
Vulnerabilities – Threatpost
Cloudbric
Cloudbric
G
GRAHAM CLULEY
S
Securelist
Schneier on Security
Schneier on Security
Help Net Security
Help Net Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Project Zero
Project Zero
Spread Privacy
Spread Privacy
P
Privacy International News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
T
Tailwind CSS Blog
博客园_首页
有赞技术团队
有赞技术团队
Simon Willison's Weblog
Simon Willison's Weblog
Stack Overflow Blog
Stack Overflow Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Latest news
Latest news
T
Tor Project blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Attack and Defense Labs
Attack and Defense Labs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
O
OpenAI News
J
Java Code Geeks
T
Tenable Blog
K
Kaspersky official blog
AWS News Blog
AWS News Blog
S
Security @ Cisco Blogs
The GitHub Blog
The GitHub Blog
T
Threatpost
月光博客
月光博客
H
Heimdal Security Blog
Security Latest
Security Latest
The Hacker News
The Hacker News
Y
Y Combinator Blog
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
C
Cisco Blogs
美团技术团队
Microsoft Security Blog
Microsoft Security Blog
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
C
CERT Recently Published Vulnerability Notes
D
Docker
Google Online Security Blog
Google Online Security Blog
D
DataBreaches.Net
V
Visual Studio Blog
H
Help Net Security

博客园 - 王琳

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