(C#)Windows Shell 外壳编程系列 - ContextMenu 注册文件右键菜单
2011-01-12 15:41
776 查看
从本节起,我所要讲述的是对 Windows 系统的“Shell 扩展”。“Shell 扩展”从字面上分两个部分:Shell 与 Extension。Shell 指 Windows Explorer,而Extension 则指由你编写的当某一预先约定好的事件(如在以. doc 为后缀的文件图标上单击右键)发生时由 Explorer 调用执行的代码。因此一个“Shell 扩展”就是一个为 Explorer 添加功能的 COM 对象。
“Shell 扩展”有很多种类型,每种类型都在各自不同的事件发生时被调用运行,但也有一些扩展的类型和调用情形是非常相似的。
现在你可能想知道“Shell 扩展”到底是什么样的,不过我还是乐意把我后面所实现的技术效果直接展示出来。以下三副图片分别代表了三种“Shell 扩展”:
(1)实现类似 WinRAR 的右键菜单
注册扩展
[System.Runtime.InteropServices.ComRegisterFunctionAttribute()]
static void RegisterServer(String str1)
{
try
{
//注册 DLL
RegistryKey root;
RegistryKey rk;
root = Registry.LocalMachine;
rk = root.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved", true);
rk.SetValue(GUID, KEYNAME);
rk.Close();
root.Close();
//注册文件
RegTXT();
}
catch{
}
}
[System.Runtime.InteropServices.ComUnregisterFunctionAttribute()]
static void UnregisterServer(String str1)
{
try
{
//注销动态库
RegistryKey root;
RegistryKey rk;
root = Registry.LocalMachine;
rk = root.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved", true);
rk.DeleteValue(GUID);
rk.Close();
root.Close();
//注销文件
UnRegTXT();
}
catch
{
}
}
private static void RegTXT()
{
RegistryKey root;
RegistryKey rk;
root = Registry.ClassesRoot;
rk = root.OpenSubKey(".txt");
string txtclass = (string)rk.GetValue("");
if (string.IsNullOrEmpty(txtclass))
{
txtclass = "TXT";
rk.SetValue("", txtclass);
}
rk.Close();
rk = root.CreateSubKey(txtclass + "\\shellex\\ContextMenuHandlers\\" + KEYNAME);
rk.SetValue("", GUID);
rk.Close();
rk = root.CreateSubKey(txtclass + "\\shellex\\IconHandler");
rk.SetValue("", GUID);
rk.Close();
rk = root.CreateSubKey(txtclass + "\\shellex\\{00021500-0000-0000-C000-000000000046}");
rk.SetValue("", GUID);
rk.Close();
}
private static void UnRegTXT()
{
RegistryKey root;
RegistryKey rk;
root = Registry.ClassesRoot;
rk = root.OpenSubKey(".txt");
rk.Close();
string txtclass = (string)rk.GetValue("");
if (!string.IsNullOrEmpty(txtclass))
{
root.DeleteSubKey(txtclass + "\\shellex\\ContextMenuHandlers\\" + KEYNAME);
root.DeleteSubKey(txtclass + "\\shellex\\IconHandler");
root.DeleteSubKey(txtclass + "\\shellex\\{00021500-0000-0000-C000-000000000046}");
}
}
注册动态库
.NET 开发的动态库有些特别,需要在 .NET SDK 中注册
regasm MyContextMenu.dll /CodeBase
反注册则是:regasm /unregister MyContextMenu.dll /CodeBase
代码:http://files.cnblogs.com/lemony/MyContextMenu.rar
关于代码:代码里面还包括了图标扩展和提示扩展的代码,如果有兴趣,可自行阅读。
题外话:还有相当多的关于 Shell 扩展的内容无法一一说明,如果有机会,以后会尽量补上。或大家查阅网上的“Windows Shell扩展编程完全指南”(虽然是VC版的,但内容相当丰富)
原文地址:http://www.cnblogs.com/lemony/archive/2009/04/09/1279588.html
“Shell 扩展”有很多种类型,每种类型都在各自不同的事件发生时被调用运行,但也有一些扩展的类型和调用情形是非常相似的。
类型 | 何时被调用 | 应该作些什么 |
Context menu 扩展处理器 | 用户右键单击文件或文件夹对象时, 或在一个文件夹窗口中的背景处单击右键时(要求shell版本为4.71+) | 添加菜单项到上下文菜单中 |
Property sheet 扩展处理器 | 要显示一个文件对象的属性框时 | 添加定制属性页到属性表中 |
Drag and drop 扩展处理器 | 用户用右键拖放文件对象到文件夹窗口或桌面时 | 添加菜单项到上下文菜单中 |
Drop 扩展处理器 | 用户拖动Shell对象并将它放到一个文件对象上时 | 任何想要的操作 |
QueryInfo扩展处理器 (需要shell版本 4.71+) | 用户将鼠标盘旋于文件或其他Shell对象的图标上时 | 返回一个浏览器用于显示在提示框中的字符串 |
(1)实现类似 WinRAR 的右键菜单
注册扩展
[System.Runtime.InteropServices.ComRegisterFunctionAttribute()]
static void RegisterServer(String str1)
{
try
{
//注册 DLL
RegistryKey root;
RegistryKey rk;
root = Registry.LocalMachine;
rk = root.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved", true);
rk.SetValue(GUID, KEYNAME);
rk.Close();
root.Close();
//注册文件
RegTXT();
}
catch{
}
}
[System.Runtime.InteropServices.ComUnregisterFunctionAttribute()]
static void UnregisterServer(String str1)
{
try
{
//注销动态库
RegistryKey root;
RegistryKey rk;
root = Registry.LocalMachine;
rk = root.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved", true);
rk.DeleteValue(GUID);
rk.Close();
root.Close();
//注销文件
UnRegTXT();
}
catch
{
}
}
private static void RegTXT()
{
RegistryKey root;
RegistryKey rk;
root = Registry.ClassesRoot;
rk = root.OpenSubKey(".txt");
string txtclass = (string)rk.GetValue("");
if (string.IsNullOrEmpty(txtclass))
{
txtclass = "TXT";
rk.SetValue("", txtclass);
}
rk.Close();
rk = root.CreateSubKey(txtclass + "\\shellex\\ContextMenuHandlers\\" + KEYNAME);
rk.SetValue("", GUID);
rk.Close();
rk = root.CreateSubKey(txtclass + "\\shellex\\IconHandler");
rk.SetValue("", GUID);
rk.Close();
rk = root.CreateSubKey(txtclass + "\\shellex\\{00021500-0000-0000-C000-000000000046}");
rk.SetValue("", GUID);
rk.Close();
}
private static void UnRegTXT()
{
RegistryKey root;
RegistryKey rk;
root = Registry.ClassesRoot;
rk = root.OpenSubKey(".txt");
rk.Close();
string txtclass = (string)rk.GetValue("");
if (!string.IsNullOrEmpty(txtclass))
{
root.DeleteSubKey(txtclass + "\\shellex\\ContextMenuHandlers\\" + KEYNAME);
root.DeleteSubKey(txtclass + "\\shellex\\IconHandler");
root.DeleteSubKey(txtclass + "\\shellex\\{00021500-0000-0000-C000-000000000046}");
}
}
注册动态库
.NET 开发的动态库有些特别,需要在 .NET SDK 中注册
regasm MyContextMenu.dll /CodeBase
反注册则是:regasm /unregister MyContextMenu.dll /CodeBase
代码:http://files.cnblogs.com/lemony/MyContextMenu.rar
关于代码:代码里面还包括了图标扩展和提示扩展的代码,如果有兴趣,可自行阅读。
题外话:还有相当多的关于 Shell 扩展的内容无法一一说明,如果有机会,以后会尽量补上。或大家查阅网上的“Windows Shell扩展编程完全指南”(虽然是VC版的,但内容相当丰富)
原文地址:http://www.cnblogs.com/lemony/archive/2009/04/09/1279588.html
相关文章推荐
- (C#)Windows Shell 外壳编程系列7 - ContextMenu 注册文件右键菜单
- (C#)Windows Shell 外壳编程系列7 - ContextMenu 注册文件右键菜单
- (C#)Windows Shell 外壳编程系列7 - ContextMenu 注册文件右键菜单
- (C#)Windows Shell 外壳编程系列 - ContextMenu 注册文件右键菜单
- (C#)Windows Shell 外壳编程系列7 - ContextMenu 注册文件右键菜单
- (C#)Windows Shell 外壳编程系列7 - ContextMenu 注册文件右键菜单
- (C#)Windows Shell 外壳编程系列7 - ContextMenu 注册文件右键菜单
- (C#)Windows Shell 外壳编程系列3 - 上下文菜单(iContextMenu)(一)右键菜单
- (C#)Windows Shell 外壳编程系列3 - 上下文菜单(iContextMenu)(一)右键菜单
- (C#)Windows Shell 外壳编程系列3 - 上下文菜单(iContextMenu)(一)右键菜单
- 详解ASP.NET异步和Windows Shell 外壳编程系列7-ContextMenu 注册文件右键菜单
- (C#)Windows Shell 外壳编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令
- (C#)Windows Shell 外壳编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令
- (C#)Windows Shell 编程系列3 - 上下文菜单(iContextMenu)(一)右键菜单
- (C#)Windows Shell 外壳编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令
- (C#)Windows Shell 编程系列3 - 上下文菜单(iContextMenu)(一)右键菜单
- (C#)Windows Shell 外壳编程系列5 - 获取图标
- (C#)Windows Shell 外壳编程系列8 - 同后缀名不同图标?
- (C#)Windows Shell 外壳编程系列6 - 执行
- (C#)Windows Shell 外壳编程系列8 - 同后缀名不同图标?