vb 如何实现文件拖动到窗体时将该文件打开

2025-01-03 16:07:24
推荐回答(2个)
回答1:

'先把form的OLEDropMode属性设置为1,然后复制下面代码运行,拖拽一个xls文件到form(电脑的有office组件)
Option Explicit
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 Form_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
If LCase(Right(Data.Files(1), 4)) = ".xls" Then
ShellExecute Me.hWnd, "Open", Data.Files(1), "", App.Path, 1
Else
MsgBox "非xls文件:" & Data.Files(1)
End If
End Sub

回答2:

Private Sub Form_Load()
Form1.OLEDropMode = 1
End Sub

Private Sub Form_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
Open Data.Files(1) For Binary As #1
'......
End Sub