我设置 FORM 为 OLE 后,可以得到任何文件的信息,但是如果把“我的电脑”、“我的文档”等图标拖到我程序的窗体就无法识别,提示错误是“数据格式不匹配”。请问这些特殊的图标该怎么获得数据?代码如下:
Private Sub Form_OLEDragOver(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single, State As Integer) If Data.GetFormat(vbCFText) Then
Text1.Text = Data.GetData(vbCFText)
Else
For i1 = 1 To Data.Files.Count
Text1.Text = Data.Files(i1)
Next
End If
End Sub
Private Sub Form_OLEDragOver(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single, State As Integer) If Data.GetFormat(vbCFText) Then
Text1.Text = Data.GetData(vbCFText)
Else
For i1 = 1 To Data.Files.Count
Text1.Text = Data.Files(i1)
Next
End If
End Sub
下面是一个获取特殊文件夹路径的例子:
'Module Code
Option Explicit
Declare Function SHGetSpecialFolderLocation Lib "Shell32.dll" _
(ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long
Declare Function SHGetPathFromIDList Lib "Shell32.dll" Alias "SHGetPathFromIDListA" _
(ByVal pidl As Long, ByVal pszPath As String) As Long
Public Type SHITEMID
cb As Long
abID As Byte
End TypePublic Type ITEMIDLIST
mkid As SHITEMID
End Type
Public Const MAX_PATH As Integer = 260Public Function fGetSpecialFolder(CSIDL As Long) As String
Dim sPath As String
Dim IDL As ITEMIDLIST
'
' Retrieve info about system folders such as the "Recent Documents" folder.
' Info is stored in the IDL structure.
'
fGetSpecialFolder = ""
If SHGetSpecialFolderLocation(Form1.hwnd, CSIDL, IDL) = 0 Then
'
' Get the path from the ID list, and return the folder.
'
sPath = Space$(MAX_PATH)
If SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal sPath) Then
fGetSpecialFolder = Left$(sPath, InStr(sPath, vbNullChar) - 1) & ""
End If
End If
End Function'Form Code
Private Const CSIDL_DESKTOP = &H0 '// The Desktop - virtual folder
Private Const CSIDL_PROGRAMS = 2 '// Program Files
Private Const CSIDL_CONTROLS = 3 '// Control Panel - virtual folder
Private Const CSIDL_PRINTERS = 4 '// Printers - virtual folder
Private Const CSIDL_DOCUMENTS = 5 '// My Documents
Private Const CSIDL_FAVORITES = 6 '// Favourites
Private Const CSIDL_STARTUP = 7 '// Startup Folder
Private Const CSIDL_RECENT = 8 '// Recent Documents
Private Const CSIDL_SENDTO = 9 '// Send To Folder
Private Const CSIDL_BITBUCKET = 10 '// Recycle Bin - virtual folder
Private Const CSIDL_STARTMENU = 11 '// Start Menu
Private Const CSIDL_DESKTOPFOLDER = 16 '// Desktop folder
Private Const CSIDL_DRIVES = 17 '// My Computer - virtual folder
Private Const CSIDL_NETWORK = 18 '// Network Neighbourhood - virtual folder
Private Const CSIDL_NETHOOD = 19 '// NetHood Folder
Private Const CSIDL_FONTS = 20 '// Fonts folder
Private Const CSIDL_SHELLNEW = 21 '// ShellNew folderPrivate Sub form_load()
MsgBox "Desktop Folder " & fGetSpecialFolder(CSIDL_DESKTOPFOLDER)
MsgBox "Recent Folder " & fGetSpecialFolder(CSIDL_RECENT)
'// etc...
End Sub
Text1.Text = Data.GetData(vbCFText)
Else If Data.GetFormat(vbCFFiles) Then '加上判断吧。OLE类型又不是仅仅这么两种
For i1 = 1 To Data.Files.Count
Text1.Text = Data.Files(i1)
Next
end if
End If