Page 1 of 1

Copy files to Clipboard and insert files from Clipboard?

Posted: Mon Apr 13, 2009 1:01 pm
by ts-soft
I have only found this: http://www.purebasic.fr/english/viewtop ... 706#216706 from sparkie, but i require this for
more than one file and for insert files from clipboard. Can anybody help me?

Greetings

Thomas

Posted: Mon Apr 13, 2009 9:47 pm
by Sparkie
Here's a quick try that appears to work on XP.

Code: Select all

Procedure CopyFile2ClipBoard(*files) 
  clipFile = 0 
  If OpenClipboard_(0) 
    EmptyClipboard_() 
    hGlobal = GlobalAlloc_(#GHND, SizeOf(DROPFILES) + MemorySize(*files)) 
    If hGlobal 
      *lpGlobal.DROPFILES = GlobalLock_(hGlobal) 
      ZeroMemory_(*lpGlobal, SizeOf(DROPFILES)) 
      *lpGlobal\pFiles = SizeOf(DROPFILES) 
      *lpGlobal\fWide = 0 ; 1 = Unicode
      *lpGlobal\fNC = 0 
      *lpGlobal\pt\x = 0 
      *lpGlobal\pt\y = 0 
      CopyMemory_((*lpGlobal + SizeOf(DROPFILES)), *files, MemorySize(*files)) 
      GlobalUnlock_(hGlobal) 
      If SetClipboardData_(#CF_HDROP, hGlobal) 
        clipFile = #True 
      EndIf 
    EndIf 
    CloseClipboard_() 
  EndIf 
  ProcedureReturn clipFile 
EndProcedure 

;- each file in the list must be separarted by a single null char
;- and double null chars at the end of the file list
*temp = AllocateMemory(4096)
file2copy.s = OpenFileRequester("Select file to copy to the ClipBoard", "", "All files|*.*", 0, #PB_Requester_MultiSelection)  
If file2copy
  PokeS(*temp, file2copy, Len(file2copy))
  nextFile = Len(file2copy) + 1
  memSize = nextFile
  While file2copy
    file2copy = NextSelectedFileName()
    PokeS(*temp + nextFile, file2copy, Len(file2copy))
    nextFile + Len(file2copy) + 1
    memSize = nextFile 
  Wend
EndIf
PokeB(*temp + memSize + 1, 0)
*allfiles = AllocateMemory(memSize + 1)
CopyMemory(*temp, *allfiles, MemorySize(*allfiles))
FreeMemory(*temp)
CopyFile2ClipBoard(*allfiles)

Posted: Mon Apr 13, 2009 10:01 pm
by ts-soft
thanks Sparkie :D
great code

but i require also the other way, to insert from clipboard
(for my own filemanager :wink: )

greetings

Thomas

Posted: Mon Apr 13, 2009 10:04 pm
by Sparkie
I'm at work right now but I'll see what I can do when I get home later tonight. :)

Posted: Tue Apr 14, 2009 12:54 am
by Sparkie
Here is a base for you to improve on. ;)

Code: Select all

Procedure CopyFileToClipBoard(*files) 
  clipFile = 0 
  If OpenClipboard_(0) 
    EmptyClipboard_() 
    hGlobal = GlobalAlloc_(#GHND, SizeOf(DROPFILES) + MemorySize(*files)) 
    If hGlobal 
      *lpGlobal.DROPFILES = GlobalLock_(hGlobal) 
      ZeroMemory_(*lpGlobal, SizeOf(DROPFILES)) 
      *lpGlobal\pFiles = SizeOf(DROPFILES) 
      *lpGlobal\fWide = 0 ; 1 = Unicode 
      *lpGlobal\fNC = 0 
      *lpGlobal\pt\x = 0 
      *lpGlobal\pt\y = 0 
      CopyMemory_((*lpGlobal + SizeOf(DROPFILES)), *files, MemorySize(*files)) 
      GlobalUnlock_(hGlobal) 
      If SetClipboardData_(#CF_HDROP, hGlobal) 
        clipFile = #True 
      EndIf 
    EndIf 
    CloseClipboard_() 
  EndIf 
  ProcedureReturn clipFile 
EndProcedure 

Procedure PasteFileFromClipBoard()
  nFiles = 0
  If OpenClipboard_(0) 
    ;- See if there are any files in the clipboard
    If IsClipboardFormatAvailable_(#CF_HDROP)
      ;- get a handle to the data
      cbFiles = GetClipboardData_(#CF_HDROP)
      If cbFiles
        ;- Get a file count
        nFiles = DragQueryFile_(cbFiles, -1, 0, 0)
        ;- Loop through to get filenames
        For f = 0 To nFiles - 1
          buffSize = DragQueryFile_(cbFiles, f, 0, 0) + 1
          file$ = Space(buffSize)
          DragQueryFile_(cbFiles, f, @file$, buffSize)
          ;- For testing, I'll just list the files but
          ;- you can add your file copy routine here
          AddGadgetItem(2, -1, file$)
        Next
      EndIf
    EndIf
  EndIf 
    CloseClipboard_() 
  ProcedureReturn nFiles  
EndProcedure

Procedure GetFilesToCopy()
  ;- each file in the list must be separarted by a single null char 
  ;- and double null chars at the end of the file list 
  *temp = AllocateMemory(4096) 
  file2copy.s = OpenFileRequester("Select file to copy to the ClipBoard", "", "All files|*.*", 0, #PB_Requester_MultiSelection)  
  If file2copy 
    PokeS(*temp, file2copy, Len(file2copy)) 
    nextFile = Len(file2copy) + 1 
    memSize = nextFile 
    While file2copy 
      file2copy = NextSelectedFileName() 
      PokeS(*temp + nextFile, file2copy, Len(file2copy)) 
      nextFile + Len(file2copy) + 1 
      memSize = nextFile 
    Wend 
  EndIf 
  PokeB(*temp + memSize + 1, 0) 
  *allfiles = AllocateMemory(memSize + 1) 
  CopyMemory(*temp, *allfiles, MemorySize(*allfiles)) 
  FreeMemory(*temp) 
  CopyFileToClipBoard(*allfiles) 

EndProcedure


If OpenWindow(0, 0, 0, 400, 400, "Clipboard Files", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
  ButtonGadget(0, 5, 5, 100, 25, "Copy Files")
  ButtonGadget(1, 105, 5, 100, 25, "Paste Files")
  EditorGadget(2, 5, 40, 390, 350)
  Repeat
    event = WaitWindowEvent()
    If event = #PB_Event_Gadget And EventGadget() = 0
      GetFilesToCopy()
    EndIf
    If event = #PB_Event_Gadget And EventGadget() = 1
      PasteFileFromClipBoard()
    EndIf
  Until event = #PB_Event_CloseWindow
EndIf

Posted: Tue Apr 14, 2009 1:20 am
by ts-soft
Sparkie wrote:Here is a base for you to improve on. ;)
:D
I thank you, you make me happy

Posted: Tue Apr 14, 2009 2:24 am
by Sparkie
You're welcome. :)