当前位置:编程学习 > VB >>

VB编程问题。

设置一个控件 Text1
如何将文件拖曳到控件中显示其路径。。

希望高手能提供代码。。
答案:
调用API的功能实现, 
因相应的API较复杂,不太好理解,我用子程序对它做了封装。
实际使用过程中你将下面这段代码Copy到程序模块中,然后调用SubOpenFile子程序就行了。

'注意在引用该FSO功能时先引用 Microsoft Scripting Runtime
Public ObjFso As New FileSystemObject

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

Public Sub SubOpenFile(ByVal FileSaveName As String, ByVal StrFrmName As Form)

'打开文件
'输入:sFileName 待打开文件的全路径加上文件名,如 "F:\VB\RunExe\Module1.bas"
'输入:调用该子程序的窗体名称

If Not ObjFso.FileExists(FileSaveName) Then
MsgBox ("没有找到要打开的文件")
Exit Sub
End If

sCorrectPath = ObjFso.GetParentFolderName(FileSaveName)
sFileName = ObjFso.GetFileName(FileSaveName)

'输入:调用该子程序的窗体名称
lHwnd = StrFrmName.hwnd
'lShellFile = shellExecute(lHwnd, "open", FileSaveName, vbNullString, sCorrectPath, SW_SHOWNORMAL)
lShellFile = shellExecute(lHwnd, "open", FileSaveName, vbNullString, sCorrectPath, 5)

'错误处理
If lShellFile > 32 Then
Exit Sub
Else
Select Case lShellFile
Case 2
If Right(sFileName, 3) <> "htm" Then
MsgBox "File Not Found.", vbCritical + vbOKOnly, "X-File:"
End If
Exit Sub
Case 3
MsgBox "Path not Found.", vbCritical + vbOKOnly, "X-File:"
Exit Sub
Case 5
MsgBox "Access denied.", vbCritical + vbOKOnly, "X-File:"
Exit Sub
Case 8
MsgBox "Out of Memory.", vbCritical + vbOKOnly, "X-File:"
Exit Sub

Case 32
MsgBox "Shell32.dll not Found.", vbCritical + vbOKOnly, "X-File:"
Exit Sub
End Select
End If
参考我的blog里的:VB程序实现文件拖放功能
http://blog.163.com/ysli_195/blog/static/4205419420078251157783/

上一个:请教VB编程
下一个:VB编程问题

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,