PowerBuilder – Dragging Files onto the Application

Posted on Tuesday, November 30th, 2010 at 8:22 pm in

Many applications I’ve worked on have had internal drag/drop capabilities. By ‘internal’ I mean you can drag items from one place to another from within the application itself. Now what if you want to drag files from an explorer window into your application and perform some processing? This functionality is not as common.

The solution involves three Windows API calls, one of which has a Unicode and an ANSI version. If you are using the PFC you can add these to your environment object so the appropriate version is called depending upon the operating system of the user.

The API methods

subroutine DragAcceptFiles(long l_hWnd,boolean fAccept) library "shell32.dll"
subroutine DragFinish(long hDrop) library "shell32.dll"
function int DragQueryFileW(long hDrop,int iFile,ref string szFileName,int cb) library "shell32.dll"
function int DragQueryFileA(long hDrop,int iFile,ref string szFileName,int cb) library "shell32.dll"

To allow items to be dragged onto your application you must first call the DragAcceptFiles method. This should be put in the open/postopen type event of the app.

DragAcceptFiles(handle(this), true)

Now create an user event mapped to the pbm_dropfiles Event ID. Put the following in it:

integer	li_i, li_numFiles, li_rc
string 	ls_File

ls_File = Space(1024)

// first call gets number of files dragged
// unicode API call
li_numFiles = DragQueryFileW(handle, 4294967295, ls_File, 1023)
// ansi API call
//li_numFiles = DragQueryFileA(handle, 4294967295, ls_File, 1023) 

FOR li_i = 0 To li_numFiles - 1
	ls_File = Space(1024)
	// subsequent calls get file names from dragged object
	li_rc = DragQueryFileW( handle, li_i, ls_File, 1023 )
	// add file to listbox
	lb_1.AddItem(ls_File)
NEXT 

DragFinish(handle)

This example is populating a listbox with the file names dragged onto the application.

The MSDN reference for DragQueryFile can be found here.

Top