簡體   English   中英

單擊上下文菜單項時,將上下文菜單項插入資源管理器並傳遞完整文件名

[英]Insert a Context menu Item to Explorer and pass the Full File Name when Context Menu item is Clicked

我想為所有“ jpg”文件的Windows資源管理器上下文菜單添加一個上下文菜單項。當用戶單擊此上下文菜單項的名稱將為“ process JPEG”,將調用一個主要的可執行文件。問題是我已經創建了主要的c#可執行文件,它不僅可以通過上下文菜單運行,還可以獨立運行。我想將用戶選擇的文件名或文件名傳遞給上下文文件,然后單擊上下文菜單命令,然后傳遞給主要可執行文件,因此主可執行文件將能夠使用某種方法來獲取文件並對其進行處理。我已完成以下操作以集成上下文菜單-請幫幫我

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));

            // create full path to registry location
            string regPath = string.Format(@"{0}\shell\{1}", fileType, shellKeyName);

            // add context menu to the registry
            using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(regPath))
            {
                key.SetValue(null, menuText);
            }

            // add command that is invoked to the registry
            using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(
                string.Format(@"{0}\command", regPath)))
            {               
                key.SetValue(null, menuCommand);
            }
        }

要在較舊版本的Windows上為文件類型注冊動詞,您需要:

  1. 獲取ProgID 在您的情況下,請讀取HKCR\\.jpg下的默認值(這通常會為您提供jpgfile類的jpgfile
  2. HKCR\\%ProgId%\\shell\\%YourVerb%\\command下編寫動詞HKCR\\%ProgId%\\shell\\%YourVerb%\\command

從XP開始,您可以在SystemFileAssociations下注冊補充動詞,而無需控制ProgID:

  1. HKCR\\SystemFileAssociations\\.jpg\\shell\\%YourVerb%\\command下編寫動詞HKCR\\SystemFileAssociations\\.jpg\\shell\\%YourVerb%\\command

謂詞命令通常為"c:\\full\\path\\to\\yourapp.exe" "%1" ,%1被Windows替換為文件名。 您的應用程序必須解析命令行。

為了正確地支持同時打開多個文件,最好使用DDE或IDropTarget (或Win7 +上的IExecuteCommand

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM