Page 1 of 1

Get path of folder dragged to *updated*

Posted: Mon May 04, 2009 1:40 am
by Joakim Christiansen
This code (tested on XP and Vista) will show the path you drag files to using the drag and drop functions in PureBasic. This is extremely handy if you need to put files there but don't have them ready at exactly the same moment you call DragFiles(); for example if your program needs to download them first.

Let me know if you have any problems! :)

The "text.s=Space(1024*charSize)" stuff is so it will work fine if compiled with unicode flag too. Happy now ts-soft? :P

Code: Select all

EnableExplicit

Procedure child_findPath(hWnd,lParam) ;this will be called on each child window by EnumChildWindows_
  Protected charSize = SizeOf(Character) ;unicode support
  Protected class.s=Space(128*charSize), text.s=Space(1024*charSize);{1024}
  GetClassName_(hWnd,@class,128*charSize)
  SendMessage_(hWnd,#WM_GETTEXT,1024*charSize,@text);Debug Class;Debug Text;Debug "--"  
  If Class = "ComboBoxEx32"
    If Mid(Text,2,2) = ":\" ;here we will need to check if the string is actually a path 
      PokeS(lParam,text,1024*charSize)
      ProcedureReturn #False ;tell enumerator to stop
    EndIf
  EndIf
  ProcedureReturn #True
EndProcedure
Procedure.s DragFilesGetPath()
  Protected charSize = SizeOf(Character)
  Protected hWnd, pos.POINT, OSv = OSVersion(), pathDraggedTo$=Space(1024*charSize)
  If OSv < #PB_OS_Windows_Vista ;XP shows error if no file
    CloseFile(OpenFile(#PB_Any,GetTemporaryDirectory()+"dummyfile")) ;create dummy file
    DragFiles(GetTemporaryDirectory()+"dummyfile"+#LF$)
  Else
    DragFiles(#LF$)
  EndIf
  GetCursorPos_(@pos)
  hWnd = WindowFromPoint_(PeekQ(@pos))
  If hWnd < 65800;Is desktop; XP=65670, Vista=65764
    pathDraggedTo$ = GetEnvironmentVariable("userprofile")+"\desktop";#CSIDL_DESKTOPDIRECTORY
    If OSv < #PB_OS_Windows_Vista
      DeleteFile(pathDraggedTo$+"\dummyfile")
      SendMessageTimeout_(#HWND_BROADCAST,#WM_SETTINGCHANGE,#SPI_SETNONCLIENTMETRICS,#Null,#SMTO_ABORTIFHUNG,1000,0);@result.l) ;update desktop icons
    EndIf
  Else
    hWnd = GetAncestor_(hWnd,#GA_ROOT)
    EnumChildWindows_(hWnd,@child_findPath(),@pathDraggedTo$)
    If OSv < #PB_OS_Windows_Vista: DeleteFile(pathDraggedTo$+"\dummyfile"): EndIf
  EndIf
  ProcedureReturn pathDraggedTo$
EndProcedure

Define windowEvent, path$

OpenWindow(1,0,0,400,100,"Drag & Drop",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
ListIconGadget(1,10,10,380,80,"files",300)
AddGadgetItem(1,-1,"drag me to a open folder window")

Repeat
  windowEvent = WaitWindowEvent()  
  If windowEvent = #PB_Event_Gadget And EventGadget() = 1 And EventType() = #PB_EventType_DragStart
    path$ = DragFilesGetPath()
    If Not path$
      MessageRequester("Error","Can't copy files there...")
    Else
      Debug path$
    EndIf
  EndIf
Until windowEvent = #PB_Event_CloseWindow

Posted: Mon May 04, 2009 4:36 am
by idle
yes it's working on XP.
Never really thought about that problem before.

looks like it's a nice solution!