Purpose Provide an area for the user to drag and drop files to.
Solution Implement the drag&drop functionality in a windows forms control and
embed the control into the sap gui screen by using the callVB command.
Example We will create an area with an image onto which the user can drop files. The filenames will then be available in a GuiXT variable for further processing. In this case, we just display all names in a new sap table control.
First of all we have to create a windows forms application. For many elements it is possible to activate the drag&drop functionality in three steps:
- Set the 'AllowDrop' property to true
- Handle the 'DragEnter' event to change the cursor to a drag&drop symbol
- Handle the 'DragDrop' event when the users drags some files onto the element
We can run and test the application to see if everything works. Then we have to switch the setting to compile it as a .dll library so it can be called by GuiXT.
That means we create a container with GuiXT and embed this application into it on the sap gui screen. In our case we will embed just a very small application, displaying a single image onto which the user can drag&drop files.
VB.NET
Imports guinet
Public Class Form1
Dim g As New guixt
Private Sub Form1_Load(sender As Object, e As EventArgs) _
Handles MyBase.Load
PictureBox1.AllowDrop = True
End Sub
Private Sub PictureBox1_DragEnter_1(sender As Object,
e As DragEventArgs) _
Handles PictureBox1.DragEnter
' SET THE PROPER ACTION FOR FILE DROP....
' BY USING THE FILEDROP METHOD TO COPY
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Link
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub PictureBox1_DragDrop_1(sender As Object,
e As DragEventArgs) _
Handles PictureBox1.DragDrop
Try
If e.Data.GetDataPresent("FileDrop", True) = True Then
Dim Werte As String() =
CType(e.Data.GetData("FileDrop"), Object)
'
Dim fileNames As String = ""
Dim i As Integer = 0
For Each f In Werte
g.SetVariable("dragged_files." _
& (i + 1).ToString, Werte(i))
i += 1
Next
' Get name of InputScript
Dim inputScript As String = ""
inputScript = g.GetVariable("dragdrop_inputscript")
' Start InputScript ...
g.Input("OK:/0,process=" & inputScript)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public Sub embed(ByVal w As Object, imagepath As String)
Try
If Not imagepath = "" Then
PictureBox1.Image = Image.FromFile(imagepath)
End If
guixt.EmbedForm(Me, w)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
Here is the GuiXT Script to embed the control and display the files in a new table:
GuiXT
// Create a container to embed our windows form control
Control (-0.4,99.8) (5.2,136.1) progID="Shell.Explorer" _
="r1" initFlag="r1init" -closeOnHide
// Define the script that will be run after a user dropped files
Set V[dragdrop_inputscript] "va03_drop_files.txt"
// Embed the control into our container
if V[r1init]
CallVB draganddrop.form1.embed w:="&V[r1]" _
imagepath:="C:\guixtscripts\draghere_2.png"
endif
if not V[rows]
Set V[rows] "10"
endif
Table (0,162) (10,223) title="Dropped Files" _
name="droppedfiles" rows="&V[rows]"
Column "Filename" size=60 name="col1"
Here is the InputScript that is processed after the user has dragged files onto the control:
GuiXT
Set V[i] 1
clear V[droppedfiles.cell.*]
label nextRow
if V[dragged_files.&V[i]]
Set V[droppedfiles.cell.col1.&V[i]] "&V[dragged_files.&V[i]]"
Set V[i] "&V[i]" + 1
goto nextRow
endif
Set V[rows] "&V[i]"
clear V[dragged_files.*]
return
Result:
You can download all files for this example here: DragAndDrop.zip
Here is video with further details about the project: