vb的filelistbox中打开文件的问题

2024-12-18 00:12:38
推荐回答(1个)
回答1:

如果是可执行文件可以直接用Shell 那就简单了 不过这里是打开任意文件 需要使用函数ShellExecute 使用这个函数需要先声明 就是在最头部的通用部分写下如下声明
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

然后就能使用这个函数了 给你提供我刚写的一个样例代码 双击打开任意文件
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub

Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub

Private Sub File1_DblClick()
Dim astr As String
Dim myopen

astr = Dir1.Path
If Right(astr, 1) <> "\" Then astr = astr & "\"

myopen = ShellExecute(0, "open", astr & File1.FileName, "", "", 1)
End Sub

调试通过 绝对没问题