Private Declare Function FindExecutable Lib “shell32.dll” Alias “FindExecutableA” (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
Public Sub Associate(ByVal FileExtend As String, ByVal FileType As String, ByVal FileDescribe As String, ByVal FileIcon As String, ByVal ProgramName As String) ‘注册文件类型,并设置在浏览器双击打开文件
‘第四位参数说明
‘”C:\WINDOWS\SYSTEM32\shell32.dll,-151″,软件指定图标
‘ProgramName,应用软件图标
‘”%1″,系统指定图标
On Error Resume Next
Dim hkey As Long, ReturnVal As Long
ReturnVal = RegCreateKeyEx(HKEY_CLASSES_ROOT, FileExtend, 0, vbNullString, 0, KEY_ALL_ACCESS, 0, hkey, 0)
If ReturnVal = 0 Then
RegSetValueEx hkey, vbNullString, 0, 1, FileType, LenB(StrConv(FileType, vbFromUnicode)) ‘Len(FileType) 汉字时不准
RegCloseKey hkey
End If
ReturnVal = RegCreateKeyEx(HKEY_CLASSES_ROOT, FileType, 0, vbNullString, 0, KEY_ALL_ACCESS, 0, hkey, 0)
If ReturnVal = 0 Then
RegSetValueEx hkey, vbNullString, 0, 1, FileDescribe, LenB(StrConv(FileDescribe, vbFromUnicode)) ‘Len(FileDescribe) 汉字时不准
RegCloseKey hkey
End If
ReturnVal = RegCreateKeyEx(HKEY_CLASSES_ROOT, FileType & “\DefaultIcon”, 0, vbNullString, 0, KEY_ALL_ACCESS, 0, hkey, 0)
If ReturnVal = 0 Then
RegSetValueEx hkey, vbNullString, 0, 1, FileIcon, LenB(StrConv(FileIcon, vbFromUnicode)) ‘Len(FileIcon) 汉字时不准
RegCloseKey hkey
End If
ReturnVal = RegCreateKeyEx(HKEY_CLASSES_ROOT, FileType & “\Shell\Open\Command”, 0, vbNullString, 0, KEY_ALL_ACCESS, 0, hkey, 0)
If ReturnVal = 0 Then
ProgramName = ProgramName & Space$(1) & “/open %1”
RegSetValueEx hkey, vbNullString, 0, 1, ProgramName, LenB(StrConv(ProgramName, vbFromUnicode)) ‘Len(ProgramName) 汉字时不准
RegCloseKey hkey
End If
On Error GoTo 0
End Sub
Public Sub DisAssociate(ByVal FileExtend As String, ByVal FileType As String) ‘解除在浏览器双击打开文件
DelKey HKEY_CLASSES_ROOT, FileExtend
DelKey HKEY_CLASSES_ROOT, FileType
SHDeleteKey HKEY_CLASSES_ROOT, FileType ‘删除含有子项的项
End Sub
Public Sub SetRightMenu(ByVal sMenuShow As String, ByVal sProgram As String) ‘设置在系统右键菜单打开文件
Call SaveString(HKEY_CLASSES_ROOT, “*\shell\” & sMenuShow & “\command”, “”, sProgram & Space$(1) & “%1″) ‘”” 代表子项 command 的字符串值(默认)
End Sub
Public Sub DisRightMenu(ByVal sMenuShow As String) ‘解除在系统右键菜单打开文件
SHDeleteKey HKEY_CLASSES_ROOT, sMenuShow ‘删除含有子项的项
End Sub
Public Function GetAssociatedProgram(ByVal sExtension As String) As String ‘根据扩展名查找默认打开程序
GetAssociatedProgram = “”
‘If LCase(sExtension) = “dwg” Then
‘GetAssociatedProgram = Trim(GetString(HKEY_CLASSES_ROOT, “AutoCAD\Shell\Open\command”, vbNullString))
‘If GetAssociatedProgram = “” Then GetAssociatedProgram = Trim(GetString(HKEY_LOCAL_MACHINE, “SOFTWARE\Classes\autocad\shell\open\command”, vbNullString)) ‘Belt and braces
‘Exit Function
‘End If
‘ Reading the host program of DWG from the registry.
Dim temp_title As String
Dim temp_path As String
Dim fnum As Integer
Dim result As String
Dim pos As Integer
‘ Get a temporary file name with this extension.
GetTempFile IIf(LCase(sExtension) = “dwg”, “dxf”, sExtension), temp_path, temp_title
‘ Findexecutable cannot find the host program of DWG. The host programs of DXF and DWT can be found, and they are the same host program as DWG.
‘ Make the file.
fnum = FreeFile
Open temp_path & temp_title For Output As fnum
Close fnum
‘ Get the associated executable.
result = Space$(1024)
FindExecutable temp_title, temp_path, result
pos = InStr(result, Chr$(0))
GetAssociatedProgram = Left$(result, pos – 1)
‘ Delete the temporary file.
Kill temp_path & temp_title
End Function
Private Sub GetTempFile(ByVal sExtension As String, ByRef temp_path As String, ByRef temp_title As String)’ Return a temporary file name.
Dim I As Integer
If Left$(sExtension, 1) <> “.” Then sExtension = “.” & sExtension
temp_path = Environ(“TEMP”)
If Right$(temp_path, 1) <> “\” Then temp_path = temp_path & “\”
I = 0
Do
temp_title = “tmp” & Format$(I) & sExtension
If Len(Dir$(temp_path & temp_title)) = 0 Then Exit Do
I = I + 1
Loop
End Sub
‘效果
Public Sub Main()
On Error Resume Next
Dim OptFile As String ‘所选文件的名称
If Len(Command) = 0 Then ‘点击软件本身启动 Command = “”
‘GO ON
Else
If LCase(Left(Command, 6)) = “/open” & Space$(1) Then ‘浏览器双击启动 Command = “/open D:\Test.iso”
OptFile = Right(Command, Len(Command) – 6)
Else ‘系统右键菜单启动 Command = “D:\Test.iso”
OptFile = Command
End If
End If
‘GO ON
On Error GoTo 0
End Sub
‘调用
Sub SetFile()
Dim sProgram As String
sProgram = “C:\Windows\notepad.exe”
Dim sShow As String
sShow = “Open With notepad”
Call Associate(“.iso”, “ISO File”, “”, “%1”, sProgram) ‘注册后缀为 iso 的文件,并确定其默认打开程序为 sProgram
Call SetRightMenu(sShow, sProgram) ‘在系统右键菜单增加一项显示字符为show,并指定由sProgram打开所选文件
End Sub