Copy files to Clipboard and insert files from Clipboard?

Windows specific forum
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Copy files to Clipboard and insert files from Clipboard?

Post 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
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post 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)
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post 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
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

I'm at work right now but I'll see what I can do when I get home later tonight. :)
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post 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
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

Sparkie wrote:Here is a base for you to improve on. ;)
:D
I thank you, you make me happy
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

You're welcome. :)
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Post Reply