zoukankan      html  css  js  c++  java
  • 为程序添加系统上下文菜单

    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


  • 相关阅读:
    HTML常用标签1
    mysql 多对多拆分成 一对多(学生,选修课,成绩)
    JDBC工具包使用
    类反射,用于JDBC
    JDBC基础语句使用
    关于线程等待、线程唤醒方法的引入
    synchronized站点抢票同步例题
    python-装饰器
    jwt, token, session和cookies
    miniapp之登录、授权和支付
  • 原文地址:https://www.cnblogs.com/zhangqs008/p/2802211.html
Copyright © 2011-2022 走看看