为程序添加系统上下文菜单

简介:
using System;
using System.Diagnostics;
using Microsoft.Win32;

namespace SimpleContextMenu
{
    /// <summary>
    /// 在注册表中注册和注销文件上下文菜单.
    /// </summary>
    static class FileShellExtension
    {
        /// <summary>
        /// 注册上下文菜单
        /// </summary>
        /// <param name="fileType">要注册的文件类型</param>
        /// <param name="shellKeyName">在注册表中显示的名称</param>
        /// <param name="menuText">在上下文菜单中显示的文字</param>
        /// <param name="menuCommand">被执行的命令行</param>
        public static void Register(string fileType, string shellKeyName, string menuText, string menuCommand)
        {
            Debug.Assert(!string.IsNullOrEmpty(fileType) &&
                !string.IsNullOrEmpty(shellKeyName) &&
                !string.IsNullOrEmpty(menuText) &&
                !string.IsNullOrEmpty(menuCommand));

            //创建注册表位置的完整路径
            string regPath = string.Format(@"{0}\shell\{1}", fileType, shellKeyName);

            //注册表中添加上下文菜单
            using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(regPath))
            {
                key.SetValue(null, menuText);
            }

            //添加命令到被调用的注册表
            using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(string.Format(@"{0}\command", regPath)))
            {
                key.SetValue(null, menuCommand);
            }
        }

        /// <summary>
        /// 注销上下文菜单.
        /// </summary>
        /// <param name="fileType">注销的文件类型</param>
        /// <param name="shellKeyName">在注册表中注册的名称</param>
        public static void Unregister(string fileType, string shellKeyName)
        {
            Debug.Assert(!string.IsNullOrEmpty(fileType) && !string.IsNullOrEmpty(shellKeyName));

            // 注册表中的位置的完整路径	
            string regPath = string.Format(@"{0}\shell\{1}", fileType, shellKeyName);

            //从注册表中删除上下文菜单
            Registry.ClassesRoot.DeleteSubKeyTree(regPath);
        }
    }

}

调用方法:

using System;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

[assembly: CLSCompliant(true)]
namespace SimpleContextMenu
{
    static class Program
    {       
        const string FileType = "Directory";          // 注册的文件类型
        const string KeyName = "Simple Context Menu"; //在注册表的上下文菜单名称
        const string MenuText = "Copy to Grayscale";  // 上下文菜单文本

        [STAThread]
        static void Main(string[] args)
        {
            // 注册右键菜单
            if (!ProcessCommand(args))
            {
                //接收命令行参数处理
            }
        }
	}
}

原文地址: http://www.codeproject.com/Articles/15171/Simple-shell-context-menu


目录
相关文章
怎样将Beyond Compare添加到系统右键菜单
怎样将Beyond Compare添加到系统右键菜单
1209 0
electron菜单或托盘点击如何打开新的窗口
electron菜单或托盘点击如何打开新的窗口
|
区块链
MFC为应用程序添加托盘(右键托盘,弹出菜单)
源代码:http://download.csdn.net/detail/nuptboyzhb/4137784 1.       导入一个托盘图标的资源(.ico)格式;资源ID为IDI_ICON1 2.
1324 0
AndroidStudio 鼠标进入代码区显示提示信息
AndroidStudio 鼠标进入代码区显示提示信息
268 0
AndroidStudio 鼠标进入代码区显示提示信息
|
安全 Android开发
Android开发之调用系统设置界面
Android开发之调用系统设置界面
211 0
QT应用编程: 鼠标拖动文件到应用程序窗口
QT应用编程: 鼠标拖动文件到应用程序窗口
337 0
QT应用编程: 鼠标拖动文件到应用程序窗口
|
Linux Windows
Qt6 防止程序多重启动,并实现双击图标显示已运行的程序
欢迎来到我的博客,希望这篇文章对你有所帮助,如果觉得不错,请点赞搜藏哈。
649 0
|
测试技术
MFC中为菜单或按钮添加快捷键功能
1、新建一快捷键资源,ACCELERATOR,关联相应的ID号,下图所示中,其中,第一个ID为自定义快捷键ID,按CTRL+R,此时响应该ID以应的消息响应函数, 第二个ID为菜单ID,此时按CTRL+V,调出ID_NETWORK_PING菜单对应的响应函数。
1404 0
|
索引 小程序 容器
小程序 — 选项卡
前言:在小程序中实现选项卡的功能。 GitHub:https://github.com/Ewall1106/miniProgramDemo 先看一下最终的实现效果: 小程序实现选项卡功能 1、页面结构 使用wx:for对list数组进...
1459 0

热门文章

最新文章