vb的filelistbox中打开文件的问题
现在有drivelistbox,dirlistbox,filelistbox三个控件,三个是同步的,怎么打开某盘里任意目录下的任意文件?不仅是exe,还有txt,mp3,...
现在有drivelistbox,dirlistbox,filelistbox三个控件,三个是同步的,怎么打开某盘里任意目录下的任意文件?
不仅是exe ,还有txt ,mp3,dll 等格式的文件 展开
不仅是exe ,还有txt ,mp3,dll 等格式的文件 展开
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
调试通过 绝对没问题
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
调试通过 绝对没问题
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询