如何在Windows右键菜单加上自己的程序
比如我做一个记事本名字为“记事本B”,在任意一个txt格式文件右键,菜单上会显示“用记事本B打开”;而且可以筛选文件格式,打开时默认用我的程序。
'' 关闭打开的键 Private Declare Function RegCloseKey Lib "advapi32.dll" _ (ByVal hKey As Long) As Long '建立键 Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" _ (ByVal hKey As Long, _ ByVal lpSubKey As String, _ phkResult As Long) As Long '写入启动值 Private Declare Function RegSetValueEx Lib "advapi32" Alias "RegSetValueExA" ( _ ByVal hKey As Long, ByVal lpValueName As String, _ ByVal Reserved As Long, ByVal dwType As Long, _ ByVal lpData As String, ByVal cbData As Long) As Long '删除项目 Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long '打开注册表subkey的hkey Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" ( _ ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long Private Const HKEY_CLASSES_ROOT = &H80000000 Private Const REG_SZ = 1 '删除注册表键函数 Function DeleteKey(RootKey As Long, ParentKeyName As String, SubKeyName As String) Dim hKey As Long RegOpenKey RootKey, ParentKeyName, hKey RegDeleteKey hKey, SubKeyName RegCloseKey hKey End Function Private Sub Command1_Click() '把应用程序加入右键菜单 Dim hKey As Long, ret As Long '建立注册表项 RegCreateKey HKEY_CLASSES_ROOT, "*\shell\我的记事本\command", hKey 'Any To Icon为右键菜单显示的名称 '设置右键菜单项目 ret = RegSetValueEx(hKey, "", 0, REG_SZ, ByVal App.Path & "\" & App.EXEName & ".exe", ByVal LenB(StrConv(App.Path & "\" & App.EXEName & ".exe", vbFromUnicode)) + 1) If ret = 0 Then MsgBox "右键菜单写入成功" Else MsgBox "右键菜单写入失败" End If RegCloseKey hKey '关闭注册表项 End Sub Private Sub Command2_Click() '这里必须分步执行,如同删除文件夹一样,不能删除非空的文件夹,此处重要。 DeleteKey HKEY_CLASSES_ROOT, "*\shell\我的记事本", "command" DeleteKey HKEY_CLASSES_ROOT, "*\shell", "我的记事本" MsgBox "成功删除右键菜单项", , "提示" End Sub