UDF:
#include-once ; #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 ; #INDEX# ======================================================================================================================= ; Title .........: _ContextMenu ; AutoIt Version : v3.2.12.1 or higher ; Language ......: English ; Description ...: Create an entry in the desktop contextmenu, with the program icon as well. ; Note ..........: Thanks to GEOSoft for the initial idea & LaCastiglione for the link on how to add an icon to the desktop contextmenu. ; Link: http://www.autoitscript.com/forum/topic/129677-need-an-answer-to-a-simple-problem/ ; Author(s) .....: guinness ; Remarks .......: Special thanks to KaFu for EnumRegKeys2Array() which I used as inspiration for enumerating the Registry Keys. ; Warning .......: This has been tested on Windows 7 only. ; =============================================================================================================================== ; #INCLUDES# ========================================================================================================= ; None ; #GLOBAL VARIABLES# ================================================================================================= ; None ; #CURRENT# ===================================================================================================================== ; _ContextMenu_Install: Creates an entry in the 'All Users/Current Users' registry for displaying a program entry in the desktop shell contextmenu. ; _ContextMenu_Uninstall: Deletes an entry in the 'All Users/Current Users' registry for displaying a program entry in the desktop shell contextmenu. ; =============================================================================================================================== ; #INTERNAL_USE_ONLY#============================================================================================================ ; __ContextMenu_RegistryGet ......; Retrieve an array of registry entries for a specific key. ; =============================================================================================================================== ; #FUNCTION# ========================================================================================================= ; Name...........: _ContextMenu_Install() ; Description ...: Creates an entry in the 'All Users/Current Users' registry for displaying a program entry in the desktop shell contextmenu. ; Syntax.........: _ContextMenu_Install($sText, [$sName = @ScriptName, [$sFilePath = @ScriptFullPath, [$sIconPath = @ScriptFullPath, [$iIcon = 0, [$iAllUsers = 0, [$iExtended = 0]]]]]]) ; Parameters ....: $sText - Text to be shown in the contextmenu. ; $sName - [Optional] Name of the program. [Default = Script name] ; $sFilePath - [Optional] Location of the program executable. [Default = Full script location] ; $sIconPath - [Optional] Location of the icon e.g. program executable or dll file. [Default = Full script location] ; $$iIcon - [Optional] Index of icon to be used. [Default = 0 - Main icon] ; $iAllUsers - [Optional] Add to Current Users (0) or All Users (1) [Default = 0 - Current user] ; $iExtended - [Optional] Show in the Extended contextmenu using Shift + Right click. [Default = 0 - Show in main contextmeu.] ; Requirement(s).: v3.2.12.1 or higher ; Return values .: Success - RegWrite() Return code. ; Failure - none ; Author ........: guinness ; Example........; Yes ;===================================================================================================================== Func _ContextMenu_Install($sText, $sName = @ScriptName, $sFilePath = @ScriptFullPath, $sIconPath = @ScriptFullPath, $iIcon = 0, $iAllUsers = 0, $iExtended = 0) Local $i64Bit = "", $sRegistryKey = "" If @OSArch = "X64" Then $i64Bit = "64" EndIf If $iAllUsers Then $sRegistryKey = "HKEY_LOCAL_MACHINE" & $i64Bit & "\SOFTWARE\Classes\Directory\Background\shell\" Else $sRegistryKey = "HKEY_CURRENT_USER" & $i64Bit & "\SOFTWARE\Classes\Directory\Background\shell\" EndIf $sName = StringLower(StringReplace($sName, ".exe", "")) If $sName = "" Or $sFilePath = "" Then Return SetError(1, 0, 0) EndIf _ContextMenu_Uninstall($sName, $iAllUsers) RegWrite($sRegistryKey & $sName, "", "REG_SZ", $sText) RegWrite($sRegistryKey & $sName, "Icon", "REG_EXPAND_SZ", $sIconPath & "," & $iIcon) RegWrite($sRegistryKey & $sName & "\command", "", "REG_SZ", $sFilePath) If $iExtended Then RegWrite($sRegistryKey & $sName, "Extended", "REG_SZ", "") EndIf Return SetError(@error, 0, @error) EndFunc ;==>_ContextMenu_Install ; #FUNCTION# ========================================================================================================= ; Name...........: _ContextMenu_Uninstall() ; Description ...: Deletes an entry in the 'All Users/Current Users' registry for displaying a program entry in the desktop shell contextmenu. ; Syntax.........: _ContextMenu_Uninstall([$sName = @ScriptName, [$iAllUsers = 0]]) ; Parameters ....: $sName - [Optional] Name of the Program [Default = Script name.] ; $iAllUsers - [Optional] Was it added to Current Users (0) or All Users (1). [Default = 0 - Current user] ; Requirement(s).: v3.2.12.1 or higher ; Return values .: Success - Returns 2D Array of registry entries. ; Failure - Returns 0 and sets @error to 1. ; Author ........: guinness ; Example........; Yes ;===================================================================================================================== Func _ContextMenu_Uninstall($sName = @ScriptName, $iAllUsers = 0) Local $aArray, $i64Bit = "", $sDelete, $sRegistryKey = "" If @OSArch = "X64" Then $i64Bit = "64" EndIf If $iAllUsers Then $sRegistryKey = "HKEY_LOCAL_MACHINE" & $i64Bit & "\SOFTWARE\Classes\Directory\Background\shell\" Else $sRegistryKey = "HKEY_CURRENT_USER" & $i64Bit & "\SOFTWARE\Classes\Directory\Background\shell\" EndIf $sName = StringLower(StringReplace($sName, ".exe", "")) If $sName = "" Then Return SetError(1, 0, 0) EndIf $aArray = __ContextMenu_RegistryGet($sRegistryKey) If $aArray[0][0] > 0 Then For $A = 1 To $aArray[0][0] If $aArray[$A][0] = $sName And $sDelete <> $aArray[$A][1] Then $sDelete = $aArray[$A][1] RegDelete($sDelete) EndIf Next EndIf Return $aArray EndFunc ;==>_ContextMenu_Uninstall ; #INTERNAL_USE_ONLY#============================================================================================================ Func __ContextMenu_RegistryGet($sRegistryKey) Local $aArray[1][5] = [[0, 5]], $iCount_1 = 0, $iCount_2 = 0, $iDimension, $iError = 0, $sRegistryKey_All, $sRegistryKey_Main, $sRegistryKey_Name, $sRegistryKey_Value While 1 If $iError Then ExitLoop EndIf $sRegistryKey_Main = RegEnumKey($sRegistryKey, $iCount_1 + 1) If @error Then $sRegistryKey_All = $sRegistryKey $iError = 1 Else $sRegistryKey_All = $sRegistryKey & $sRegistryKey_Main EndIf $iCount_2 = 0 While 1 $sRegistryKey_Name = RegEnumVal($sRegistryKey_All, $iCount_2 + 1) If @error Then ExitLoop EndIf If ($aArray[0][0] + 1) >= $iDimension Then $iDimension = ($aArray[0][0] + 1) * 2 ReDim $aArray[$iDimension][$aArray[0][1]] EndIf $sRegistryKey_Value = RegRead($sRegistryKey_All, $sRegistryKey_Name) $aArray[$aArray[0][0] + 1][0] = $sRegistryKey_Main $aArray[$aArray[0][0] + 1][1] = $sRegistryKey_All $aArray[$aArray[0][0] + 1][2] = $sRegistryKey & $sRegistryKey_Main & "\" & $sRegistryKey_Name $aArray[$aArray[0][0] + 1][3] = $sRegistryKey_Name $aArray[$aArray[0][0] + 1][4] = $sRegistryKey_Value $aArray[0][0] += 1 $iCount_2 += 1 WEnd $iCount_1 += 1 WEnd ReDim $aArray[$aArray[0][0] + 1][$aArray[0][1]] Return $aArray EndFunc ;==>__ContextMenu_RegistryGet
Example 1:
All of the above has been included in a ZIP file.
ContextMenu.zip 2.31K
415 downloadsWarning: This has been tested on Windows 7 only.
Edited by guinness, 15 October 2011 - 11:12 PM.









