VB6调用API打开目标文件所在文件夹且选中目标文件
Option Explicit
' 模塊名稱: mOpenFolderAndSetFileFocus
'???? 作者: 唐細剛
'???? 時間: 2010-08-22
'???? 功能: VB6調用API打開目標文件所在文件夾且選中目標文件
' 注:
' 由于調用 Explorer.exe /select 方式會使系統產生多余的 Explorer.exe 進程
' 所以還是API來實現較好,速度也有優勢,不知道是否低碳? O(∩_∩)O~
Private Declare Function SHCreateFromPath Lib "Shell32" Alias "ILCreateFromPathA" (ByVal lpFileName As String) As Long
Private Declare Sub SHFree Lib "Shell32" Alias "ILFree" (ByVal lngPidl As Long)
Private Declare Function SHOpenFolderAndSelectItems Lib "Shell32" ( _
??????? ByVal pidlFolder As Long, _
??????? ByVal cidl As Long, _
??????? ByVal apidl As Long, _
??????? ByVal dwFlags As Long) As Long
'增加判斷文件是否存在
Private Const INVALID_HANDLE_VALUE = -1
Private Const MAX_PATH = 260
Private Type FILETIME
??? dwLowDateTime As Long
??? dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
??? dwFileAttributes As Long
??? ftCreationTime As FILETIME
??? ftLastAccessTime As FILETIME
??? ftLastWriteTime As FILETIME
??? nFileSizeHigh As Long
??? nFileSizeLow As Long
??? dwReserved0 As Long
??? dwReserved1 As Long
??? cFileName As String * MAX_PATH
??? cAlternate As String * 14
End Type
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" ( _
??? ByVal lpFileName As String, _
??? lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Private Function FileExists(ByVal lpFileName As String) As Boolean
??? Dim tpWFD As WIN32_FIND_DATA
??? Dim lngFile As Long
??? lngFile = FindFirstFile(lpFileName, tpWFD)
??? FileExists = lngFile <> INVALID_HANDLE_VALUE
??? If lngFile Then Call FindClose(lngFile)
End Function
'調用成功返回 True,否則返回 False
Public Function OpenFolderAndSetFileFocus(ByVal lpFileName As String) As Boolean
??? On Error Resume Next
??? Dim lngPidl As Long
??? Dim lngRet? As Long
??? Dim strFile As String
??? strFile = Trim(lpFileName)
??? If FileExists(strFile) = False Then Exit Function
??? lngPidl = SHCreateFromPath(strFile & vbNullChar)
??? If lngPidl <> 0 Then
?????? lngRet = SHOpenFolderAndSelectItems(lngPidl, 0, 0, 0)
?????? If lngRet = 0 Then
????????? OpenFolderAndSetFileFocus = True
?????? End If
?????? Call SHFree(lngPidl)
??? End If
End Function
?
?
轉載于:https://www.cnblogs.com/it201108/archive/2010/08/22/2148049.html
總結
以上是生活随笔為你收集整理的VB6调用API打开目标文件所在文件夹且选中目标文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python 植物大战僵尸代码实现
- 下一篇: 9.4SAS软件入门